0

As part of a migration for a build from Java 1.7 to 1.8, I am trying to cleanly maintain a separation of the maven artifacts (the current JRE on the web server is still running 1.7 - it would be a Bad Thing to have a Java 8 compiled program get on the wrong server). I am attempting to use a classifier that is added as part of a profile to clearly distinguish the artifacts that are built.

There is a jar pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>parentpom</artifactId>
        <groupId>com.shagie.mvn</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <packaging>jar</packaging>

    <artifactId>the-jar</artifactId>
</project>

There is a war pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>parentpom</artifactId>
        <groupId>com.shagie.mvn</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>the-war</artifactId>

    <dependencies>
        <dependency>
            <groupId>com.shagie.mvn</groupId>
            <artifactId>the-jar</artifactId>
        </dependency>
    </dependencies>
</project>

There is also the parent pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.shagie.mvn</groupId>
    <artifactId>parentpom</artifactId>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>the-jar</module>
        <module>the-war</module>
    </modules>

    <packaging>pom</packaging>

    <properties>
        <java.version>1.7</java.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.shagie.mvn</groupId>
                <artifactId>the-jar</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <profiles>
        <profile>
            <id>jdk8</id>
            <properties>
                <java.version>1.8</java.version>
            </properties>
            <build>
                <pluginManagement>
                    <plugins>
                        <plugin>
                            <artifactId>maven-jar-plugin</artifactId>
                            <executions>
                                <execution>
                                    <phase>package</phase>
                                    <goals>
                                        <goal>jar</goal>
                                    </goals>
                                    <configuration>
                                        <classifier>jdk8</classifier>
                                    </configuration>
                                </execution>
                            </executions>
                        </plugin>
                        <plugin>
                            <artifactId>maven-war-plugin</artifactId>
                            <executions>
                                <execution>
                                    <phase>package</phase>
                                    <goals>
                                        <goal>war</goal>
                                    </goals>
                                    <configuration>
                                        <classifier>jdk8</classifier>
                                    </configuration>
                                </execution>
                            </executions>
                        </plugin>
                    </plugins>
                </pluginManagement>
            </build>
            <dependencyManagement>
                <dependencies>
                    <dependency>
                        <groupId>com.shagie.mvn</groupId>
                        <artifactId>the-jar</artifactId>
                        <version>1.0-SNAPSHOT</version>
                        <classifier>jdk8</classifier>
                    </dependency>
                </dependencies>
            </dependencyManagement>
        </profile>
    </profiles>
</project>

The individual artifacts do build correctly with the specified Java version.

Unfortunately, the war is not getting built using the jdk8 classifier specified jar as a dependency.

This can be seen when running mvn help:effective-pom -P jdk8 in the war project:

  ...
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>com.shagie.mvn</groupId>
        <artifactId>the-jar</artifactId>
        <version>1.0-SNAPSHOT</version>
      </dependency>
      <dependency>
        <groupId>com.shagie.mvn</groupId>
        <artifactId>the-jar</artifactId>
        <version>1.0-SNAPSHOT</version>
        <classifier>jdk8</classifier>
      </dependency>
    </dependencies>
  </dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>com.shagie.mvn</groupId>
      <artifactId>the-jar</artifactId>
      <version>1.0-SNAPSHOT</version>
      <scope>compile</scope>
    </dependency>
  </dependencies>
  ...

This is reflected when running the dependency graph:

$ mvn dependency:tree -P jdk8
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building the-war 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ the-war ---
[INFO] com.shagie.mvn:the-war:jar:1.0-SNAPSHOT
[INFO] \- com.shagie.mvn:the-jar:jar:1.0-SNAPSHOT:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

Is there a way to have a profile activation to change specific jar dependencies to use a classifier?

  • Building a module with different JDK's means either to build the module two time with different JDK or create separate modules one for building JDK 7 and one for building JDK 8 and best is to use toolchains for such things... – khmarbaise Mar 03 '17 at 19:23
  • It sounds like OP is indeed running two builds. As I read the question, the hope is that one of the builds will create classified artifacts that use classified dependencies and this is not happening. – user944849 Mar 03 '17 at 22:17
  • @user944849 is correct. I am trying to set up the second build. Activating `-P jdk8` profile compiles code targeted for 1.8. The problem is that the war build is still pulling in the 1.7 built jar dependency. The poms above can be tested without needing any source behind them to see the dependency (and re-reading it, I see a typo... fixing) –  Mar 03 '17 at 22:38

0 Answers0