我正在尝试使用 maven 创建一个多版本 jar,它是编译器插件。我想通过使用配置文件来控制目标 JDK。我创建了两个配置文件,一个用于 jdk8,另一个用于 jdk11,以及一个带有常用选项的构建插件定义。
<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>
我遇到的问题是运行时mvn package -P \!target-jdk8,target-jdk11
我得到一个error: modules are not supported in -source 6 (use -source 9 or higher to enable modules)
. 我们是如何得到 -source 6 的?在这种情况下,我不是为 JDK 11 构建的吗?显然,我的 maven-compiler-plugin 配置覆盖没有从配置文件中应用,但我不明白为什么。