1 前言
說真的,我是真的不想看構建了,因為真的太多了。又多又亂。Maven、Gradle、Make、CMake、Meson、Ninja,Android BP。。。感覺學不完,根本學不完。。。
但是沒辦法最近又要用一下Maven,所以咬著牙再簡單整理一下。
安裝:
sudo apt update
sudo apt install maven
mvn -v
功能:
功能 | 說明 |
---|---|
📦 依賴管理 | 自動下載并管理第三方 JAR 包 |
🔨 項目構建 | 編譯 Java、打包成 JAR/WAR 等 |
📁 項目結構標準化 | 統一目錄結構,便于協作 |
🔁 生命周期管理 | 統一管理編譯、測試、打包、部署流程 |
📜 插件系統 | 可通過插件擴展(如編譯器、測試、部署) |
核心就是POM(Project Object Model),一個XML配置文件。然后maven工具讀取這個xml,根據上面的配置進行處理。
maven有三個生命周期
clean生命周期: clean
? ? ? ? ? ?|
default生命周期: validate → compile → test → package → verify → install → deploy
? ? ? ? ? ?|
site生命周期:site→ site-deploy 生成文檔部署文檔
所以典型命令是:
mvn clean package
這個命令就是首先清理之前的構建,再進行新的打包。
三個周期中最重要的是default周期,命令如下:
階段(Phase) | 作用 |
---|---|
compile | 編譯源碼 |
test | 編譯并運行單元測試 |
package | 打包(生成 JAR/WAR) |
install | 安裝到本地倉庫 |
deploy | 部署到遠程倉庫 |
2 一些實際例子
2.1 Hello
代碼結構:
helloworld/
├── pom.xml
└── src/
? ? └── main/
? ? ? ? └── java/
? ? ? ? ? ? └── com/example/
? ? ? ? ? ? ? ? └── App.java
pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.example</groupId><artifactId>helloworld</artifactId><version>1.0-SNAPSHOT</version><packaging>jar</packaging><name>HelloWorld</name><build><plugins><!-- 編譯和運行入口 --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.11.0</version><configuration><source>17</source><target>17</target></configuration></plugin></plugins></build>
</project>
App.java
package com.example;public class App {public static void main(String[] args) {System.out.println("Hello, Maven!");}
}
編譯打包命令是mvn package
從log可以看出,maven自動下載的內容真的非常多。
ubuntu@VM-8-10-ubuntu:~/java$ mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< com.example:helloworld >-----------------------
[INFO] Building HelloWorld 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-resources-plugin/2.6/maven-resources-plugin-2.6.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-resources-plugin/2.6/maven-resources-plugin-2.6.pom (8.1 kB at 2.7 kB/s)...Downloading from central: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/3.5.0/plexus-utils-3.5.0.jar
Downloaded from central: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-compiler-api/2.13.0/plexus-compiler-api-2.13.0.jar (27 kB at 4.9 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-compiler-manager/2.13.0/plexus-compiler-manager-2.13.0.jar
Downloaded from central: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-compiler-manager/2.13.0/plexus-compiler-manager-2.13.0.jar (4.7 kB at 735 B/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-compiler-javac/2.13.0/plexus-compiler-javac-2.13.0.jar
Downloaded from central: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-compiler-javac/2.13.0/plexus-compiler-javac-2.13.0.jar (23 kB at 2.4 kB/s)
Downloaded from central: https://repo.maven.apache.org/maven2/commons-io/commons-io/2.6/commons-io-2.6.jar (215 kB at 21 kB/s)
Downloaded from central: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/3.5.0/plexus-utils-3.5.0.jar (267 kB at 16 kB/s)
Downloaded from central: https://repo.maven.apache.org/maven2/org/ow2/asm/asm/9.4/asm-9.4.jar (122 kB at 6.3 kB/s)
Downloaded from central: https://repo.maven.apache.org/maven2/com/thoughtworks/qdox/qdox/2.0.3/qdox-2.0.3.jar (334 kB at 13 kB/s)
[INFO] Changes detected - recompiling the module! :source
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file with javac [debug target 17] to target/classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ helloworld ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/ubuntu/java/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.11.0:testCompile (default-testCompile) @ helloworld ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ helloworld ---
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/maven-plugin-api/2.0.9/maven-plugin-api-2.0.9.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/maven-plugin-api/2.0.9/maven-plugin-api-2.0.9.pom (1.5 kB at 3.9 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/maven/2.0.9/maven-2.0.9.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/maven/2.0.9/maven-2.0.9.pom (19 kB at 14 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/maven-parent/8/maven-parent-8.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/maven-parent/8/maven-parent-8.pom (24 kB at 9.8 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/apache/4/apache-4.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/apache/4/apache-4.pom (4.5 kB at 1.7 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-booter/2.12.4/surefire-booter-2.12.4.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-booter/2.12.4/surefire-booter-2.12.4.pom (3.0 kB at 2.4 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-api/2.12.4/surefire-api-2.12.4.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-api/2.12.4/surefire-api-2.12.4.pom (2.5 kB at 2.8 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/maven-surefire-common/2.12.4/maven-surefire-common-2.12.4.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/maven-surefire-common/2.12.4/maven-surefire-common-2.12.4.pom (5.5 kB at 4.8 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugin-tools/maven-plugin-annotations/3.1/maven-plugin-annotations-3.1.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugin-tools/maven-plugin-annotations/3.1/maven-plugin-annotations-3.1.pom (1.6 kB at 2.1 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugin-tools/maven-plugin-tools/3.1/maven-plugin-tools-3.1.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugin-tools/maven-plugin-tools/3.1/maven-plugin-tools-3.1.pom (16 kB at 8.4 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/maven-artifact/2.0.9/maven-artifact-2.0.9.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/maven-artifact/2.0.9/maven-artifact-2.0.9.pom (1.6 kB at 4.3 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/3.0.8/plexus-utils-3.0.8.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/3.0.8/plexus-utils-3.0.8.pom (3.1 kB at 4.3 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus/3.2/plexus-3.2.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus/3.2/plexus-3.2.pom (19 kB at 16 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/sonatype/spice/spice-parent/17/spice-parent-17.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/sonatype/spice/spice-parent/17/spice-parent-17.pom (6.8 kB at 11 kB/s)...Downloaded from central: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-archiver/2.1/plexus-archiver-2.1.jar (184 kB at 19 kB/s)
Downloaded from central: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/3.0/plexus-utils-3.0.jar (226 kB at 15 kB/s)
Downloaded from central: https://repo.maven.apache.org/maven2/commons-lang/commons-lang/2.1/commons-lang-2.1.jar (208 kB at 9.8 kB/s)
[INFO] Building jar: /home/ubuntu/java/target/helloworld-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 04:17 min
[INFO] Finished at: 2025-06-01T22:14:24+08:00
[INFO] ------------------------------------------------------------------------
編譯完成之后直接用java運行即可。
ubuntu@VM-8-10-ubuntu:~/java$ java -cp target/helloworld-1.0-SNAPSHOT.jar com.example.App
Hello, Maven!
2.2?添加第三方依賴(如 Gson)
在pom.xml中增加,并且指示編譯成fat jar
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.example</groupId><artifactId>helloworld</artifactId><version>1.0-SNAPSHOT</version><packaging>jar</packaging><name>HelloWorld</name><dependencies><dependency><groupId>com.google.code.gson</groupId><artifactId>gson</artifactId><version>2.10.1</version></dependency></dependencies><build><plugins><!-- Java 編譯器插件 --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.11.0</version><configuration><source>17</source><target>17</target></configuration></plugin><!-- 打包為 fat jar --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-shade-plugin</artifactId><version>3.5.0</version><executions><execution><phase>package</phase><goals><goal>shade</goal></goals><configuration><transformers><transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"><mainClass>com.example.App</mainClass></transformer></transformers></configuration></execution></executions></plugin></plugins></build>
</project>
修改代碼App.java
package com.example;
import com.google.gson.Gson;public class App {public static void main(String[] args) {Gson gson = new Gson();String json = gson.toJson(new int[]{1, 2, 3});System.out.println(json); // 輸出:[1,2,3]}
}
之后編譯時會自動下載新的依賴。
buntu@VM-8-10-ubuntu:~/java$ mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< com.example:helloworld >-----------------------
[INFO] Building HelloWorld 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
Downloading from central: https://repo.maven.apache.org/maven2/com/google/code/gson/gson/2.10.1/gson-2.10.1.pom
Downloaded from central: https://repo.maven.apache.org/maven2/com/google/code/gson/gson/2.10.1/gson-2.10.1.pom (9.4 kB at 1.0 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/com/google/code/gson/gson-parent/2.10.1/gson-parent-2.10.1.pom
Downloaded from central: https://repo.maven.apache.org/maven2/com/google/code/gson/gson-parent/2.10.1/gson-parent-2.10.1.pom (13 kB at 5.2 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/com/google/code/gson/gson/2.10.1/gson-2.10.1.jar
Downloaded from central: https://repo.maven.apache.org/maven2/com/google/code/gson/gson/2.10.1/gson-2.10.1.jar (283 kB at 10.0 kB/s)
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ helloworld ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/ubuntu/java/src/main/resources
運行:
ubuntu@VM-8-10-ubuntu:~/java$ java -cp target/helloworld-1.0-SNAPSHOT.jar com.example.App
[1,2,3]
2.3 多POM
項目結構:
myproject/
├── pom.xml ?<-- 父項目
├── module-a/
│ ? └── pom.xml
└── module-b/
? ? └── pom.xml
父pom.xml
<modules><module>module-a</module><module>module-b</module>
</modules>
3 對比
最后列一下這一堆構建工具的對比。
特性/工具 | Maven | Gradle | Make | CMake | Meson | Ninja |
---|---|---|---|---|---|---|
🔧 主要用于語言 | Java, Kotlin | Java, Kotlin, Groovy | C/C++、Fortran | C/C++、CUDA | C/C++、Python、Rust 等 | C/C++(需搭配生成器) |
?? 配置方式 | XML | Groovy/Kotlin DSL | 手寫 Makefile | CMakeLists.txt(專有語法) | meson.build(Python-like) | .ninja (自動生成) |
📦 依賴管理 | ? 內置依賴管理(Maven Central) | ? 更靈活支持 Maven/Gradle 倉庫 | ? 無內置 | ? 無,依賴手動或外部工具 | 🔄 可接入 wrapdb、pkg-config | ? 無 |
🚀 性能 | 較慢(XML解析、順序執行) | 快(增量構建、多線程) | 慢(無并行、無依賴緩存) | 中等(生成效率好,構建慢) | 快(配合 Ninja 后端) | 極快(專注高效執行) |
🔁 增量構建 | 支持(但較粗) | ? 高級支持(內建緩存) | ? 無內建 | ? 無內建 | ? 支持 | ? 由生成器控制 |
🧱 多模塊支持 | ? 優秀 | ? 優秀 | ? 需手動 | ? 通過 add_subdirectory | ? 支持 | ? 不支持 |
🔌 插件/擴展 | ? 豐富插件系統 | ? 插件和自定義 Task 多 | ? 無插件 | ? 一些模塊 | 少量內建模塊 | ? 無插件系統 |
📚 文檔與社區 | 📘 成熟,企業多 | 📘 成熟,現代項目多 | 📙 歷史悠久 | 📘 主流開源工具使用 | 📘 新興,GN、Gnome 使用 | 📙 少,偏底層 |
📈 學習曲線 | 中(XML配置略繁瑣) | 中高(DSL強大但復雜) | 低(語法簡單) | 中(語法非標準) | 低-中(簡潔清晰) | 低(但手動寫復雜) |
🧩 常用場景 | Java EE、Spring Boot 項目 | Android、Kotlin、Java 項目 | Linux 內核、小項目 | Qt、Vulkan、LLVM 項目 | GNOME、系統包項目 | Chromium、LLVM 構建后端 |
選用指南
你是… | 推薦工具 | 理由 |
---|---|---|
Java 開發者 | Maven 或 Gradle | Java 生態標準構建工具 |
Android 開發者 | Gradle | Android Studio 默認工具,支持 DSL |
C/C++ 項目開發者 | CMake + Ninja | 廣泛支持 IDE,效率高 |
嵌入式或 Linux 驅動開發者 | Make | 簡潔、可控,資源占用少 |
現代 C/C++ 工程、GNOME/GTK | Meson + Ninja | 更快、更現代的構建系統 |
要最高性能的構建執行 | Ninja | 超快速構建執行器(需搭配生成器使用) |