How to activate Maven profile in dependent module?

Suppose I have an A:jar module whose dependencies on the execution and compilation cycles depend on the version of the JDK. In my example, I have a pre-jdk6-profile for the JAXB API: before JDK 1.6.0, I need to enable jaxb-api-nnn.jar as a compilation dependency, This profile is placed in A.pom .

I also have a B:war module, which depends on A:jar . I want to be able to activate this profile on the build server to build JDK 1.5.x. When I start Maven with the given profile, I get the message:

 mvn -Ppre-jdk6-profile -o install [WARNING] Profile with id: 'pre-jdk6-profile' has not been activated. 

and jaxb-api-nnn.jar missing as a result of B.war . However, if I activate this profile when creating from the parent pom.xml , everything is fine. This means that profiles are not inherited from dependencies, and the parent multi-module pom.xml was able to correctly build everything, because it seems that all profiles are combined into a reactor.

Changing the profile to the parent pom complicates the situation, since dependencies apply to all other projects (for example, to C:ear ). Are there any good solutions for this problem, namely, if any module A depends on module B , then all the correct processing of compilation and execution dependencies that are activated by the profile?

The profile in project A:jar follows:

 <project ...> <artifactId>A</artifactId> <packaging>jar</packaging> ... <parent> <artifactId>P</artifactId> ... </parent> <profiles> <profile> <id>pre-jdk6-profile</id> <activation> <jdk>(,1.6.0)</jdk> </activation> <dependencies> <dependency> <groupId>javax.xml.ws</groupId> <artifactId>jaxws-api</artifactId> </dependency> </dependencies> </profile> </profiles> ... </project> 
+7
maven-2
source share
2 answers

a) In a multi-module assembly, you should always build from the top, and not from a separate module. If you want to build only one module, use advanced reactor parameters (see Mvn --help) as follows:

 mvn -pl mymodule 

b) Define and activate the profile in the parent pom, but add the configuration to the child pom.

parent pom.xml

 <profiles> <profile> <id>pre-jdk-6</id> <activation> <jdk>(,1.6.0)</jdk> </activation> </profile> </profiles> 

child pom.xml

 <profiles> <profile> <id>pre-jdk-6</id> <dependencies> <dependency> <groupId>javax.xml.ws</groupId> <artifactId>jaxws-api</artifactId> </dependency> </dependencies> </profile> </profiles> 
+8
source share

A few notes after the fact:

  • When you use -P profName , it activates a profile called "profName"
  • After that, it disables all profiles for which there is a <activation> . It doesn’t matter if they are activated using the java version, as in the example, or by default or the value env or something else.
  • This means that -P causes any other activated profile to be deactivated.

Solution: use <activation><jdk>...</jdk></activation> or use -P , but do not use both.

+1
source share

All Articles