Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to activate a Maven profile in a dependent module?

Tags:

maven-2

Suppose I have a module A:jar, whose runtime and compilation set of dependencies depends on the JDK version. In my example, I have a pre-jdk6-profile for JAXB API: prior to JDK 1.6.0 I need to include jaxb-api-nnn.jar as a compile dependency. This profile is placed to A.pom.

I also have module B:war, which depends on A:jar. I want to be able to activate this profile on a build server to build the JDK 1.5.x deliverable. When I execute Maven with a given profile activated, 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 is missing in resulting B.war. However if I activate this profile when building from the parent pom.xml, everything is OK. That means the profiles are not inherited from dependencies, and the parent multi-module pom.xml was able to build everything correctly because it seems like all profiles are merged in reactor.

Shifting the profile to parent pom makes things worse, as the dependencies are applied to all other projects (e.g. to C:ear). Are there nice solutions for this task, namely, if any module A depends on module B, then all compile and runtime dependencies which are activated by a profile, are correctly handled?

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>
like image 269
dma_k Avatar asked Aug 20 '10 15:08

dma_k


People also ask

Is it possible to add library dependencies inside a profile in Maven?

Within each profile element, we can configure many elements such as dependencies, plugins, resources, finalName. So, for the example above, we could add plugins and their dependencies separately for integration-tests and mutation-tests.


1 Answers

Several notes way after the fact:

  1. When you use -P profName, it activates a profile named 'profName'
  2. After that, it disables all profiles that have an <activation> tag for them. It doesn't matter whether they are activated by the java version, as in the example, or by default or env value or anything.
  3. That means the -P causes any otherwise activated profile to become deactivated.

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

like image 141
Lee Meador Avatar answered Sep 19 '22 12:09

Lee Meador