构建系统maven

发布于:2025-06-03 ⋅ 阅读:(25) ⋅ 点赞:(0)

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 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>demo-project</artifactId>
  <version>1.0.0</version>
  <packaging>jar</packaging> <!-- 可选:jar / war / pom -->

  <name>Demo Project</name>
  <description>这是一个示例 Maven 项目</description>
  <url>http://www.example.com</url>

  <!-- Java版本控制 -->
  <properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <!-- 依赖 -->
  <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.3.30</version>
    </dependency>
  </dependencies>

  <!-- 插件 -->
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.11.0</version>
        <configuration>
          <source>${maven.compiler.source}</source>
          <target>${maven.compiler.target}</target>
        </configuration>
      </plugin>
    </plugins>
  </build>

</project>

常用配置如下:

区块 用途
<groupId> 项目的组织标识,一般为域名倒写,如:com.company.project
<artifactId> 项目的模块名,是打包后的文件名
<version> 项目的版本号,如 1.0.0-SNAPSHOT
<packaging> 打包类型(如:jarwarpom
<dependencies> 项目的所有依赖库
<build> 构建相关配置(插件、目标目录等)
<repositories> 添加第三方仓库(如果 Maven 中央仓库没有你要的包)
<profiles> 配置多种构建环境(例如 dev/test/prod)
<properties> 定义全局属性,便于统一管理,比如 Java 版本、编码等

依赖:

<dependencies>
  <dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>2.0.9</version>
  </dependency>
</dependencies>
子项 说明
<scope> 依赖作用范围(compile, test, provided, runtime
<optional> 是否为可选依赖
<exclusions> 排除传递性依赖

构建

<build>
  <sourceDirectory>src/main/java</sourceDirectory>
  <resources>
    <resource>
      <directory>src/main/resources</directory>
    </resource>
  </resources>
  <plugins>
    <!-- 编译插件 -->
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>3.11.0</version>
      <configuration>
        <source>1.8</source>
        <target>1.8</target>
      </configuration>
    </plugin>
  </plugins>
</build>
插件名 作用
maven-jar-plugin 生成 JAR 包
maven-surefire-plugin 执行单元测试
maven-assembly-plugin 打包为带依赖的 JAR/ZIP
maven-site-plugin 生成项目文档

构建

<build>
  <sourceDirectory>src/main/java</sourceDirectory>
  <resources>
    <resource>
      <directory>src/main/resources</directory>
    </resource>
  </resources>
  <plugins>
    <!-- 编译插件 -->
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>3.11.0</version>
      <configuration>
        <source>1.8</source>
        <target>1.8</target>
      </configuration>
    </plugin>
  </plugins>
</build>
插件名 作用
maven-jar-plugin 生成 JAR 包
maven-surefire-plugin 执行单元测试
maven-assembly-plugin 打包为带依赖的 JAR/ZIP
maven-site-plugin 生成项目文档

属性

<properties>
  <java.version>17</java.version>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

 仓库配置

<repositories>
  <repository>
    <id>aliyun-central</id>
    <url>https://maven.aliyun.com/repository/central</url>
  </repository>
</repositories>

构建环境

<profiles>
  <profile>
    <id>dev</id>
    <properties>
      <env>development</env>
    </properties>
  </profile>
  <profile>
    <id>prod</id>
    <properties>
      <env>production</env>
    </properties>
  </profile>
</profiles>
mvn clean install -P dev

3 一些实际例子

3.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!

3.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.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>

  <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]

3.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>

4 对比

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

特性/工具 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 超快速构建执行器(需搭配生成器使用)


网站公告

今日签到

点亮在社区的每一天
去签到