Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a maven profile inherit from another maven profile?

Tags:

maven

I have two profiles that are nearly identical. Rather than duplicating the configuration in each, I'd like for one profile to "inherit" from the other, but I don't see an obvious way to do this using maven 3.

Is profile inheritance possible in maven?

like image 381
emmby Avatar asked Nov 10 '11 18:11

emmby


2 Answers

No, profiles cannot be inherited.

However, you can have multiple active profiles, which could allow you to accomplish roughly the same thing by factoring out the common elements in the two profiles into a third profile that you activate whenever you activate one of the other two profiles.

like image 148
Spencer Uresk Avatar answered Sep 16 '22 13:09

Spencer Uresk


Strictly speaking profiles are not inherited in that profiles defined in a parent pom are not available in child poms, as per maven documentation. HOWEVER, properties and build plugins (not pluginManagement) that are specified in parent poms can get activated and hence modify the build environment for the child poms. For example if I have a parent pom A, which has the following profiles section:

  </profiles>
      <profile>
            <id>integrationTest</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <configuration>
                            <parallel>none</parallel>
                            <excludes>
                                <exclude>**/*Test.java</exclude>
                            </excludes>
                        </configuration>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-failsafe-plugin</artifactId>
                        <configuration>
                            <includes
                                <include>**/*IT.java</include>
                            </includes>
                            <!-- need to exclude nothing as it is possible that some by default some tests are excluded and we need to override that -->
                            <excludes>
                                <exclude>none</exclude>
                            </excludes>
                            <parallel>none</parallel>
                        </configuration>
                    </plugin>            
                </plugins>
            </build>
        </profile>
   </profiles>

And then a child pom B which doesn't have failsafe plugin configured in the default build but inherits from A pom via the parent tag and I then run 'mvn clean install -P integrationTest' from the child pom directory, it will take the plugin information specified in A.

like image 20
Deepak Avatar answered Sep 17 '22 13:09

Deepak