Is it possible to override the configuration of a plugin already defined for a profile in a parent POM?

Guillaume Cernier picture Guillaume Cernier · Nov 20, 2009 · Viewed 79.5k times · Source

In a POM parent file of my project, I have such a profile defining some configurations useful for this project (so that I can't get rid of this parent POM) :

<profile>
<id>wls7</id>
...
<build>
  <plugins>
    <!-- use java 1.4 -->
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <configuration>
        <fork>true</fork>
        <source>1.4</source>
        <target>1.4</target>
        <meminitial>128m</meminitial>
        <maxmem>1024m</maxmem>
        <executable>%${jdk14.executable}</executable>
      </configuration>
    </plugin>
  </plugins>
</build>

...
</profile>

But in my project I just would like to override the configuration of the maven-compiler-plugin in order to use jdk5 instead of jdk4 for compiling test-classes.

That's why I did this section in the POM of my project :

<profiles>
  <profile>
    <id>wls7</id>
        <activation>
            <property>
                <name>jdk</name>
                <value>4</value>
            </property>
        </activation>
    <build>
      <directory>target-1.4</directory>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <executions>
            <execution>
              <id>my-testCompile</id>
              <phase>test-compile</phase>
              <goals>
                <goal>testCompile</goal>
              </goals>
              <configuration>
                <fork>true</fork>
                <executable>${jdk15.executable}</executable>
                <compilerVersion>1.5</compilerVersion>
                <source>1.5</source>
                <target>1.5</target>
                <verbose>true</verbose>
              </configuration>
            </execution>
          </executions>
        </plugin>
      </plugins>
    </build>
  </profile>
              ...
</profiles>

and it's not working ...

I even tried to override the configuration in regular plugin sections of my POM (I mean, not for a specific profile but for my whole POM).

What could be the problem ?

To clarify some of my requirements :

  • I don't want to get rid of the parent POM and the profile (wls7) defined inside it (since I need many and many properties, configurations, ...) and that is not the process in my company.
  • A solution based on duplicating the parent POM and/or the profile defined inside it is not a good one. Since if the responsible of
    the parent POM change something, I
    would have to report it in mine.

It's just an inheritance matter (extend or override a profile, a configuration from an upper-level POM) so I think it should be possible with Maven 2.

Answer

Rolf picture Rolf · Nov 14, 2011

Overriding configurations from a parent pom can be done by adding the combine.self="override" attribute to the element in your pom.

Try changing your plugin configuration to:

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <executions>
        <execution>
          <id>my-testCompile</id>
          <phase>test-compile</phase>
          <goals>
            <goal>testCompile</goal>
          </goals>
          <configuration combine.self="override">
            <fork>true</fork>
            <executable>${jdk15.executable}</executable>
            <compilerVersion>1.5</compilerVersion>
            <source>1.5</source>
            <target>1.5</target>
            <verbose>true</verbose>
          </configuration>
        </execution>
      </executions>
    </plugin>

For more information on overriding plugins, see: http://maven.apache.org/pom.html