与maven父模块中的Checkstyle和PMD配置不同

ram*_*ech 5 java checkstyle pmd maven maven-checkstyle-plugin

我有一个maven的java应用程序具有以下结构:

parent
| - pom.xml
| - child
    | - pom.xml
| - analyzers
    | - pmdrules.xml
    | - checkstyle.xml
Run Code Online (Sandbox Code Playgroud)

我在父pom.xml中配置了PMD和checkstyle.对于PMD,规则集配置如下,它对父模块和子模块都可以正常工作:

<configuration>
    <rulesets>
        <ruleset>${basedir}/../analyzers/pmdrules.xml</ruleset>
    </rulesets>
</configuration>
Run Code Online (Sandbox Code Playgroud)

但是,对于checkstyle,如果我以相同的方式配置configLocation,它在父节点或子节点中都会失败.我必须使用自定义属性来克服这个问题.

<configuration>
    <!-- Below config with ${projectRootDir}, a custom property always pointing to parent root works fine -->
    <configLocation>${projectRootDir}/analyzers/checkstyle.xml</configLocation>
    <!-- Below configurations with ${basedir} will fail build for child -->
    <!--<configLocation>${basedir}/analyzers/checkstyle.xml</configLocation>-->
    <!-- Below configurations with ${basedir} will fail build for parent -->
    <!--<configLocation>${basedir}/../analyzers/checkstyle.xml</configLocation>-->
</configuration>
Run Code Online (Sandbox Code Playgroud)

这是一个复制器样本 - https://github.com/ramtech123/pocs/blob/master/myapp-parent-module/pom.xml

我尝试在调试模式下运行maven构建.从日志中,对我而言,实际的PMD执行似乎仅针对子模块进行,因此它没有问题.

有人可以帮我理解根本原因,并改善我的配置.

提前致谢.

Rus*_*dov 3

当您创建子项时,pom.xml它会继承其父项配置,但诸如baseDir会被子项覆盖,因此在模板化您的插件配置时它会使用自己的路径。

作为一个选项,您可以<configLocation>analyzers/checkstyle.xml</configLocation>不使用{baseDir}{projectRootDir},它工作得很好。

然而,您的解决方案也很好,正如您projectRootDir在父根中指定的那样,并且它在那里进行评估,并且子 pom 不会覆盖您的自定义属性,因此它是正确的。

您的 pmd 始终有效,因为 pmd 插件仅针对子模块运行。