当前位置: 首页 > news >正文

构建系统maven

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

最后列一下这一堆构建工具的对比。

特性/工具MavenGradleMakeCMakeMesonNinja
🔧 主要用于语言Java, KotlinJava, Kotlin, GroovyC/C++、FortranC/C++、CUDAC/C++、Python、Rust 等C/C++(需搭配生成器)
⚙️ 配置方式XMLGroovy/Kotlin DSL手写 MakefileCMakeLists.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 或 GradleJava 生态标准构建工具
Android 开发者GradleAndroid Studio 默认工具,支持 DSL
C/C++ 项目开发者CMake + Ninja广泛支持 IDE,效率高
嵌入式或 Linux 驱动开发者Make简洁、可控,资源占用少
现代 C/C++ 工程、GNOME/GTKMeson + Ninja更快、更现代的构建系统
要最高性能的构建执行Ninja超快速构建执行器(需搭配生成器使用)

相关文章:

  • 实验:基于SpringBoot+MyBatis-Plus实现文章列表增删改查
  • 怎么更改cursor chat中的字体大小
  • 【Oracle】安装单实例
  • 上位机知识篇---网络类型
  • res.json() vs res.send() 的区别
  • Java内存区域与内存溢出异常分析与解决
  • linux命令 systemctl 和 supervisord 区别及用法解读
  • 卷积神经网络(CNN)完全指南:从原理到实战
  • ESP32基础知识1:项目工程建立和烧录
  • 分类预测 | Matlab实现CNN-LSTM-Attention高光谱数据分类
  • 操作系统学习(十)——文件系统
  • Marvin - 生成结构化输出 和 构建AI工作流
  • BLIP-2
  • Gunicorn 配置文件参数详解
  • Git初识Git安装
  • 税法 -2.2- 增值税-允许抵扣的进项税额
  • 6月1日星期日今日早报简报微语报早读
  • CppCon 2014 学习:C++ Memory Model Meets High-Update-Rate Data Structures
  • python学习打卡day42
  • 2024 CKA模拟系统制作 | Step-By-Step | 8、题目搭建-创建 Ingress
  • 广州营销型网站建设团队/万网域名注册
  • 网络教学平台长沙理工大学/seo一个月赚多少钱
  • 外国平面设计网站/域名注册查询软件
  • wordpress代码创建子站点/公司页面设计
  • 外包项目平台/泰安网站推广优化
  • 校园推广大使的职位描述/seo5