Maven profiles activates on running jdk not on toolchains jdk

818 Views Asked by At

I'm attempting to create a pom that will:

I've included the attempted pom.xml to achieve this.

Testing this I'm using mvn clean help:active-profiles which for each shows the following:

JAVA_HOME=/path/to/jdk/1.8:

The following profiles are active:

 - telstra-ae-sda (source: external)
 - jdk-8 (source: com.example:profile-test:1.0.0-SNAPSHOT)

JAVA_HOME=/path/to/jdk/14:

The following profiles are active:

 - telstra-ae-sda (source: external)
 - jdk-14 (source: com.example:profile-test:1.0.0-SNAPSHOT)

I would have thought that profiles would pull from the compiling JDK, not the running JDK. Is this expected behaviour, and; anyone know a workaround?

The following pom.xml is used to achieve this:

<?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.example</groupId>
  <artifactId>profile-test</artifactId>
  <version>1.0.0-SNAPSHOT</version>
  <packaging>pom</packaging>

  <properties>
    <spring-boot.version>2.3.0.RELEASE</spring-boot.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>14</java.version>
    <!--java.version>1.8</java.version-->
  </properties>

  <build>
    <plugins>
      <plugin>
        <artifactId>maven-toolchains-plugin</artifactId>
        <executions>
          <execution>
            <goals>
              <goal>toolchain</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <toolchains>
            <jdk>
              <version>${java.version}</version>
            </jdk>
          </toolchains>
        </configuration>
      </plugin>
    </plugins>
  </build>

  <profiles>
    <profile>
      <id>jdk-8</id>
      <activation>
        <jdk>1.8</jdk>
      </activation>
      <build>
        <plugins>
          <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
              <source>${java.version}</source>
              <target>${java.version}</target>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile>
    <profile>
      <id>jdk-14</id>
      <activation>
        <jdk>14</jdk>
      </activation>
      <build>
        <plugins>
          <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
              <release>${java.version}</release>
              <compilerArgs>
                <arg>--enable-preview</arg>
              </compilerArgs>
              <forceJavacCompilerUse>true</forceJavacCompilerUse>
              <parameters>true</parameters>
            </configuration>
          </plugin>
          <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
              <argLine>--enable-preview</argLine>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>

</project>
1

There are 1 best solutions below

0
On

Figured I'd just move the java.version property to the profile itself and leave jdk-8 profile active by default.

This approach does fit the workflow better anyway as testing JDK 14 is not the normal pattern and best activating manually through -P jdk-14 rather than updating the java.varsion property.