目录
一、Maven常用命令
1、基本命令
- mvn clean:清理项目打包文件,即项目下的target目录
- mvn compile:编译项目下的src/main/Java目录源代码
- mvn test:执行测试程序
- mvn test-compile:编译测试程序,不编译主程序
- mvn package:项目打包,在项目target目录下生成编译后的jar或war等文件
- mvn install:项目打包并发布到本地仓库
- mvn site:生成项目相关信息的网站
2、命令参数参考:
- -D 传入属性参数
- -P 使用pom中指定的配置
- -e 显示maven运行出错的信息
- -o 离线执行命令,即不去远程仓库更新包
- -X 显示maven允许的debug信息
- -U 强制去远程参考更新snapshot包
例如 mvn install -Dmaven.test.skip=true -Poracle
其他参数可以通过mvn help 获取
3、其他命令:
- mvn eclipse:eclipse:生成eclipse项目
- mvn eclipse:clean:清除eclipse的一些系统设置
- mvn tomcat:run :在tomcat里面运行
- mvn package -Dmaven.test.skip=ture :打包时跳过测试
- mvn clean test、mvn clean package、mvn clean install :命令组合形式
参考:https://zhanghaiyang.blog.csdn.net/article/details/52170572
二、Maven生命周期
Maven生命周期定义了各个构建环节的执行顺序,对所有的构建过程进行抽象和统一。
Maven有三套相互独立的生命周期:
- Clean Lifecycle:在进行真正的构建之前进行一些清理工作。
- Default Lifecycle:构建的核心部分,编译,测试,打包,部署等等。
- Site Lifecycle:生成项目报告,站点,发布站点。
它们是相互独立的,可以仅仅调用clean来清理工作目录,仅仅调用site来生成站点。当然也可以直接运行 mvn clean install site 运行所有这三套生命周期。
1、Clean Lifecycle
clean 生命周期包含三个阶段:
- pre-clean:执行一些需要在clean之前完成的工作
- clean:移除所有上一次构建生成的文件
- post-clean:执行一些需要在clean之后立刻完成的工作
如果执行 mvn post-clean 命令,则运行上面三个阶段。如果执行mvn clean 命令,只运行 pre-clean 和 clean两个阶段。
*在工程pom.xml中添加一个插件,Maven 将会开始处理并显示 clean 生命周期的所有阶段。
<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.starfall.maven</groupId>
<artifactId>MavenTest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<id>id.pre-clean</id>
<phase>pre-clean</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>pre-clean phase</echo>
</tasks>
</configuration>
</execution>
<execution>
<id>id.clean</id>
<phase>clean</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>clean phase</echo>
</tasks>
</configuration>
</execution>
<execution>
<id>id.post-clean</id>
<phase>post-clean</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>post-clean phase</echo>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
执行mvn pre-clean
执行mvn clean
执行mvn post-clean
2、Default Lifecycle
Default生命周期是Maven生命周期中最重要的一个,基本上的生命周期发生在这里。
生命周期阶段 | 描述 |
---|---|
validate | 验证项目是否正确,并且所有必要的信息可用于完成构建过程 |
initialize | 建立初始化状态,例如设置属性 |
generate-sources | 产生任何的源代码包含在编译阶段 |
process-sources | 处理源代码,例如,过滤器值 |
generate-resources | 包含在包中产生的资源 |
process-resources | 复制和处理资源到目标目录,准备打包阶段 |
compile | 编译该项目的源代码 |
process-classes | 从编译生成的文件提交处理,例如:Java类的字节码增强/优化 |
generate-test-sources | 生成任何测试的源代码包含在编译阶段 |
process-test-sources | 处理测试源代码,例如,过滤器任何值 |
test-compile | 编译测试源代码到测试目标目录 |
process-test-classes | 处理测试代码文件编译生成的文件 |
test | 运行测试使用合适的单元测试框架(JUnit) |
prepare-package | 执行必要的任何操作的实际打包之前准备一个包 |
package | 提取编译后的代码,并在其分发格式打包,如JAR,WAR或EAR文件 |
pre-integration-test | 完成执行集成测试之前所需操作。例如,设置所需的环境 |
integration-test | 处理并在必要时部署软件包到集成测试可以运行的环境 |
pre-integration-test | 完成集成测试已全部执行后所需操作。例如,清理环境 |
verify | 运行任何检查,验证包是有效的,符合质量审核规定 |
install | 将包安装到本地存储库,它可以用作当地其他项目的依赖 |
deploy | 复制最终的包到远程仓库与其他开发者和项目共享 |
*继续添加Maven的插件:maven-antrun-plugin:run ,显示Default生命周期中的重要阶段
<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.starfall.maven</groupId>
<artifactId>MavenTest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<id>id.pre-clean</id>
<phase>pre-clean</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>pre-clean phase</echo>
</tasks>
</configuration>
</execution>
<execution>
<id>id.clean</id>
<phase>clean</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>clean phase</echo>
</tasks>
</configuration>
</execution>
<execution>
<id>id.post-clean</id>
<phase>post-clean</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>post-clean phase</echo>
</tasks>
</configuration>
</execution>
<execution>
<id>id.validate</id>
<phase>validate</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>validate phase</echo>
</tasks>
</configuration>
</execution>
<execution>
<id>id.compile</id>
<phase>compile</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>compile phase</echo>
</tasks>
</configuration>
</execution>
<execution>
<id>id.test</id>
<phase>test</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>test phase</echo>
</tasks>
</configuration>
</execution>
<execution>
<id>id.package</id>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>package phase</echo>
</tasks>
</configuration>
</execution>
<execution>
<id>id.deploy</id>
<phase>deploy</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>deploy phase</echo>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
执行mvn compile命令:
3、Site Lifecycle
Maven Site 插件一般用来创建新的报告文档、部署站点等。
包含四个阶段:
- pre-site:执行一些需要在生成站点文档之前完成的工作
- site:生成项目的站点文档
- post-site: 执行一些需要在生成站点文档之后完成的工作,并且为部署做准备
- site-deploy:将生成的站点文档部署到特定的服务器上
经常用到的是site阶段和site-deploy阶段,用以生成和发布Maven站点。是Maven相当强大的功能,Manager比较喜欢,文档及统计数据自动生成。
*继续添加插件 maven-antrun-plugin:run ,显示site过程
<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.starfall.maven</groupId>
<artifactId>MavenTest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<id>id.pre-clean</id>
<phase>pre-clean</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>pre-clean phase</echo>
</tasks>
</configuration>
</execution>
<execution>
<id>id.clean</id>
<phase>clean</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>clean phase</echo>
</tasks>
</configuration>
</execution>
<execution>
<id>id.post-clean</id>
<phase>post-clean</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>post-clean phase</echo>
</tasks>
</configuration>
</execution>
<execution>
<id>id.validate</id>
<phase>validate</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>validate phase</echo>
</tasks>
</configuration>
</execution>
<execution>
<id>id.compile</id>
<phase>compile</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>compile phase</echo>
</tasks>
</configuration>
</execution>
<execution>
<id>id.test</id>
<phase>test</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>test phase</echo>
</tasks>
</configuration>
</execution>
<execution>
<id>id.package</id>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>package phase</echo>
</tasks>
</configuration>
</execution>
<execution>
<id>id.deploy</id>
<phase>deploy</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>deploy phase</echo>
</tasks>
</configuration>
</execution>
<execution>
<id>id.pre-site</id>
<phase>pre-site</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>pre-site phase</echo>
</tasks>
</configuration>
</execution>
<execution>
<id>id.site</id>
<phase>site</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>site phase</echo>
</tasks>
</configuration>
</execution>
<execution>
<id>id.post-site</id>
<phase>post-site</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>post-site phase</echo>
</tasks>
</configuration>
</execution>
<execution>
<id>id.site-deploy</id>
<phase>site-deploy</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>site-deploy phase</echo>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
三、生命周期与自动化构建
Maven在运行任何一个阶段的时候,它前面的所有阶段都会被自动顺序的运行。