Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven inherit checkstyle from parent

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.

like image 386
dani77 Avatar asked Dec 08 '25 09:12

dani77


1 Answers

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.

like image 76
Naman Avatar answered Dec 10 '25 21:12

Naman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!