为何Maven集成PMD未扫描指定目录,无法检测代码违规?
Maven PMD插件未检测到JavaScript代码违规(扫描目录异常)
问题现象
通过Maven运行PMD时,日志显示已正确应用JavaScript语言及对应规则,但故意添加的违规代码未被识别。查看DEBUG日志发现,PMD实际在扫描PMD自身的Java源码文件,而非配置中指定的目标JavaScript目录。
配置文件(pom.xml中reporting部分)
<reporting> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-pmd-plugin</artifactId> <version>3.20.0</version> <configuration> <language>javascript</language> <rulesets> <ruleset>/category/ecmascript/bestpractices.xml</ruleset> <ruleset>/category/ecmascript/codestyle.xml</ruleset> <ruleset>/category/ecmascript/errorprone.xml</ruleset> </rulesets> <includes> <include>**/*.js</include> </includes> <excludes> <exclude>.*/node_modules/*.*</exclude> </excludes> <compileSourceRoots> <compileSourceRoot>${basedir}\..\..\TODO_list_javascript</compileSourceRoot> </compileSourceRoots> </configuration> <reportSets> <reportSet> <reports> <report>pmd</report> </reports> </reportSet> </reportSets> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jxr-plugin</artifactId> <version>3.3.0</version> </plugin> </plugins> </reporting>
关键日志片段
[DEBUG] Processing *path*/Applications/pmd-pmd_releases-7.0.0-rc1/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/ast/AbstractInfixEcmascriptNode.java [DEBUG] Processing *path*/Applications/pmd-pmd_releases-7.0.0-rc1/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/rule/bestpractices/ConsistentReturnRule.java [DEBUG] Processing *path*/Applications/pmd-pmd_releases-7.0.0-rc1/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/rule/AbstractEcmascriptRule.java [DEBUG] Processing *path*/Applications/pmd-pmd_releases-7.0.0-rc1/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/ast/TrailingCommaNode.java [DEBUG] No type information for node Expression [DEBUG] Processing *path*/Applications/pmd-pmd_releases-7.0.0-rc1/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/ast/EcmascriptVisitorBase.java [DEBUG] Processing *path*/Applications/pmd-pmd_releases-7.0.0-rc1/pmd-javascript/src/main /java/net /sourceforge/pmd/lang/ecmascript/ast/EcmascriptVisitor.java [DEBUG] No type information for node Expression [DEBUG] PMD finished. Found 0 violations. [DEBUG] Removing excluded violations. Using 0 configured exclusions. [DEBUG] Excluded 0 violations.
排查与解决方案
1. 修复路径分隔符问题
配置中使用Windows风格的\作为路径分隔符,在非Windows环境下会导致路径解析错误,改为Maven通用的/:
<compileSourceRoot>${basedir}/../../TODO_list_javascript</compileSourceRoot>
2. 验证目标目录路径正确性
运行以下命令查看当前项目根目录:
mvn help:evaluate -Dexpression=basedir
根据输出结果,手动验证../../TODO_list_javascript是否指向正确的JavaScript代码目录,确保目录存在且包含待扫描的.js文件。
3. 替换扫描目录配置项
部分版本的maven-pmd-plugin对sourceDirectories的支持更稳定,尝试替换compileSourceRoots:
<sourceDirectories> <sourceDirectory>${basedir}/../../TODO_list_javascript</sourceDirectory> </sourceDirectories>
4. 确认扫描范围
检查includes规则**/*.js是否覆盖目标目录下所有需要扫描的.js文件(包括子目录),若有特殊路径需求可调整规则。
5. 查看完整扫描日志
运行Maven命令时添加-X参数,查看完整DEBUG日志,确认PMD实际加载的扫描目录列表,排查是否有其他配置覆盖了指定目录。
内容的提问来源于stack exchange,提问作者Donald
相关产品推荐
相关产品推荐

