引言
Maven是Java项目的标准构建工具。本文介绍Maven的核心功能和实战应用。
一、Maven核心概念
1.1 坐标(Coordinates)
1 2 3 4 5
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.7.0</version> </dependency>
|
1.2 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
| <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>my-app</artifactId> <version>1.0.0</version> <packaging>jar</packaging> <properties> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> </dependencies> <build> <plugins> </plugins> </build> </project>
|
二、依赖管理
2.1 依赖范围
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
| <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.3.20</version> <scope>compile</scope> </dependency>
<dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency>
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <scope>test</scope> </dependency>
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.29</version> <scope>runtime</scope> </dependency>
|
2.2 依赖传递
1 2 3 4 5 6 7 8 9 10 11
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency>
|
2.3 依赖管理(父POM)
1 2 3 4 5 6 7 8 9 10 11
| <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.7.0</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
|
三、生命周期
3.1 三套生命周期
1 2 3 4 5 6 7 8 9 10 11 12
| mvn clean
mvn compile mvn test mvn package mvn install mvn deploy
mvn site
|
3.2 常用命令
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| mvn clean package
mvn clean package -DskipTests
mvn clean install
mvn spring-boot:run
mvn dependency:tree
mvn help:effective-pom
|
四、插件配置
4.1 编译插件
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| <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> </configuration> </plugin> </plugins> </build>
|
4.2 Spring Boot插件
1 2 3 4 5 6 7 8 9 10 11 12
| <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.7.0</version> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin>
|
五、多模块项目
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <packaging>pom</packaging> <modules> <module>module-common</module> <module>module-dao</module> <module>module-service</module> <module>module-web</module> </modules>
<parent> <groupId>com.example</groupId> <artifactId>parent</artifactId> <version>1.0.0</version> </parent>
|
总结
Maven核心:
- 依赖管理:统一管理项目依赖
- 生命周期:标准化构建流程
- 插件系统:扩展构建功能
- 仓库管理:本地和远程仓库
参考资料