Struggling with Maven parent/child plugin configuration inheritance

20,795

Solution 1

Ok, I think I have it. The answer, in my case, relates to what you specified - I did need the tag. However the solution was in the tag; by binding it to a non-phase, it does execute. This I knew. What I discovered is that the had to match in order for it to override. Thus, the config never gets parsed and doesn't matter.

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <!-- Main declaration of the plugin -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-checkstyle-plugin</artifactId>
                <version>2.9.1</version>
                <executions>
                    <execution>
                        <!--This must be named-->
                        <id>checkstyle</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>check</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration...>
            </plugin>
        </plugins>
    </pluginManagement>
    <plugins>
        <plugin>
            <!-- Uses the default config -->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-checkstyle-plugin</artifactId>
            <inherited>false</inherited>
            <executions>
                <execution>
                    <!--This matches and thus overrides-->
                    <id>checkstyle</id>
                    <phase>none</phase>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Solution 2

You can explicitly specify in your parent pom that the plugin should not be inherited:

<build>
  <pluginManagement>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-checkstyle-plugin</artifactId>
            <version>2.9.1</version>
            <executions...>
            <configuration>
                <configLocation>
                    (used by all children)
                </configLocation>
            </configuration>
        </plugin>
    </plugins>
</pluginManagement>
<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-checkstyle-plugin</artifactId>
        <inherited>false</inherited>                      <!-- Add this line -->
        <configuration>
            <configLocation>
                (unique to the parent)
            </configLocation>
        </configuration>
    </plugin>
  </plugins>
<build>

And in your child pom, you need to specify the plugin (the config will then come from the <pluginManagement> parent element.

<build>
  <plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-checkstyle-plugin</artifactId>
    </plugin>
  </plugins>
<build>
Share:
20,795

Related videos on Youtube

end-user
Author by

end-user

Lov'n me some Springframework! But also deal with a vast array of technologies.

Updated on January 17, 2020

Comments

  • end-user
    end-user over 4 years

    I'm trying to write a parent pom, and I have a plugin defined, but I need to change the config for all inherited instances. So, I can put some configuration in the <pluginManagement> definition, and I can override it in the <plugin>, but how do I get the children to default back to the <pluginManagement> version?

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-checkstyle-plugin</artifactId>
                    <version>2.9.1</version>
                    <executions...>
                    <configuration>
                        <configLocation>
                            (used by all children)
                        </configLocation>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-checkstyle-plugin</artifactId>
                <configuration>
                    <configLocation>
                        (unique to the parent)
                    </configLocation>
                </configuration>
            </plugin>
        </plugins>
    <build>
    

    So, what happens is the children continue to show the parent's config.

  • end-user
    end-user over 11 years
    Well, yes, that's correct. However, it means specifying the plugin explicitly in every child. I was liking the idea that I could inherit from the parent automatically.