3

I have a parent POM with this checkstyle configuration:

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

And this is the inherited 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>

I want to execute the checkstyle only in the inherited projects (because I will have multiple projects that use the parent pom). In parent project the checkstyle is not necessary to run but I have to use the checkstyle.xml and checkstyle-suppressions.xml configuration files on the parent project and use those files from inherited projects.

This is the error obtained after 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

The child project doesn't find the checkstyle configuration files from parent project.

dani77
  • 199
  • 2
  • 5
  • 26
  • The log reads *src/checkstyle/checkstyle-suppressions.xml:*, while the properties is way different *src/**main/resources**/checkstyle-suppressions.xml*. Even if it fails to get the correct path for parent, the path value to be replaced using properties is not reflected in the logs. Please confirm if the path values and logs shared by you are correct. – Naman Mar 14 '17 at 15:07
  • Sorry, I copied a bad properties. In my PC these properties are OK. – dani77 Mar 14 '17 at 15:17
  • does using a property within inherited pom with value `${project.parent.basedir}/${checkstyle.configLocation}` and replacing that in the configuration there solve it? – Naman Mar 14 '17 at 15:21
  • If I change and add this properties in child pom: `C:\Users\daniel\git\parent-project\src\checkstyle\checkstyle.xml` `C:\Users\daniel\git\parent-project\src\checkstyle\checkstyle-suppressions.xml` It works OK. But it is not the idea. – dani77 Mar 14 '17 at 15:28
  • You can add similar properties in the inherited pom as well, where you can use different value as - `${project.parent.basedir}/${checkstyle.configLocation}` and then that prperty can be used in the configuration – Naman Mar 14 '17 at 15:30
  • Also just to confirm, as I was assuming this. By Inherited POM you mean that it is a submodule of the Parent POM, right? – Naman Mar 14 '17 at 15:39
  • I got the solution in this example: http://stackoverflow.com/questions/2165686/in-maven-can-i-specify-a-relative-path-above-my-current-project Thanks! – dani77 Mar 14 '17 at 18:58
  • B, which structure were you using ? flat layout or when the parent isn't directly above the module? – Naman Mar 15 '17 at 03:42
  • We have a main project as parent with a root pom only. Then we have a parent-project (it is a child of root pom) with checkstyle definition and files and finally we have a MyProject which is the grand child. root-pom - Grand Project parent-project - Parent Project MyProject - Child Project – dani77 Mar 15 '17 at 17:30

2 Answers2

1

From the docs

pluginManagement: is an element that is seen along side plugins. Plugin Management contains plugin elements in much the same way, except that rather than configuring plugin information for this particular project build, it is intended to configure project builds that inherit from this one. However, this only configures plugins that are actually referenced within the plugins element in the children. The children have every right to override pluginManagement definitions.

Considering your Inherited POM, the maven-checkstyle-plugin used would be the one you've declared first(outside the pluginManagement). Instead of this, for the inherited pom.xml, to override the configuration, you must specify the same under the plugins and not pluginManagement. Try simplifying the pom's build tag as --

<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 : - From the comment, it was visible that the OP was not using the Simple Inheritance structure hence using a relative path would solve the problem in such cases.

Naman
  • 27,789
  • 26
  • 218
  • 353
  • In 3.0.4 and above this approach no longer works: https://stackoverflow.com/questions/1012402/maven2-property-that-indicates-the-parent-directory#comment15100548_1019431 – DeaconDesperado Feb 05 '18 at 21:40
  • @nullpointer I also have the similar issue as explained in this question. I have a reproducer here: https://github.com/ramtech123/pocs/blob/master/myapp-parent-module/pom.xml Using relative path works fine for PMD (refer line# 57 in pom.xml of myapp-parent-module), but fails for checkstyle (line# 34-36). Configuring checkstyle with a custom property (line# 32-33), which always points to project root directory is the last option that I ended up with. I'm wondering why the relative path option works fine for PMD, but not for checkstyle. – ramtech May 02 '18 at 11:19
  • Here is the link to related question https://stackoverflow.com/a/50134874/2685234 – ramtech May 02 '18 at 14:44
0

If your inherited project is in a different repo, you need another approach. Just add the parent as a dependency in the plugin. Note that you can't use this approach in <reporting> because it doesn't support plugin dependencies.

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

For more info, see https://maven.apache.org/plugins/maven-checkstyle-plugin/examples/multi-module-config.html

mjlitz
  • 148
  • 10