Java Maven从父项继承checkstyle

Java Maven从父项继承checkstyle,java,maven,inheritance,maven-plugin,checkstyle,Java,Maven,Inheritance,Maven Plugin,Checkstyle,我有一个具有此checkstyle配置的父POM: <properties> <checkstyle.configLocation>src/checkstyle/checkstyle.xml</checkstyle.configLocation> <checkstyle.suppressionsLocation>src/checkstyle/checkstyle-suppressions.xml</checkstyle.sup

我有一个具有此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>
子项目找不到父项目中的checkstyle配置文件。

来自文档

插件管理:是一个可以在插件旁边看到的元素。 插件管理包含插件元素的方式大致相同, 除此之外,不需要为此配置插件信息 对于特定的项目构建,它旨在配置项目构建 从这个继承下来的。但是,这只配置 实际上是在子元素的plugins元素中引用的。 孩子们完全有权凌驾于插件管理之上 定义

考虑到您继承的POM,使用的
maven checkstyle插件将是您首先声明的插件(在插件管理之外)。相反,对于继承的
pom.xml
,要覆盖配置,必须在
插件下指定相同的配置,而不是
插件管理下的配置。尝试将pom的build标记简化为--


org.apache.maven.plugins

因此,在这种情况下,使用相对路径可以解决问题。

如果继承的项目处于不同的回购中,则需要另一种方法。只需在插件中添加父项作为依赖项。请注意,您不能在
中使用此方法,因为它不支持插件依赖项

<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>

org.apache.maven.plugins

日志读取src/checkstyle/checkstyle suppressions.xml:,而属性与src/**main/resources**/checkstyle-suppressions.xml有很大不同。即使无法为父级获取正确的路径,要使用属性替换的路径值也不会反映在日志中。请确认您共享的路径值和日志是否正确。对不起,我复制了一个错误的属性。在我的电脑中,这些属性是可以的。在继承的pom中使用值为
${project.parent.basedir}/${checkstyle.configLocation}的属性
并在那里的配置中替换它可以解决问题吗?如果我更改并在子pom中添加此属性:
C:\Users\daniel\git\parent project\src\checkstyle\checkstyle.xml
C:\Users\daniel\git\parent project\src\checkstyle\checkstyle suppressions.xml
它工作正常。但这不是我们的想法。您也可以在继承的pom中添加类似的属性,在其中您可以使用不同的值作为-
${project.parent.basedir}/${checkstyle.configLocation}
然后,在3.0.4及更高版本的配置中可以使用性能。这种方法不再有效:@nullpointer我也有类似问题,如本问题所述。我这里有一个复制器:使用相对路径可以很好地用于PMD(请参阅myapp父模块pom.xml中的第57行),但无法用于checkstyle(第34-36行)。最后一个选项是使用自定义属性(第32-33行)配置checkstyle,该属性始终指向项目根目录。我想知道为什么相对路径选项适用于PMD,但不适用于checkstyle
[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
<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>
<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>