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

时间:2018-05-02 11:52:31

标签: java maven checkstyle pmd maven-checkstyle-plugin

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

parent
| - pom.xml
| - child
    | - pom.xml
| - analyzers
    | - pmdrules.xml
    | - checkstyle.xml

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

<configuration>
    <rulesets>
        <ruleset>${basedir}/../analyzers/pmdrules.xml</ruleset>
    </rulesets>
</configuration>

但是,对于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>

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

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

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

提前致谢。

1 个答案:

答案 0 :(得分:3)

当你创建子pom.xml时,它继承了它的父配置,但是baseDir这样的属性被子程序覆盖,所以它在模板化你的插件配置时使用它自己的路径。

作为一种选择,您可以离开<configLocation>analyzers/checkstyle.xml</configLocation>而不{baseDir}{projectRootDir},它可以正常工作。

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

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

相关问题