Maven 工具鏈指南
1. 引言
Maven 工具鏈讓我們可以定義和使用特定的工具版本——例如,我們可以使用與 Maven 本身運行的 JDK 不同的 JDK 來建置我們的專案。
在本文中,我們將逐步介紹如何設定 Maven 工具鏈,並探索它提供的一些實用功能。
2. 設定
要使用 Maven 工具鏈,首先需要設定一個toolchains.xml文件,用於定義我們要使用的工具以及它們在建置機器上的位置。在本例中,我們將使用一個特定的 JDK,而不是 Maven 偵測到的預設 JDK,以便在本機開發機器上建置我們的專案。
讓我們在m2目錄下建立toolchains.xml檔:
$ cd ~/.m2/
$ touch toolchains.xml
接下來,讓我們定義用於建置專案的 JDK 版本及其位置:
<?xml version="1.0" encoding="UTF-8"?>
<toolchains>
<toolchain>
<type>jdk</type>
<provides>
<version>24</version>
<vendor>liberica</vendor>
</provides>
<configuration>
<jdkHome>${env.HOME}/.sdkman/candidates/java/24.0.1.fx-librca/</jdkHome>
</configuration>
</toolchain>
</toolchains>
接下來,讓我們在專案的pom.xml檔案中設定建置依賴項:
<build>
<plugins>
<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>24</version>
<vendor>liberica</vendor>
</jdk>
</toolchains>
</configuration>
</plugin>
<!-- ... -->
<plugins>
<build>
Maven Toolchains外掛程式為專案中支援工具鏈的插件設定工具鏈。建置專案時,Maven 會將此要求與本機上可用的工具鏈進行比對。
讓我們測試一下我們的設定:
$ mvn clean compile
從建置日誌中我們可以看到,Maven 偵測到了我們的工具鏈:
[INFO] --- toolchains:1.1:toolchain (default) @ maven-toolchains ---
[INFO] Required toolchain: jdk [ vendor='liberica' version='24' ]
[INFO] Found matching toolchain for type jdk: JDK[/<home-directory>/.sdkman/candidates/java/24.0.1.fx-librca/]
接下來,我們來看看如何將 Maven 工具鏈與其他自訂工具一起使用。
3. 配置自訂工具
讓我們設定 Maven Protocol Buffers 外掛程式來探索這個用例。
在這種情況下,我們希望使用特定版本的 Protocol Buffers 編譯器 (protoc) 來產生 Java 類別。注意: protobuf-maven-plugin已停止維護,此處僅作範例說明。
首先,讓我們在toolchains.xml檔案中新增另一個條目:
<toolchains>
<!-- ... -->
<toolchain>
<type>protobuf</type>
<provides>
<version>3.0.0</version>
</provides>
<configuration>
<protocExecutable>${env.HOME}/DevTools/protoc-3.0.0/bin/protoc</protocExecutable>
</configuration>
</toolchain>
</toolchains>
protocExecutable指向 protoc 二進位檔案的位置。接下來,我們更新專案的pom.xml檔:
<dependencies>
<!-- ... -->
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>3.19.4</version>
</dependency>
</dependencies>
接下來,更新maven-toolchains-plugin並加入 toolchain-aware protobuf-maven-plugin :
<build>
<plugins>
<!-- ... -->
<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>
<!-- ... -->
<protobuf>
<version>3.0.0</version>
</protobuf>
</toolchains>
</configuration>
</plugin>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.6.1</version>
<extensions>true</extensions>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
org.xolstice.maven.plugins:protobuf-maven-plugin是工具鏈感知的,因此它可以使用maven-toolchains-plugin設定的工具鏈。
最後,我們可以定義addressbook.proto檔案並驗證我們的設定:
syntax = "proto3";
option java_package = "com.baeldung";
option java_multiple_files = true;
option java_outer_classname = "AddressBookProtos";
message Address {
string street_address = 1;
string city = 2;
string state = 3;
string postal_code = 4;
}
message Contact {
string first_name = 1;
string last_name = 2;
string email = 3;
string phone_number = 4;
Address address = 5;
}
message AddressBook {
repeated Contact contacts = 1;
}
$ mvn clean compile
Java 原始碼已生成,編譯日誌顯示 Maven 偵測到並使用了該工具鏈:
[INFO] --- toolchains:1.1:toolchain (default) @ maven-toolchains ---
//...
[INFO] Required toolchain: protobuf [ version='3.0.0' ]
[INFO] Found matching toolchain for type protobuf: PROTOC[/<home-directory>/DevTools/protoc-3.0.0/bin/protoc]
[INFO]
[INFO] --- protobuf:0.6.1:compile (default) @ maven-toolchains ---
[INFO] Toolchain in protobuf-maven-plugin: PROTOC[/<home-directory>/DevTools/protoc-3.0.0/bin/protoc]
[INFO] Compiling 1 proto file(s) to /<home-directory>/workspace/baeldung/local/maven-modules/maven-toolchains/target/generated-sources/protobuf/java
4. 結論
在本教程中,我們探索了 Maven 工具鏈,以及如何利用它們為建置定義特定的工具版本,例如使用與 Maven 運行環境不同的 JDK。我們配置了too toolchains.xml文件,使用支援工具鏈的插件更新了 pom.xml 文件,並驗證了 Maven 是否正確檢測並使用了指定的工具鏈,從而確保了跨環境構建的一致性和可重現性。
和往常一樣,程式碼可以在 GitHub 上找到。