【问题标题】:Maven inherit checkstyle from parentMaven从父级继承checkstyle
【发布时间】:2017-08-04 23:26:51
【问题描述】:

我有一个带有这种 checkstyle 配置的 父 POM

<properties>
    <checkstyle.configLocation>src/checkstyle/checkstyle.xml</checkstyle.configLocation>
    <checkstyle.suppressionsLocation>src/checkstyle/checkstyle-suppressions.xml</checkstyle.suppressionsLocation>
    <maven-checkstyle-plugin.version>2.17</maven-checkstyle-plugin.version>
<properties>

<build>
    <pluginManagement>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-checkstyle-plugin</artifactId>
            <version>${maven-checkstyle-plugin.version}</version>
            <configuration>
                <skip>true</skip>
            </configuration>
            <executions>
                <execution>
                    <id>validate</id>
                    <phase>validate</phase>
                    <configuration>
                        <configLocation>${checkstyle.configLocation}</configLocation>
                        <suppressionsLocation>${checkstyle.suppressionsLocation}</suppressionsLocation>
                    </configuration>
                </execution>
            </executions>     
        </plugin>
    </pluginManagement>
</build>

这是继承的 POM

<build>
    <plugins>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-checkstyle-plugin</artifactId>
    </plugins>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-checkstyle-plugin</artifactId>
                <configuration>
                    <configLocation>${project.parent.basedir}/${checkstyle.configLocation}</configLocation>
                    <suppressionsFile>${project.parent.basedir}/${checkstyle.suppressionsLocation}</suppressionsFile>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

我只想在继承的项目中执行 checkstyle(因为我将有多个使用父 pom 的项目)。在父项目中,不需要运行 checkstyle,但我必须在父项目上使用 checkstyle.xmlcheckstyle-suppressions.xml 配置文件,并使用来自继承的项目。

这是mvn clean install后得到的错误:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-checkstyle-plugin:2.17:check (validate) on project MyProject: Failed during checkstyle execution: Unable to find suppressions file at location: src/checkstyle/checkstyle-suppressions.xml: Could not find resource 'src/checkstyle/checkstyle-suppressions.xml'. -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

子项目没有找到父项目的checkstyle配置文件。

【问题讨论】:

  • 日志读取 src/checkstyle/checkstyle-suppressions.xml:,而属性则不同 src/**main/resources**/checkstyle-抑制.xml。即使无法获得正确的父路径,使用属性替换的路径值也不会反映在日志中。请确认您分享的路径值和日志是否正确。
  • 对不起,我复制了一个不好的属性。在我的电脑中,这些属性都可以。
  • 在继承的 pom 中使用值为 ${project.parent.basedir}/${checkstyle.configLocation} 的属性并在配置中替换它可以解决吗?
  • 如果我在子 pom 中更改并添加此属性:&lt;checkstyle.configLocation&gt;C:\Users\daniel\git\parent-project\src\checkstyle\checkstyle.xml&lt;/checkstyle.configLocation&gt; &lt;checkstyle.suppressionsLocation&gt;C:\Users\daniel\git\parent-project\src\checkstyle\checkstyle-suppressions.xml&lt;/checkstyle.suppressionsLocation&gt; 它工作正常。但这不是想法。
  • 您也可以在继承的 pom 中添加类似的属性,您可以使用不同的值作为 -${project.parent.basedir}/${checkstyle.configLocation},然后可以在配置中使用该 prperty

标签: java maven inheritance maven-plugin checkstyle


【解决方案1】:

来自文档

pluginManagement:是一个可以在侧面插件中看到的元素。 插件管理以几乎相同的方式包含插件元素, 除了为此配置插件信息 特定的项目构建,它旨在配置项目构建 从这个继承。但是,这只配置插件 实际上是在子元素的 plugins 元素中引用的。 孩子们有权覆盖 pluginManagement 定义。

考虑到您的继承 POM,使用的 maven-checkstyle-plugin 将是您首先声明的(在 pluginManagement 之外)。取而代之的是,对于继承的pom.xml,要覆盖配置,您必须在plugins 而不是pluginManagement 下指定相同的配置。尝试将 pom 的构建标签简化为 --

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-checkstyle-plugin</artifactId>
            <!--Note - the version would be inherited-->
            <configuration>
                <configLocation>${project.parent.basedir}/${checkstyle.configLocation}</configLocation>
                <suppressionsFile>${project.parent.basedir}/${checkstyle.suppressionsLocation}</suppressionsFile>
            </configuration>
        </plugin>
    </plugins>
</build>

Edit : - 从评论中可以看出,OP 没有使用 Simple Inheritance structure,因此在这种情况下使用 相对路径 可以解决问题.

【讨论】:

【解决方案2】:

如果您继承的项目位于不同的存储库中,则需要另一种方法。只需将父级添加为插件中的依赖项即可。请注意,您不能在 &lt;reporting&gt; 中使用此方法,因为它不支持插件依赖项。

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-checkstyle-plugin</artifactId>
            <!--version is inherited-->
            <dependencies>
                <dependency>
                    <groupId>parent.group.id</groupId>
                    <artifactId>parent</artifactId>
                    <version>1.0.0-SNAPSHOT</version>
                </dependency>
            </dependencies>
         </plugin>
    </plugins>
</build>

有关详细信息,请参阅 https://maven.apache.org/plugins/maven-checkstyle-plugin/examples/multi-module-config.html

【讨论】:

    猜你喜欢
    • 2012-12-02
    • 1970-01-01
    • 1970-01-01
    • 2012-12-22
    • 1970-01-01
    • 2012-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多