廖雪峰Java12maven基础-2maven进阶-1使用插件
1.maven的Lifecycle,Phase和Goal:
- 使用maven构建项目就是执行Lifecycle
- 执行Lifecycle就是按顺序执行一系列Phase
- 每执行一个Phase,都会执行该Phase绑定的若干Goal
- Goal是最小任务单元
2.maven通过调用不同的插件Plugin来构建项目的。
mvn compile:执行compile这个Phase时,maven本身并不知道如何执行compile。它是通过插件来执行。maven会调用compiler插件执行compile这个Phase。compiler插件会执行和compile关联的compiler:compile这个Goal来完成编译。
3.maven常用的标准插件
插件名称 | 对应执行的Phase |
---|---|
clean | clea |
compiler | compile |
surefire | test |
jar | package |
常用插件:
- maven-shade-plugin:打包所有依赖包并生成可执行jar
- cobertura-maven-plugin:生成单元测试覆盖率报告
- findbugs-maven-plugin:对Java源码进行静态分析以找出潜在问题
如果标准的插件无法满足需求,还可以自定义插件
3.1maven-shade-plugin
例如:想要创建1个可执行的jar包,同时把所有可依赖的jar包都打包到自己最终生成的jar包中。此时我们需要在pom.xml中声明自定义的plugin。注意maven自带的标准插件比如compile是无须声明的,只有当我们加入了其他的插件才需要声明。
百度搜索maven shade plugin executable jar,进入其官网,查看示例
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>org.sonatype.haven.HavenCli</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
...
</project>
进入工程,修改pom.xml,在
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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.forme</groupId>
<artifactId>OneWorld</artifactId>
<version>1.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>OneWorld</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version><!--JDK1.8-->
<maven.compiler.source>1.8</maven.compiler.source><!--Java源码使用1.8格式-->
<maven.compiler.target>1.8</maven.compiler.target><!--编译后的class文件采用1.8格式-->
</properties>
<dependencies>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jcl</artifactId>
<version>2.10.0</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.10.0</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<!--此处需要指定main方法所在class-->
<mainClass>com.forme.App</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
App.java
package com.forme;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
Log log = LogFactory.getLog(App.class);
log.info("Hello,world!");
}
}
AppTest.java
package com.forme;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
/**
* Unit test for simple App.
*/
public class AppTest
{
/**
* Rigorous Test :-)
*/
@Test
public void shouldAnswerWithTrue()
{
assertTrue( true );
}
}
目录结构
命令行打包成功
#切换到工程根目录,打包
mvn clean package
#进入target目录,查看jar文件
ls -lh *jar
![](https://img2018.cnblogs.com/blog/1418970/201907/1418970-20190703210849575-1943583950.jpg)
![](https://img2018.cnblogs.com/blog/1418970/201907/1418970-20190703211248981-1920391733.png)
3.2 cobertura maven plugin usage
搜索cobertura maven plugin usage,进入官网
<project>
<reporting>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.7</version>
<reportSets>
<reportSet>
<reports>
<report>cobertura</report>
</reports>
</reportSet>
</reportSets>
</plugin>
</plugins>
</reporting>
</project>
3.2.1 初识cobertura
将
<?xml version="1.0" encoding="UTF-8"?>
<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.forme</groupId>
<artifactId>OneWorld</artifactId>
<version>1.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>OneWorld</name>
<!--FIXME change it to the project's website-->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version><!--JDK1.8-->
<maven.compiler.source>1.8</maven.compiler.source><!--Java源码使用1.8格式-->
<maven.compiler.target>1.8</maven.compiler.target><!--编译后的class文件采用1.8格式-->
</properties>
<dependencies>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jcl</artifactId>
<version>2.10.0</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.10.0</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<!--此处需要修改main函数所在类-->
<mainClass>com.forme.App</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.7</version>
</plugin>
</plugins>
</build>
</project>
App.java
package com.forme;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
Log log = LogFactory.getLog(App.class);
log.info("Hello,world!");
}
//增加一个求和的方法
int sum(int ... ns){
int x = 0;
for(int n:ns){
x += n;
}
return x;
}
}
AppTest.java
package com.forme;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
/**
* Unit test for simple App.
*/
public class AppTest
{
/**
* Rigorous Test :-)
*/
@Test
public void shouldAnswerWithTrue()
{
assertEquals(6,new App().sum(1,2,3));
}
}
#切换到项目的根目录
mvn cobertura:cobertura
![](https://img2018.cnblogs.com/blog/1418970/201907/1418970-20190704084541785-623145812.png)
![](https://img2018.cnblogs.com/blog/1418970/201907/1418970-20190704101837196-299838234.png)
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
/**
- Unit test for simple App.
/
public class AppTest
{
/*- Rigorous Test 😃
*/
@Test
public void shouldAnswerWithTrue()
{
App.main(null);
assertEquals(6,new App().sum(1,2,3));
}
}
- Rigorous Test 😃
再次执行mvn cobertura:cobertura,测试覆盖率达到了100%
<img src="https://img2018.cnblogs.com/blog/1418970/201907/1418970-20190704102059370-1421298262.png" width="500" />
## 4总结:
* maven通过自定义插件可以执行项目构建时需要的额外功能
* 在pom.xml中声明插件及配置
* 插件会在某个Phase被执行
* 插件的配置和用法需参考插件官方文档