exec-maven-plugin: Can I run exec:exec goal without first running the toolchains:toolchain goal?

huangapple go评论72阅读模式
英文:

exec-maven-plugin: Can I run exec:exec goal without first running the toolchains:toolchain goal?

问题

我继承了一个只能在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

这个方法可行,但是文档让我认为工具链会自动配置,而不需要我执行 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

英文:

I've inherited an application that only compiles and runs in Java 1.8. Because I don't want to make Java 1.8 the primary jvm on my machine, I felt that the best way to manage this was through Maven toolchains. Configuring the maven-compiler-plugin was pretty straight forward, but I also want to add the ability to execute the service via Maven in order to take advantage of the toolchain I have configured for 1.8.

The challenge is that I don't seem to be able to get the exec-maven-plugin to use the toolchain as documented. According to the documentation, I would think that the exec-maven-plugin would utilize the maven-toolchains-plugin as needed. However, in order to get exec:exec to use the right toolchain, I have to use:

mvn toolchains:toolchain exec:exec

This works, but the documentation leads me to think that the toolchain would be configured automatically without me needing to execute the toolchains:toolchain goal.

pom.xml

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

toolchains.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF8&quot;?&gt;
&lt;toolchains&gt;
  &lt;toolchain&gt;
    &lt;type&gt;jdk&lt;/type&gt;
    &lt;provides&gt;
      &lt;id&gt;1.8&lt;/id&gt;
      &lt;version&gt;1.8&lt;/version&gt;
    &lt;/provides&gt;
    &lt;configuration&gt;
      &lt;jdkHome&gt;/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home&lt;/jdkHome&gt;
    &lt;/configuration&gt;
  &lt;/toolchain&gt;
&lt;/toolchains&gt;

Additional Note: I also tried configuring the exec-maven-plugin to run on exec:java with the following configuration:

&lt;configuration&gt;
  &lt;mainClass&gt;com.my.Main&lt;/mainClass&gt;
&lt;/configuration&gt;

However this does not work, not even with mvn toolchains:toolchain exec:java.

Is there a way to configure this so that I only have to run mvn exec:exec, or mvn exec:java?

答案1

得分: 0

我认为答案是你必须确保 toolchains 插件本身是你构建过程的一部分。或者,至少是根据相关文档所示。 (我在上面看到你已经有了;我要说的是,是的,这是必需的。)

英文:

I think the answer is you must ensure that the toolchains plugin itself is part of your build. Or that's what the relevant documentation seems to say. (I see above you have that; what I'm saying is yes, that is required.)

huangapple
  • 本文由 发表于 2020年10月1日 00:59:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/64142374.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定