0

我正在尝试使用 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 配置覆盖没有从配置文件中应用,但我不明白为什么。

4

1 回答 1

0

我遇到了同样的问题,在我的情况下,我解决了它在 pom 配置中更改版本

<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>
于 2021-04-28T12:24:40.687 回答