Maven 概述
什么是 Maven
Maven 是一个基于项目对象模型(POM)的项目管理工具,主要用于 Java 项目的构建、依赖管理和项目管理。
- 核心功能
- 项目构建:编译、测试、打包、部署
- 依赖管理:自动下载和管理第三方库
- 项目信息管理:生成项目文档、站点
- 主要优势
- 约定优于配置:标准化的项目结构
- 依赖传递:自动处理依赖关系
- 插件体系:丰富的扩展能力
- 多模块支持:大型项目模块化开发
Maven 的核心概念
项目对象模型(POM)
POM(Project Object Model)是 Maven 项目的核心配置文件,描述了项目的基本信息、依赖、构建配置等。
1 2 3 4 5 6 7 8
| <project> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>my-project</artifactId> <version>1.0.0</version> <packaging>jar</packaging> </project>
|
坐标系统
Maven 使用三个坐标唯一标识一个项目:
- groupId:组织或项目的唯一标识,通常采用域名反写
- artifactId:项目名称或模块名称
- version:项目版本号
示例:com.google.guava:guava:31.1-jre
仓库类型
- 本地仓库(Local Repository)
- 位于用户目录下的
.m2/repository
- 存储从远程仓库下载的依赖
- 中央仓库(Central Repository)
- Maven 官方维护的公共仓库
- 包含绝大多数开源项目
- 远程仓库(Remote Repository)
- 公司或组织私有的仓库
- 如 Nexus、Artifactory
Maven 的工作原理
构建生命周期
Maven 有三个内置的构建生命周期:
- clean:清理项目
pre-clean:执行清理前的工作
clean:删除之前构建生成的文件
post-clean:执行清理后的工作
- default(build):构建项目
- 包含 compile、test、package、install、deploy 等阶段
- site:生成项目站点
构建阶段与插件目标
每个生命周期由多个阶段组成,每个阶段绑定到插件的目标(goal)。
1 2 3 4 5 6 7 8 9 10 11
| 典型构建流程: validate → compile → test → package → verify → install → deploy
常用阶段说明: - validate:验证项目是否正确 - compile:编译源代码 - test:运行单元测试 - package:打包(jar/war) - verify:运行集成测试 - install:安装到本地仓库 - deploy:部署到远程仓库
|
依赖解析机制
1 2 3 4 5 6 7 8 9
| 依赖查找顺序: 1. 本地仓库 2. 远程仓库(按配置顺序) 3. 中央仓库
依赖传递规则: - 最短路径优先原则 - 最先声明优先原则 - 可选依赖不会被传递
|
安装配置
安装 Maven
环境要求
- JDK 1.7+(Maven 3.3+ 需要 JDK 1.7+)
- 操作系统:Windows、Linux、macOS
Windows 安装
下载地址:https://maven.apache.org/download.cgi
步骤:
- 下载压缩包并解压到指定目录(如
D:\apache-maven-3.9.6)
- 配置环境变量:
1 2 3 4 5
| MAVEN_HOME=D:\apache-maven-3.9.6
%MAVEN_HOME%\bin
|
- 验证安装:
Linux/macOS 安装
1 2 3 4 5 6 7 8 9 10 11 12
|
sudo apt-get install maven
sudo yum install maven
brew install maven
mvn -version
|
手动安装(通用)
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| wget https://dlcdn.apache.org/maven/maven-3/3.9.6/binaries/apache-maven-3.9.6-bin.tar.gz
tar -xzf apache-maven-3.9.6-bin.tar.gz sudo mv apache-maven-3.9.6 /opt/maven
echo 'export MAVEN_HOME=/opt/maven' >> ~/.bashrc echo 'export PATH=$MAVEN_HOME/bin:$PATH' >> ~/.bashrc source ~/.bashrc
mvn -version
|
初始配置
Maven 的配置文件位于 ${MAVEN_HOME}/conf/settings.xml,用户级别的配置位于 ~/.m2/settings.xml。
配置本地仓库
1 2 3 4 5
| <settings> <localRepository>D:/maven-repository</localRepository> </settings>
|
配置镜像加速
1 2 3 4 5 6 7 8 9
| <mirrors> <mirror> <id>aliyunmaven</id> <mirrorOf>central</mirrorOf> <name>阿里云公共仓库</name> <url>https://maven.aliyun.com/repository/public</url> </mirror> </mirrors>
|
配置 JDK 版本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| <profiles> <profile> <id>jdk-11</id> <activation> <activeByDefault>true</activeByDefault> <jdk>11</jdk> </activation> <properties> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> <maven.compiler.encoding>UTF-8</maven.compiler.encoding> </properties> </profile> </profiles>
<properties> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties>
|
常用配置项
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"> <localRepository>/path/to/local/repo</localRepository> <interactiveMode>true</interactiveMode> <offline>false</offline> <pluginGroups> <pluginGroup>org.codehaus.mojo</pluginGroup> </pluginGroups> <servers> <server> <id>nexus-releases</id> <username>admin</username> <password>admin123</password> </server> </servers> <mirrors> <mirror> <id>aliyun</id> <mirrorOf>*</mirrorOf> <url>https://maven.aliyun.com/repository/public</url> </mirror> </mirrors> <proxies> <proxy> <id>optional</id> <active>true</active> <protocol>http</protocol> <host>proxy.example.com</host> <port>8080</port> </proxy> </proxies> <profiles> <profile> <id>dev</id> <properties> <env>development</env> </properties> </profile> </profiles> <activeProfiles> <activeProfile>dev</activeProfile> </activeProfiles> </settings>
|
基本操作
创建项目
使用 archetype 创建项目
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| mvn archetype:generate
mvn archetype:generate \ -DgroupId=com.example \ -DartifactId=my-app \ -DarchetypeArtifactId=maven-archetype-quickstart \ -DinteractiveMode=false
mvn archetype:generate \ -DgroupId=com.example \ -DartifactId=my-webapp \ -DarchetypeArtifactId=maven-archetype-webapp \ -DinteractiveMode=false
|
手动创建项目结构
1 2 3 4 5 6 7 8 9 10 11 12
| my-project/ ├── src/ │ ├── main/ │ │ ├── java/ │ │ ├── resources/ │ │ └── webapp/ │ └── test/ │ ├── java/ │ └── resources/ ├── target/ └── pom.xml
|
构建项目
常用构建命令
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| mvn clean
mvn compile
mvn test
mvn package
mvn install
mvn deploy
mvn site
mvn clean compile mvn clean package mvn clean install mvn clean deploy
|
跳过测试
1 2 3 4 5
| mvn clean package -DskipTests
mvn clean package -Dmaven.test.skip=true
|
查看项目信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| mvn dependency:tree
mvn help:effective-pom
mvn help:effective-settings
mvn help:describe -Dplugin=compiler
mvn help:help
mvn dependency:analyze
|
清理项目
1 2 3 4 5 6 7 8 9 10 11 12 13
| mvn clean
mvn clean compile
mvn clean package
|
依赖管理
依赖声明
在 pom.xml 中使用 <dependencies> 标签声明依赖。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.7.0</version> </dependency> </dependencies>
|
依赖范围(Scope)
| Scope |
编译 |
测试 |
运行 |
示例 |
| compile |
✓ |
✓ |
✓ |
spring-core(默认) |
| provided |
✓ |
✓ |
✗ |
servlet-api |
| runtime |
✗ |
✓ |
✓ |
JDBC 驱动 |
| test |
✗ |
✓ |
✗ |
junit |
| system |
✓ |
✓ |
✓ |
本地 JAR(不推荐) |
1 2 3 4 5
| 细节注意: 1. compile 是默认范围,适用于大多数情况 2. provided 用于容器提供的依赖(如 Tomcat 提供的 servlet-api) 3. test 仅用于测试阶段 4. 避免使用 system 范围,会导致不可移植
|
依赖传递
Maven 会自动解析依赖的依赖(transitive dependencies)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
<dependency> <groupId>com.example</groupId> <artifactId>B</artifactId> <version>1.0</version> </dependency>
<dependency> <groupId>com.example</groupId> <artifactId>C</artifactId> <version>1.0</version> </dependency>
|
排除依赖
1 2 3 4 5 6 7 8 9 10 11 12
| <dependency> <groupId>com.example</groupId> <artifactId>B</artifactId> <version>1.0</version> <exclusions> <exclusion> <groupId>com.example</groupId> <artifactId>C</artifactId> </exclusion> </exclusions> </dependency>
|
依赖调解
当出现依赖冲突时,Maven 使用以下规则:
- 最短路径优先:依赖路径越短优先级越高
- 最先声明优先:路径长度相同时,先声明的优先
1 2 3 4 5
| mvn dependency:tree
mvn dependency:tree -Dverbose
|
可选依赖
1 2 3 4 5 6 7 8 9 10 11 12
| <dependency> <groupId>com.example</groupId> <artifactId>optional-lib</artifactId> <version>1.0</version> <optional>true</optional> </dependency>
# 细节注意: # 1. 可选依赖不会被传递 # 2. 使用者需要显式声明该依赖才能使用 # 3. 适用于提供多种实现方案的库
|
依赖管理(Dependency Management)
在父 POM 中统一管理依赖版本,子模块继承时不需要指定版本。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| <dependencyManagement> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.3.20</version> </dependency> </dependencies> </dependencyManagement>
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> </dependencies>
|
插件管理
常用插件
| 插件 |
功能 |
常用目标 |
| maven-compiler-plugin |
编译 Java 代码 |
compile, testCompile |
| maven-surefire-plugin |
运行单元测试 |
test |
| maven-jar-plugin |
打包 JAR 文件 |
jar |
| maven-war-plugin |
打包 WAR 文件 |
war |
| maven-assembly-plugin |
自定义打包 |
single |
| maven-shade-plugin |
创建可执行 JAR |
shade |
| maven-source-plugin |
生成源码包 |
jar-no-fork |
| maven-javadoc-plugin |
生成 Javadoc |
jar |
插件配置
编译器插件配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.10.1</version> <configuration> <source>11</source> <target>11</target> <encoding>UTF-8</encoding> <compilerArgs> <arg>-parameters</arg> </compilerArgs> </configuration> </plugin> </plugins> </build>
|
Surefire 插件配置(测试)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.0.0-M7</version> <configuration> <skipTests>false</skipTests> <includes> <include>**/*Test.java</include> </includes> <excludes> <exclude>**/*IntegrationTest.java</exclude> </excludes> <parallel>methods</parallel> <threadCount>4</threadCount> </configuration> </plugin>
|
Shade 插件配置(可执行 JAR)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.4.1</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.Main</mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin>
|
插件管理(Plugin Management)
在父 POM 中统一管理插件版本。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| <build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.10.1</version> </plugin> </plugins> </pluginManagement> </build>
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> </plugin> </plugins> </build>
|
多模块项目
项目结构
1 2 3 4 5 6 7 8 9 10 11
| parent-project/ ├── pom.xml # 父 POM ├── module-a/ │ ├── pom.xml # 子模块 A │ └── src/ ├── module-b/ │ ├── pom.xml # 子模块 B │ └── src/ └── module-c/ ├── pom.xml # 子模块 C └── src/
|
父 POM 配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| <project> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>parent-project</artifactId> <version>1.0.0</version> <packaging>pom</packaging> <modules> <module>module-a</module> <module>module-b</module> <module>module-c</module> </modules> <dependencyManagement> <dependencies> </dependencies> </dependencyManagement> <build> <pluginManagement> <plugins> </plugins> </pluginManagement> </build> </project>
|
子模块配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| <project> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.example</groupId> <artifactId>parent-project</artifactId> <version>1.0.0</version> </parent> <artifactId>module-a</artifactId> <packaging>jar</packaging> <dependencies> <dependency> <groupId>com.example</groupId> <artifactId>module-b</artifactId> <version>${project.version}</version> </dependency> </dependencies> </project>
|
模块间依赖
1 2 3 4 5 6 7 8 9 10
| 依赖关系示例: module-a 依赖 module-b module-c 依赖 module-a 和 module-b
构建顺序: 1. module-b(无依赖) 2. module-a(依赖 module-b) 3. module-c(依赖 module-a 和 module-b)
Maven 会根据依赖关系自动确定构建顺序
|
生命周期与阶段
完整生命周期
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| clean 生命周期: - pre-clean - clean - post-clean
default 生命周期(部分重要阶段): - validate:验证项目 - initialize:初始化 - generate-sources:生成源代码 - process-sources:处理源代码 - generate-resources:生成资源文件 - process-resources:处理资源文件 - compile:编译 - process-classes:处理编译后的文件 - generate-test-sources:生成测试源代码 - process-test-sources:处理测试源代码 - generate-test-resources:生成测试资源 - process-test-resources:处理测试资源 - test-compile:编译测试代码 - process-test-classes:处理测试编译后的文件 - test:运行测试 - prepare-package:准备打包 - package:打包 - pre-integration-test:准备集成测试 - integration-test:运行集成测试 - post-integration-test:集成测试后处理 - verify:验证 - install:安装到本地仓库 - deploy:部署到远程仓库
site 生命周期: - pre-site - site:生成站点 - post-site - site-deploy:部署站点
|
绑定插件目标到阶段
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>3.1.0</version> <executions> <execution> <id>custom-task</id> <phase>compile</phase> <goals> <goal>run</goal> </goals> <configuration> </configuration> </execution> </executions> </plugin> </plugins> </build>
|
自定义生命周期
通过插件可以实现自定义任务。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>3.1.0</version> <executions> <execution> <id>custom-script</id> <phase>process-resources</phase> <goals> <goal>exec</goal> </goals> <configuration> <executable>bash</executable> <arguments> <argument>scripts/custom.sh</argument> </arguments> </configuration> </execution> </executions> </plugin>
|
profiles 配置
Profile 的作用
Profile 允许为不同环境定义不同的配置,如开发、测试、生产环境。
定义 Profile
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| <profiles> <profile> <id>dev</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <env>development</env> <db.url>jdbc:mysql://localhost:3306/dev</db.url> <db.username>dev_user</db.username> <db.password>dev_pass</db.password> </properties> </profile> <profile> <id>test</id> <properties> <env>testing</env> <db.url>jdbc:mysql://test-server:3306/test</db.url> <db.username>test_user</db.username> <db.password>test_pass</db.password> </properties> </profile> <profile> <id>prod</id> <properties> <env>production</env> <db.url>jdbc:mysql://prod-server:3306/prod</db.url> <db.username>prod_user</db.username> <db.password>prod_pass</db.password> </properties> </profile> </profiles>
|
激活 Profile
1 2 3 4 5 6 7 8 9 10 11 12 13
| mvn clean package -P prod
mvn clean package -P dev,debug
mvn clean package -Denv=prod
<activeProfiles> <activeProfile>dev</activeProfile> </activeProfiles>
|
Profile 激活条件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| <profile> <id>jdk-11</id> <activation> <jdk>11</jdk> <os> <name>Windows 10</name> <family>Windows</family> <arch>x86_64</arch> <version>10.0</version> </os> <file> <exists>config.properties</exists> <missing>debug.properties</missing> </file> <property> <name>environment</name> <value>production</value> </property> </activation> </profile>
|
使用 Profile 属性
1 2 3 4 5 6 7 8 9 10
| <properties> <database.url>${db.url}</database.url> <database.username>${db.username}</database.username> </properties>
db.url=${db.url} db.username=${db.username}
|
资源过滤
资源文件处理
Maven 可以过滤资源文件,替换其中的占位符。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| <build> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> </resource> <resource> <directory>src/main/resources</directory> <filtering>false</filtering> <excludes> <exclude>**/*.properties</exclude> </excludes> </resource> </resources> </build>
|
过滤示例
1 2 3 4
| app.name=${project.name} app.version=${project.version} db.url=${db.url}
|
1 2 3 4 5 6
| <properties> <db.url>jdbc:mysql://localhost:3306/mydb</db.url> </properties>
|
发布管理
配置发布仓库
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <distributionManagement> <repository> <id>nexus-releases</id> <name>Nexus Release Repository</name> <url>http://nexus.example.com/repository/maven-releases/</url> </repository> <snapshotRepository> <id>nexus-snapshots</id> <name>Nexus Snapshot Repository</name> <url>http://nexus.example.com/repository/maven-snapshots/</url> </snapshotRepository> </distributionManagement>
|
配置服务器认证
1 2 3 4 5 6 7 8 9 10 11 12 13
| <servers> <server> <id>nexus-releases</id> <username>admin</username> <password>admin123</password> </server> <server> <id>nexus-snapshots</id> <username>admin</username> <password>admin123</password> </server> </servers>
|
执行部署
1 2 3 4 5 6 7 8 9 10 11
| mvn deploy
mvn clean deploy
|
最佳实践
项目结构规范
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| 推荐的项目结构: project/ ├── src/ │ ├── main/ │ │ ├── java/ │ │ ├── resources/ │ │ │ ├── config/ │ │ │ ├── i18n/ │ │ │ └── static/ │ │ └── webapp/ │ └── test/ │ ├── java/ │ └── resources/ ├── docs/ # 文档 ├── scripts/ # 脚本 ├── pom.xml ├── README.md └── .gitignore
|
版本管理规范
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| 语义化版本(Semantic Versioning): - MAJOR.MINOR.PATCH(主版本.次版本.修订版本) - 主版本:不兼容的 API 修改 - 次版本:向下兼容的功能性新增 - 修订版本:向下兼容的问题修正
示例: - 1.0.0:初始稳定版本 - 1.0.1:修复 bug - 1.1.0:新增功能 - 2.0.0:重大变更
快照版本: - 1.0.0-SNAPSHOT:开发中的版本 - 正式发布时去掉 -SNAPSHOT
|
依赖管理最佳实践
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-framework-bom</artifactId> <version>5.3.20</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
<properties> <spring.version>5.3.20</spring.version> <junit.version>4.13.2</junit.version> </properties>
mvn versions:display-dependency-updates
mvn versions:display-plugin-updates
|
插件配置最佳实践
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.10.1</version> </plugin> </plugins> </pluginManagement> </build>
|
构建优化
1 2 3 4 5 6 7 8 9 10 11 12
| mvn clean install -T 4 mvn clean install -T 1C
mvn clean install -o
mvn clean install -pl module-a -am
mvn clean install -DskipTests -Dmaven.javadoc.skip=true
|
常见问题
依赖冲突解决
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| mvn dependency:tree
mvn dependency:tree -Dverbose | grep "conflict"
<exclusions> <exclusion> <groupId>conflict-group</groupId> <artifactId>conflict-artifact</artifactId> </exclusion> </exclusions>
<dependencyManagement> <dependencies> <dependency> <groupId>conflict-group</groupId> <artifactId>conflict-artifact</artifactId> <version>desired-version</version> </dependency> </dependencies> </dependencyManagement>
|
下载速度慢
1 2 3 4 5 6 7 8 9 10 11 12 13
| <mirrors> <mirror> <id>aliyun</id> <mirrorOf>central</mirrorOf> <url>https://maven.aliyun.com/repository/public</url> </mirror> <mirror> <id>tencent</id> <mirrorOf>central</mirrorOf> <url>https://mirrors.cloud.tencent.com/nexus/repository/maven-public/</url> </mirror> </mirrors>
|
内存不足
1 2 3 4 5
| export MAVEN_OPTS="-Xms512m -Xmx2048m"
set MAVEN_OPTS=-Xms512m -Xmx2048m
|
编码问题
1 2 3 4 5
| <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> </properties>
|
报错处理
💗💗 Maven 报错:Could not resolve dependencies
1 2 3 4 5 6 7 8 9 10 11 12
| 错误原因: 1. 依赖不存在或版本错误 2. 网络问题无法下载 3. 仓库配置错误
解决方案: 1. 检查依赖坐标是否正确 2. 检查网络连接和仓库配置 3. 清除本地缓存后重试 rm -rf ~/.m2/repository/group/artifact mvn clean install 4. 检查是否需要认证信息
|
💗💗 Maven 报错:Failed to execute goal
1 2 3 4 5 6 7 8 9 10
| 错误原因: 1. 插件配置错误 2. JDK 版本不匹配 3. 项目结构不符合规范
解决方案: 1. 检查插件版本和配置 2. 确认 JDK 版本符合要求 3. 检查项目结构是否符合 Maven 规范 4. 查看详细错误信息:mvn clean install -X
|
💗💗 Maven 报错:SSL certificate problem
1 2 3 4 5 6
| 解决方案:
mvn clean install -Dmaven.wagon.http.ssl.insecure=true -Dmaven.wagon.http.ssl.allowall=true
keytool -importcert -alias nexus -file cert.pem -keystore $JAVA_HOME/lib/security/cacerts
|
学习资源
- 视频
- 尚硅谷Maven教程:
https://www.bilibili.com/video/BV12q4y147e4
- 官方文档
- Maven 官方文档:
https://maven.apache.org/guides/index.html
- Maven Central 搜索:
https://search.maven.org/
- 书籍
- 《Maven 实战》:许晓斌著
- 《Maven 权威指南》:Sonatype 著
- 教程
- Maven 入门教程:
https://www.runoob.com/maven/maven-tutorial.html
- Baeldung Maven 教程:
https://www.baeldung.com/category/maven/
- 工具
- Maven 版本管理插件:
versions-maven-plugin
- 依赖分析工具:
maven-dependency-plugin
- 社区
- Stack Overflow Maven 标签:
https://stackoverflow.com/questions/tagged/maven
- Maven 邮件列表:
https://maven.apache.org/mailing-lists.html