0

I am trying to create a multi-release jar with maven and it's compiler plugin. I would like to control which JDKs are targeted through the use of profiles. I created two profiles, one for jdk8 and the other jdk11, as well as a build plugin definition with common options.

<profiles>
  <profile>
    <id>target-jdk8</id>
    <activation>
      <jdk>[1.8,)</jdk>
    </activation>

    <build>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>

          <executions>
            <execution>
              <id>compile-jdk8</id>
              <phase>compile</phase>
              <goals>
                <goal>compile</goal>
              </goals>

              <configuration combine.self="append">
                <source>1.8</source>
                <target>1.8</target>

                <excludes>
                  <exclude>module-info.java</exclude>
                </excludes>
              </configuration>
            </execution>
          </executions>
        </plugin>
      </plugins>
    </build>
  </profile>

  <profile>
    <id>target-jdk11</id>
    <activation>
      <jdk>[11,)</jdk>
    </activation>

    <build>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>

          <executions>
            <execution>
              <id>compile-jdk11</id>
              <goals>
                <goal>compile</goal>
              </goals>

              <configuration combine.self="append">
                <multiReleaseOutput>true</multiReleaseOutput>
                <release>11</release>
              </configuration>
            </execution>
          </executions>
        </plugin>
      </plugins>
    </build>
  </profile>
</profiles>


<build>
  <plugins>
    ...
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>3.8.1</version>

      <configuration>
        <verbose>true</verbose>
        <fork>true</fork>

        <compileSourceRoots>
          <compileSourceRoot>${basedir}/src/main/java</compileSourceRoot>
        </compileSourceRoots>

        <compilerArgs>
          <arg>-h</arg>
          <arg>${project.build.directory}/include</arg>
        </compilerArgs>
      </configuration>

      <executions>
        <execution>
          <id>compile-java</id>
          <phase>none</phase>
          <goals>
            <goal>compile</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
    ...
  </plugins>
</build>

The issue I am having is when running mvn package -P \!target-jdk8,target-jdk11 I am getting an error: modules are not supported in -source 6 (use -source 9 or higher to enable modules). How did we get -source 6? Am I not building for JDK 11 in this scenario? Clearly my configuration override for the maven-compiler-plugin is not being applied from the profile, but I am not understanding why.

Alex Barker
  • 4,316
  • 4
  • 28
  • 47

1 Answers1

0

I was having the same issue, in my case I solved it changing the version in pom configuration

<build>
    <sourceDirectory>src/main/java</sourceDirectory>
    <testSourceDirectory>src/test/java</testSourceDirectory>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.3</version>
            <configuration>
                <source>11</source>
                <target>11</target>
            </configuration>
        </plugin>
    </plugins>
    <finalName>${project.artifactId}</finalName>
</build>