javamavenmaven-exec-pluginmaven-toolchains-plugin

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


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

<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>

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

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

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?


Solution

  • 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.)