Maven 是一个强大的项目管理和构建自动化工具,主要用于 Java 项目,但其架构也可用于其他语言项目。它解决了以下核心问题:
特性 | Maven | Gradle | Ant |
---|---|---|---|
配置文件 | XML(pom.xml) | Groovy/Kotlin | XML |
依赖管理 | 原生支持 | 支持 | 需Ivy扩展 |
学习曲线 | 中等 | 较陡 | 简单 |
灵活性 | 结构化 | 高灵活 | 高灵活 |
性能 | 中等 | 高 | 中等 |
所有Maven项目的核心配置文件pom.xml
,包含:
每个项目/依赖都由以下三个属性唯一标识:
<groupId>com.examplegroupId>
<artifactId>my-projectartifactId>
<version>1.0.0version>
Maven自动下载并管理依赖:
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
<scope>testscope>
dependency>
dependencies>
~/.m2/repository
(你电脑上的缓存)Maven有三种内建生命周期:
每个生命周期包含多个阶段(phase),执行命令时按顺序执行之前所有阶段。
/opt/maven
)# Linux/macOS
export MAVEN_HOME=/opt/maven
export PATH=$PATH:$MAVEN_HOME/bin
# Windows
系统环境变量添加:
MAVEN_HOME = C:\Program Files\maven
Path 添加 %MAVEN_HOME%\bin
mvn -v
编辑conf/settings.xml
:
<localRepository>/path/to/your/local/repolocalRepository>
<mirrors>
<mirror>
<id>aliyun-mavenid>
<name>Aliyun Maven Mirrorname>
<url>https://maven.aliyun.com/repository/publicurl>
<mirrorOf>centralmirrorOf>
mirror>
mirrors>
mvn archetype:generate \
-DgroupId=com.example \
-DartifactId=my-app \
-DarchetypeArtifactId=maven-archetype-quickstart \
-DinteractiveMode=false
my-app/
├── pom.xml
├── src/
│ ├── main/
│ │ ├── java/
│ │ ├── resources/
│ │ └── webapp/
│ └── test/
│ ├── java/
│ └── resources/
└── target/
# 编译项目
mvn compile
# 清除目标目录
mvn clean
# 运行单元测试
mvn test
# 打包项目(JAR/WAR)
mvn package
# 安装到本地仓库
mvn install
# 组合命令:先清理再安装
mvn clean install
# 跳过测试
mvn package -DskipTests
# 指定环境(profile)
mvn package -P production
# 查看依赖树
mvn dependency:tree
# 更新快照依赖
mvn versions:use-latest-versions
<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.0modelVersion>
<groupId>com.examplegroupId>
<artifactId>my-appartifactId>
<version>1.0.0version>
<packaging>jarpackaging>
<name>My Demo Applicationname>
<description>A sample Maven projectdescription>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<java.version>1.8java.version>
<junit.version>5.7.0junit.version>
properties>
<dependencies>
<dependency>
<groupId>org.junit.jupitergroupId>
<artifactId>junit-jupiter-apiartifactId>
<version>${junit.version}version>
<scope>testscope>
dependency>
<dependency>
<groupId>ch.qos.logbackgroupId>
<artifactId>logback-classicartifactId>
<version>1.2.3version>
dependency>
<dependency>
<groupId>com.google.guavagroupId>
<artifactId>guavaartifactId>
<version>30.1.1-jreversion>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-compiler-pluginartifactId>
<version>3.8.1version>
<configuration>
<source>${java.version}source>
<target>${java.version}target>
configuration>
plugin>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-jar-pluginartifactId>
<version>3.2.0version>
<configuration>
<archive>
<manifest>
<addClasspath>trueaddClasspath>
<mainClass>com.example.AppmainClass>
manifest>
archive>
configuration>
plugin>
<plugin>
<groupId>org.jacocogroupId>
<artifactId>jacoco-maven-pluginartifactId>
<version>0.8.7version>
<executions>
<execution>
<goals>
<goal>prepare-agentgoal>
goals>
execution>
<execution>
<id>reportid>
<phase>testphase>
<goals>
<goal>reportgoal>
goals>
execution>
executions>
plugin>
plugins>
build>
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-javadoc-pluginartifactId>
<version>3.3.0version>
plugin>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-project-info-reports-pluginartifactId>
<version>3.1.2version>
plugin>
plugins>
reporting>
project>
父pom.xml:
<project>
<modelVersion>4.0.0modelVersion>
<groupId>com.examplegroupId>
<artifactId>parent-projectartifactId>
<version>1.0.0version>
<packaging>pompackaging>
<modules>
<module>core-modulemodule>
<module>web-modulemodule>
<module>api-modulemodule>
modules>
project>
Scope | 说明 | 示例 |
---|---|---|
compile | 默认范围,全周期有效 | spring-core |
provided | 容器提供,运行时不需要 | servlet-api |
runtime | 运行时需要 | JDBC驱动 |
test | 仅测试阶段有效 | JUnit |
system | 系统路径依赖 | 本地jar文件 |
<profiles>
<profile>
<id>developmentid>
<properties>
<env>devenv>
properties>
<activation>
<activeByDefault>trueactiveByDefault>
activation>
profile>
<profile>
<id>productionid>
<properties>
<env>prodenv>
properties>
profile>
profiles>
版本管理策略
1.0.0
, 2.1.3
1.0.0-SNAPSHOT
依赖管理优化
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-dependenciesartifactId>
<version>2.5.4version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
CI/CD集成
# .gitlab-ci.yml 示例
stages:
- build
- test
- deploy
maven-build:
stage: build
script:
- mvn clean package
maven-test:
stage: test
script:
- mvn test
官方文档:
教程推荐:
工具与资源:
mvn dependency:tree -Dverbose
// src/main/java/com/example/App.java
package com.example;
public class App {
public static void main(String[] args) {
System.out.println("Hello Maven World!");
}
}
构建并运行:
mvn clean package
java -jar target/my-app-1.0.0.jar
<build>
<resources>
<resource>
<directory>src/main/resourcesdirectory>
<filtering>truefiltering>
resource>
resources>
build>
配置文件:
# src/main/resources/application.properties
app.env=${env}
激活生产配置:
mvn package -P production
通过本指南,你已掌握Maven的核心概念和使用方法。建议按照以下路径实践:
Maven作为Java生态系统的基础设施工具,掌握它将大大提高你的开发效率和项目标准化程度。