maven速记
核心点:项目构建、依赖管理、【更新】:源码关联
依赖的scope的含义:

构建:
- clean:清理
- compile:编译
- test:运行测试
- package:打包
打包fat-jar的插件:maven-shade-plugin
示例:
<build>
<sourceDirectory>src/main/scala</sourceDirectory>
<testSourceDirectory>src/test/scala</testSourceDirectory>
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.2.2</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
<configuration>
<args>
<arg>-make:transitive</arg>
<arg>-dependencyfile</arg>
<arg>${project.build.directory}/.scala_dependencies</arg>
</args>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
<!-- 不指定main方法,则后续运行时可以动态给出,程序也可以有多个main方法-->
<!-- <transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>cn.itcast.spark.WordCount</mainClass>
</transformer>
</transformers> -->
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
特别简单的项目,可以用assembly
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.6.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>
org.example.App2
</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
简单打包:

模块:

拆分后:

父模块:
<project>
<modelVersion>4.0.0</modelVersion>
<!-- 父项目坐标 -->
<groupId>com.example</groupId>
<artifactId>parent-project</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging> <!-- 注意打包类型为pom -->
<!-- 模块声明 -->
<modules>
<module>module-a</module>
<module>module-b</module>
<module>module-c</module>
</modules>
<!-- 公共依赖管理 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.8</version>
</dependency>
</dependencies>
</dependencyManagement>
<!-- 公共构建配置 -->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
子模块:
<project>
<modelVersion>4.0.0</modelVersion>
<!-- 继承父POM -->
<parent>
<groupId>com.example</groupId>
<artifactId>parent-project</artifactId>
<version>1.0.0</version>
</parent>
<!-- 子模块自己的坐标 -->
<artifactId>module-a</artifactId>
<packaging>jar</packaging> <!-- 通常子模块是jar或war -->
<!-- 依赖声明 -->
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<!-- 版本从父POM的dependencyManagement继承 -->
</dependency>
<!-- 依赖其他子模块 -->
<dependency>
<groupId>com.example</groupId>
<artifactId>module-b</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
打包命令:
# Maven 打包命令详解
## 常用 Maven 打包命令
### 1. 基本打包命令
```bash
mvn package
```
- 执行项目编译、测试并将代码打包成可部署的格式(JAR/WAR等)
- 打包结果默认生成在 `target/` 目录下
### 2. 带清理的打包
```bash
mvn clean package
```
- 先删除 `target/` 目录(clean 阶段)
- 然后执行完整打包流程
### 3. 跳过测试的打包
```bash
mvn package -DskipTests
```
或
```bash
mvn package -Dmaven.test.skip=true
```
- `-DskipTests`:编译测试代码但不执行测试
- `-Dmaven.test.skip=true`:完全跳过测试代码的编译和执行
### 4. 多模块项目打包
```bash
mvn clean package -pl module-a -am
```
- `-pl`:指定要构建的模块(project list)
- `-am`:同时构建依赖的模块(also make)
### 5. 指定配置文件的打包
```bash
mvn package -Pprod
```
- `-P`:激活指定的 profile(这里是 prod 环境配置)
### 6. 并行构建加速
```bash
mvn package -T 4
```
- `-T 4`:使用 4 个线程并行构建模块
## `clean` 的必要性分析
### 需要先执行 `clean` 的情况
1. **代码或资源有重大变更时**:
- 删除了某些类文件
- 修改了包结构
- 资源文件位置发生变化
2. **依赖关系发生变化时**:
- 修改了 `pom.xml` 中的依赖
- 需要确保使用最新的依赖
3. **构建出现奇怪问题时**:
- 类找不到等奇怪错误
- 怀疑是旧编译结果导致的
4. **发布正式版本前**:
- 确保从全新状态构建
- 避免包含旧的编译结果
### 可以省略 `clean` 的情况
1. **快速迭代开发时**:
- 只是修改了少量代码
- 不需要重新编译所有内容
2. **使用增量编译工具时**:
- 如 Eclipse 或 IDEA 的自动构建
- IDE 已经管理了编译状态
3. **明确知道变更不会影响已有构建时**:
- 只修改了测试代码
- 只修改了注释或日志内容
## 实际建议
1. **持续集成环境中**:总是使用 `clean package` 确保构建纯净
2. **本地开发测试时**:可以先用 `package` 快速验证,遇到问题再加 `clean`
3. **发布版本时**:必须使用 `clean package` 或 `clean install`
## 性能考量
- `clean` 会增加构建时间(需要重新编译所有内容)
- 但可以避免因缓存导致的构建问题
- 现代 Maven (3.1+) 有更好的增量编译支持,减少了 `clean` 的必要性
## 高级打包示例
### 1. 构建指定模块及其依赖
```bash
mvn clean package -pl user-service -am
```
### 2. 构建并安装到本地仓库
```bash
mvn clean install
```
### 3. 构建时指定内存参数
```bash
MAVEN_OPTS="-Xms512m -Xmx1024m" mvn clean package
```
### 4. 查看构建耗时
```bash
mvn clean package --batch-mode --show-version
```
是否添加 `clean` 取决于具体场景,在不确定时使用 `clean` 是更安全的选择,特别是在团队协作或发布环境中。

浙公网安备 33010602011771号