exec-maven-plugin:我可以先运行exec:exec目标而无需先运行toolchains:toolchain目标吗?

问题描述

我继承了一个仅在Java 1.8中编译并运行的应用程序。因为我不想使Java 1.8成为我机器上的主要jvm,所以我认为最好的管理方法是通过Maven工具链。配置maven-compiler-plugin非常简单,但是我也想添加通过Maven执行服务的功能,以便利用我为1.8配置的工具链。

挑战在于,我似乎无法获得exec-maven-plugin来使用所记录的工具链。根据文档,我认为exec-maven-plugin将根据需要利用maven-toolchains-plugin。但是,为了使exec:exec使用正确的工具链,我必须使用:

mvn toolchains:toolchain exec:exec

这可行,但是documentation导致我认为工具链将自动配置,而无需执行toolchains:toolchain目标。

pom.xml

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-toolchains-plugin</artifactId>
      <executions>
        <execution>
          <goals>
            <goal>toolchain</goal>
          </goals>
        </execution>
      </executions>
      <configuration>
        <toolchains>
          <jdk>
            <version>1.8</version>
          </jdk>
        </toolchains>
      </configuration>
    </plugin>
        
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>exec-maven-plugin</artifactId>
      <configuration>
        <executable>java</executable>
        <arguments>
          <argument>-classpath</argument>
          <classpath></classpath>
          <argument>com.my.Main</argument>
        </arguments>
      </configuration>
    </plugin>
  </plugins>
</build>

toolchains.xml

<?xml version="1.0" encoding="UTF8"?>
<toolchains>
  <toolchain>
    <type>jdk</type>
    <provides>
      <id>1.8</id>
      <version>1.8</version>
    </provides>
    <configuration>
      <jdkHome>/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home</jdkHome>
    </configuration>
  </toolchain>
</toolchains>

其他说明:我还尝试使用以下配置将exec-maven-plugin配置为在exec:java上运行:

<configuration>
  <mainClass>com.my.Main</mainClass>
</configuration>

但是,即使使用mvn toolchains:toolchain exec:java,这也不起作用。

是否可以配置它,这样我只需要运行mvn exec:execmvn exec:java

解决方法

我认为答案是您必须确保toolchains插件本身是构建的一部分。或就是the relevant documentation seems to say。 (我在上面看到了您的意思;我的意思是,这是必需的。)