【问题标题】:Ignore maven execution block which uses toolchain if ~/.m2/toolchains.xml is not present如果 ~/.m2/toolchains.xml 不存在,则忽略使用工具链的 maven 执行块
【发布时间】:2022-05-02 08:25:20
【问题描述】:

我正在开发一个必须在 jre7 上正常工作的开源库。由于 java 9 已经发布,我们决定为我们的模块提供 module-info.java 文件,这样如果用户更喜欢在 jre9 上使用带有 sdk 9 的库,他们就可以使用它们(在这种情况下,他们需要手动在 pom.xml 中放入这段代码:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.7.0</version>
    <configuration>
      <source>9</source>
      <target>9</target>
    </configuration>
  </plugin>

我尝试实现 maven (https://maven.apache.org/plugins/maven-compiler-plugin/examples/module-info.html) 提供的解决方案,用 jdk7 编译整个代码,并用 jdk9 只编译 module-info.java 文件。这是一段pom.xml

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.7.0</version>
    <executions>
      <execution>
        <id>default-compile</id>
        <configuration>
          <!-- compile everything to ensure module-info contains right entries -->
          <!-- required when JAVA_HOME is JDK 8 or below -->
          <jdkToolchain>
            <version>9</version>
          </jdkToolchain>
          <release>9</release>
        </configuration>
      </execution>
      <execution>
        <id>base-compile</id>
        <goals>
          <goal>compile</goal>
        </goals>
        <!-- recompile everything for target VM except the module-info.java -->
        <configuration>
          <excludes>
            <exclude>module-info.java</exclude>
          </excludes>
        </configuration>
      </execution>
    </executions>
    <!-- defaults for compile and testCompile -->
    <configuration>
      <!-- jdkToolchain required when JAVA_HOME is JDK 9 or above -->
      <jdkToolchain>
        <version>[1.7,9)</version>
      </jdkToolchain>
      <source>1.7</source>
      <target>1.7</target>
    </configuration>
  </plugin>

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-toolchains-plugin</artifactId>
    <version>1.1</version>
    <executions>
      <execution>
        <goals>
          <goal>toolchain</goal>
        </goals>
      </execution>
    </executions>
    <configuration>
      <toolchains>
        <jdk>
          <version>9</version>
        </jdk>
      </toolchains>
    </configuration>
  </plugin>

而且我已经正确配置了 ~/.m2/toolchains.xml 文件。它工作得很好。但问题是我们需要跳过第一个执行块,其中所有内容都使用 jdk9 和 module-info 编译,因为我们不想让我们的用户创建 toolchains.xml 文件并设置 java 9。所以我可能在看如果 ~/.m2 目录中不存在 toolchains.xml 并且只执行第二个执行块,则 for 是跳过以下代码的解决方法,其中 module-info.java 文件被忽略。这甚至可能吗?我意识到我们仍然可以保留两个分支(一个是私有的,有两个执行块并使用工具链,另一个是公共的,它完全忽略 module-info.java 文件,但这似乎不是一个优雅的解决方案。

<execution>
    <id>default-compile</id>
    <configuration>
      <!-- compile everything to ensure module-info contains right entries -->
      <!-- required when JAVA_HOME is JDK 8 or below -->
      <jdkToolchain>
        <version>9</version>
      </jdkToolchain>
      <release>9</release>
    </configuration>
  </execution>

【问题讨论】:

    标签: java maven maven-toolchains


    【解决方案1】:
    1. 您可以使用 Maven 配置文件激活/停用 pom 的某些部分。
    2. 提供库的两个不同版本不是更好吗?版本号为 1.0.0-JDK7 和 1.0.0-JDK9。然后,您图书馆的用户可以根据自己的目的选择合适的图书馆。

    【讨论】:

      猜你喜欢
      • 2021-06-28
      • 1970-01-01
      • 2017-02-15
      • 2012-03-28
      • 1970-01-01
      • 1970-01-01
      • 2022-01-18
      • 2013-07-15
      • 1970-01-01
      相关资源
      最近更新 更多