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

3

130

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.

Rodd answered 20/11, 2009 at 16:16 Comment(2)
How is the wls7 profile activated?Sculpin
The profiles wls7 and wls10 are both "activeByDefault" in the parent POM. But according customer needs, only the wls10 or both are built by scripts (with the "-P" parameter)Rodd
R
165

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

Riparian answered 14/11, 2011 at 9:47 Comment(2)
It appears that for Maven2.2.1 if you do this in a profile it doesn't merge with plugins defined in parent profiles but overrides them. If you define the same plugin directly in the build section it works. For Maven3 it resolved as expected.Colubrid
It did not work for me. I wanted to rebuild Jenkins NodeJS Plugin v1.0 with version 1.580.1 of org.jenkins-ci.plugins in the pom.xml using Maven 3.3.9. Until I manually changed the <source> to 1.7 in the ~/.m2/repository/org/jenkins-ci/jenkins/1.34/jenkins-1.34.pom nothing worked.Factious
A
7

i had the same issue. By default my maven war plugin excluded a html file. But in my acceptance-tests profile i wanted this file included. So when i added in the maven war plugin again it did not override the default.

To resolve this issue i passed in the combine.self attribute and worked fine.

Default build:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.4</version>
    <configuration>
        <packagingExcludes>swagger-ui/client.html</packagingExcludes>
    </configuration>
</plugin>

Acceptance test profile:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.4</version>
    <configuration combine.self="override"/>
</plugin>
Anesthetic answered 17/9, 2013 at 6:3 Comment(0)
S
1

Did you try to deactivate the wls7 profile (since maven 2.0.10):

Starting with Maven 2.0.10, one or more profiles can be deactivated using the command line by prefixing their identifier with either the character '!' or '-' as shown below:

mvn groupId:artifactId:goal -P !profile-1,!profile-2

This can be used to deactivate profiles marked as activeByDefault or profiles that would otherwise be activated through their activation config.

And then add your configuration in a profile with a different name or directly in your pom.xml.

Sculpin answered 20/11, 2009 at 18:25 Comment(7)
As I said above, I can't get rid of the parent POM since I inherite many configurations defined for all my company at different levels in the framework. And duplicating profiles should not be a good idea, because I would need to report changes in the parent POM and most often I am not aware of them. I just would like to override the behavior only for the compilation of test classes in my project.Rodd
Reread my answer, that's not what I suggested. I suggested to deactivate a profile, not to get rid of the parent POM. Then, why would you have to report changes in the parent pom? Nothing forces you to do that.Sculpin
Yes Pascal, thanks for your help, but the problem is that if I deactivate the wls7 profile I get rid a lot of configuration (for other plugins, maven general stuff, ...) I still need. And by reporting changes, I meant FROM the parent POM TO my POM. Because, with the solution you suggested, I would need to duplicate all the parent POM (except for the section for compilation of test classes) and if the parent POM responsible changes something in his POM, I need to be warn of any change which is not the current process and not very practical.Rodd
Ohh, ok, I get it now. However, I'm not sure (but I may be wrong) you can override a pom partially so I don't have any better solution with the provided details.Sculpin
Anyway, many thanks Pascal for trying to help me. In fact, I need such a behaviour because of a particular reason. Maybe there is another way to perform it :Rodd
The build process of the product in my company build 2 outputs, one for Weblogic7/Java1.4 and another for Weblogic10/Java5 (that's why 2 profiles "wls7" and "wls10"). But for unit testing purposes, I use a mocking framework "JMockit" which runs only Java 5+ . This should not be a problem since the requirement of Java4 for the profile "wls7" is only for runtime, not for compiling and running tests. I tried to configure only the maven-compiler-plugin in my POM but it seems to be ignored and the active profile is never overriden.Rodd
Yeah, I guessed what you were trying to do but, sadly, my best answer is: if the wls7 profile doesn't suit your needs, don't use it (there may be a better solution but, as I don't know how the wls7 profile is activated, I can't describe all options).Sculpin

© 2022 - 2024 — McMap. All rights reserved.