Fork me on GitHub

Using Toolchains Instead of Explicit Paths

To keep both, the POM and the PATH free of explicit paths, the exec:exec goal supports Maven Toolchains.

Toolchains are configured using the file toolchains.xml and apply different algorithms how to find the wanted tool on disk.

More information on toolchains can be found on the Maven website.

There are three ways of using toolchains:

  • the “paths” toolchain provided by the Exec Maven Plugin
  • the “jdk” toolchain provided by the Maven Toolchains Plugin,
  • and custom toolchains provided by third party plugins.

The “paths” Toolchain

The “paths” toolchain is included in the Exec Maven Plugin and must be enabled explicitly by <extensions>true</extensions> in the plugin configuration in POM.

It searches a configurable list of folders for the requested tool. The first match will be invoked.

pom.xml:

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-toolchains-plugin</artifactId>
        <version>3.2.0</version>
        <executions>
          <execution>
            <goals>
              <goal>toolchain</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <toolchains>
            <paths>
              <!-- Select the "foo-bar" Paths Toolchain Configuration -->
              <id>foo-bar</id>
            </paths>
          </toolchains>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>3.5.0</version>
        <!-- Enable "paths" Toolchain provided by Exec Maven Plugin -->
        <extensions>true</extensions>
        ...
        <configuration>
          <!-- select paths toolchain -->
          <toolchain>paths</toolchain>
          <executable>meExecutable</executable>
        </configuration>
        ...
      </plugin>
    </plugins>
  </build>
  ...
</project>

toolchains.xml:

<?xml version="1.0" encoding="UTF8"?>
<toolchains>
  <toolchain>
    <type>paths</type>
    <provides>
      <!-- Defines the folders to search for binaries of the "foo-bar" toolset -->
      <id>foo-bar</id>
    </provides>
    <configuration>
      <paths>
        <path>C:\Program Files\FooBar\SDK\bin</path>
        <path>C:\Program Files\FooBar\Runtime\bin</path>
      </paths>
    </configuration>
  </toolchain>
  ...
</toolchains>

In the above example executable meExecutable will be searched in directory: C:\Program Files\FooBar\SDK\bin or C:\Program Files\FooBar\Runtime\bin

The “jdk” Toolchain

The “jdk” toolchain is included in the Maven Toolchains Plugin and enabled by default. It searches the bin folder of the specified JDK for the requested tool.

pom.xml:

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-toolchains-plugin</artifactId>
        <version>3.2.0</version>
        <executions>
          <execution>
            <goals>
              <goal>toolchain</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <toolchains>
            <jdk>
              <!-- Select the JDK to be searched --> 
              <version>[x,)</version>
            </jdk>
          </toolchains>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>3.5.0</version>
        <configuration>
          <executable>java</executable> 
        </configuration>
        ...
      </plugin>
    </plugins>
  </build>
  ...
</project>

toolchains.xml:

<?xml version="1.0" encoding="UTF8"?>
<toolchains>
  <toolchain>
    <type>jdk</type>
    <provides>
      <version>z.y.z</version>
      <vendor>vendor</vendor>
      ...
    </provides>
    <configuration>
      <jdkHome>C:\Program Files\Java\jdk-x.y.z</jdkHome>
    </configuration>
  </toolchain>
  ...
</toolchains>

In the above example executable java will be searched in JDK provided by toolchain.

Custom Toolchains

Custom toolchains are included in third party Maven plugins and must be enabled explicitly in the POM. Their type id, configuration and search algorithms are plugin specific.

See particular plugin's documentation.

pom.xml:

<project>
  ...
  <build>
    <extensions>
      <extension>
        <!-- plugin's GAV coordinates go here -->
      </extension>
    </extensions>
    ...
    <plugins>
      <plugin>
        <artifactId>maven-toolchains-plugin</artifactId>
        <version>3.2.0</version>
        <executions>
          <execution>
            <goals>
              <goal>toolchain</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <toolchains>
            <!-- toolchain-specific selector goes here -->
          </toolchains>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>3.5.0</version>
        ...
        <configuration>
          <toolchain><!-- toolchain's type id goes here --></toolchain>
          <executable>meExecutable</executable>
        </configuration>
        ...
      </plugin>
    </plugins>
  </build>
  ...
</project>

toolchains.xml:

<?xml version="1.0" encoding="UTF8"?>
<toolchains>
  <toolchain>
    <type><!-- toolchain's type id goes here --></type>
    <provides>
      <!-- toolchain-specific provision goes here -->
    </provides>
    <configuration>
      <!-- toolchain-specific configuration goes here -->
    </configuration>
  </toolchain>
  ...
</toolchains>

In the above example executable meExecutable will be searched by provided custom toolchain.