Use of Apache Maven ToolChains
brief introduction
Introduction to Toolchains
Example of Toolchains
Toolchains support
summary
brief introduction
Maven is a very useful and commonly used building tool in java. Basically, Maven and gradle dominate large java projects.
Because the JDK version is now developing rapidly at the rate of half a year. Different JDK versions have different java paths. In the process of using Maven, we may often need to switch the JDK versions.
Generally speaking, we can configure the executable path in Maven compiler plugin. As shown below:
For more information, please visit www.flydean.com
<build> <plugins> <!-- target Java 14 --> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <!-- fork compilation and use the specified executable --> <fork>true</fork> <executable>/usr/bin/javac14</executable> </configuration> </plugin> </plugins> </build>
It looks good, but it may be troublesome to switch the path of executable. What's more, if you are sent by a team, one in the mac environment and the other in the windows environment, the executable paths on both sides are completely different, which will lead to code conflicts and code maintenance difficulties.
Introduction to Toolchains
To solve this problem, Maven launched Toolchains for us. Using Toolchains, we can define the path, version number and type of these executable files in a toolchains.xml file.
In the pom.xml file, you only need to refer to the alias defined in toolchains.xml.
In view of the inconsistency between the windows and linux paths above, we can ensure that pom.xml is completely consistent. You only need to adapt your own toolchains.xml file.
Example of Toolchains
Toolchains are used in combination with other plugins in pom, such as the most commonly used Maven compiler plugin.
Let's take an example to illustrate. First, define the toolchains.xml file, which is best placed in ${user.home}/.m2 /.
<?xml version="1.0" encoding="UTF8"?> <toolchains> <!-- JDK toolchains --> <toolchain> <type>jdk</type> <provides> <version>14</version> <vendor>oracle</vendor> </provides> <configuration> <jdkHome>/path/to/jdk/14</jdkHome> </configuration> </toolchain> <toolchain> <type>jdk</type> <provides> <version>11</version> <vendor>oracle</vendor> </provides> <configuration> <jdkHome>/path/to/jdk/11</jdkHome> </configuration> </toolchain> </toolchains>
In the above example, we defined two JDK toolchains. One JDK14 and one JDK11. Let's see how to use it in the pom file.
<plugins> ... <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> </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>14</version> <vendor>oracle</vendor> </jdk> </toolchains> </configuration> </plugin> ... </plugins>
In the pom configuration file above, we can seamlessly switch JDK versions by simply referring to the definitions in toolchains.
Toolchains support
Toolchains requires Maven version 2.0.9 or above.
Toolchains need to be used with plugins in pom. The following figure lists the names of plugins supported by toolchains and the minimum version requirements.
summary
This article introduces the use of toolchain in Apache Maven. I hope you can use it in practical work.
--------