Question

Is it possible to have a plugin defined in the parent POM which is deactivated, and when the child inherits this plugin it gets automatically activated?

Was it helpful?

Solution

I guess you want to configure the plugin in your parent pom, but use it only in the inherited projects. Maven has a section for this - configure your plugins in pluginManagement, but bind them to a phase just when you needed it, e.g. omit the phase tag in pluginManagement, but specify it under in you inherited pom.

OTHER TIPS

So 'siddhadev' is exactly correct. You can define the plugin configuration in the parent pom with a given id:

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <execution>
                        <id>child-caller</id>
                        <!-- 'phase' omitted -->
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <tasks>
                                <echo message="called from child!" />
                            </tasks>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

And, in the child POM, you can explicitly list the phase where this should be called:

<build>
    <plugins>
        <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>
                <execution>
                    <id>child-caller</id>
                    <phase>compile</phase>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

I've used this for targeting various JREs. Unfortunately, because you can't use the maven-compiler-plugin with different destination directories (which I consider a bug in the plugin), you must use Ant.

This isn't exactly what you're after, but I think it will work well enough for you.

If you declare the plugin in a pluginManagement tag in the parent, the configuration will be inherited by any child projects that declare that plugin.

For example, in the parent declare that the compile plugin uses Java 5 for test compilation.

<pluginManagement>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <executions>
        <execution>
          <id>test-compile</id>
          <goals>
            <goal>testCompile</goal>
          </goals>
          <configuration>
            <source>1.5</source>
            <target>1.5</target>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</pluginManagement>

Then in a child, you simple declare the compiler plugin and the configuration from the parent will be inherited:

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
    </plugin>
  </plugins>
</build>

You can declare a plugin at the top level pom and tell it to be skipped and then tell it to not be skipped at the child level. It's not quite automatic, but very minimal in the override verbosity.

Parent Pom, disabling the plugin, but declaring all the config:

<plugin>
    <groupid>org.apache.maven.plugins</groupid>
    <artifactid>maven-surefire-plugin</artifactid>
    <configuration>
        <skip>true</skip>
        ...lots more config...
        ...lots more config...
        ...lots more config...
    </configuration>
</plugin>

Child Pom, enabling the plugin:

<plugin>
    <groupid>org.apache.maven.plugins</groupid>
    <artifactid>maven-surefire-plugin</artifactid>
    <configuration>
        <skip>false</skip>
    </configuration>
</plugin>

I went with the following solution:

  • Configure the plugin in the parent-pom in the pluginManagement-section. Bind the plugin to an existing phase.

  • Deactivate the plugin for the parent-pom by binding it to a nonexistent phase: Override the phase in the plugins-section.

  • Activate the plugin in each child-pom by including the plugin in the plugins section.

Example parent-pom:

<defaultGoal>install</defaultGoal>
    <pluginManagement>
        <plugins>
            ...
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-install-plugin</artifactId>
                <executions>
                    <execution>
                        <id>install-ejb-client</id>
                        <phase>install</phase>
                        <goals>
                        <goal>install-file</goal>
                        </goals>
                        <configuration>
                            <file>${ejb-client-file}</file>
                            <groupId>${project.groupId}</groupId>
                            <artifactId>${project.artifactId}</artifactId>
                            <version>${project.version}</version>
                            <packaging>jar</packaging>
                            <classifier>client</classifier>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
                    ...                 
        </plugins>
    </pluginManagement>
    <plugins>
            <plugin>
                <!-- deactivate the plugin for this project, only child-projects do generate ejb-clients -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-install-plugin</artifactId>
                <inherited>false</inherited>
                <executions>
                    <execution>
                        <id>install-ejb-client</id>
                        <phase>none</phase>
                    </execution>
                </executions>
            </plugin>
            ...
    </plugins>
</build>

Example child-pom:

<build>
    <plugins>
            ...
        <plugin>
            <!-- Install the generated client-jar. Property 'ejb-client-file' has to be set! Plugin configuration is in the parent pom -->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-install-plugin</artifactId>
        </plugin>       
            ... 
    </plugins>
</build>

As far as I know, there is no generic solution for this. At least for the moment...

One idea (I didn't try it, but it may work) is to define, in the parent pom.xml an execution goal that does not exist, for example:

<executions>
    <execution>
        <goals>
            <goal>noGoal</goal>
        </goals>
    </execution>
</executions>

and in every child, you redefine a correct goal.

The problem of this solution (if it works, of course ;) ) is that you must redefine the plugin configuration for every child. Otherwise, it will not be executed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top