【问题标题】:Difference with Checkstyle and PMD configuration in maven parent modulemaven父模块中Checkstyle和PMD配置的区别
【发布时间】:2018-05-02 11:52:30
【问题描述】:

我有一个带有以下结构的 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 执行似乎只针对子模块发生,因此它可以顺利进行。

有人可以帮助我了解根本原因,并改进我的配置。

提前致谢。

【问题讨论】:

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


    【解决方案1】:

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

    作为一个选项,您可以只留下&lt;configLocation&gt;analyzers/checkstyle.xml&lt;/configLocation&gt; 而没有{baseDir}{projectRootDir},它可以正常工作。

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

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

    【讨论】:

    • 哇.. &lt;configLocation&gt;analyzers/checkstyle.xml&lt;/configLocation&gt; 就像一个魅力,当我从子目录的父级运行构建时。感谢那。你能解释一下pmd plugin only runs for child module吗,因为这个插件也列在父pom本身的&lt;build&gt;&lt;plugins&gt;&lt;plugin&gt;下,我希望它也能为父模块执行,与包装类型无关。
    • @ramtech 不幸的是,我真的不知道它是如何实现的,但是如果您将一些不存在的文件添加到规则集中,pmd 将失败,并出现关于找不到文件的预期异常,但这只会发生在孩子身上模块,父模块会成功,我看到的唯一体面的解释,这个插件忽略了父模块。
    • 好的。我在项目中拥有的实际 checkstyle xml 有一些对 ${config_loc} 属性的引用,所以我在 pom 中添加了这个配置来定义该属性:&lt;propertyExpansion&gt;config_loc=${projectRootDir}/analyzers&lt;/propertyExpansion&gt; - checkstyle.xml 文件的目录路径。此修改后的配置&lt;propertyExpansion&gt;config_loc=analyzers&lt;/propertyExpansion&gt; 导致失败,同样是由于相对路径问题。无论如何,我可以选择更新我的 checkstyle xml。接受这个作为答案,因为它回答了原始问题。非常感谢@ruslan-akhundov
    猜你喜欢
    • 2012-08-08
    • 1970-01-01
    • 1970-01-01
    • 2015-12-02
    • 1970-01-01
    • 2018-09-11
    • 2014-01-19
    • 1970-01-01
    • 2019-03-25
    相关资源
    最近更新 更多