mvn compile:執行compile這個Phase時,maven自己並不知道如何執行compile。它是經過插件來執行。maven會調用compiler插件執行compile這個Phase。compiler插件會執行和compile關聯的compiler:compile這個Goal來完成編譯。html
插件名稱 | 對應執行的Phase |
---|---|
clean | clea |
compiler | compile |
surefire | test |
jar | package |
經常使用插件:java
若是標準的插件沒法知足需求,還能夠自定義插件web
例如:想要建立1個可執行的jar包,同時把全部可依賴的jar包都打包到本身最終生成的jar包中。此時咱們須要在pom.xml中聲明自定義的plugin。注意maven自帶的標準插件好比compile是無須聲明的,只有當咱們加入了其餘的插件才須要聲明。
百度搜索maven shade plugin executable jar,進入其官網,查看示例shell
<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.javamaven
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
能夠看到有2個jar包
original-OneWorld-1.1-SNAPSHOT.jar是maven本身的jar插件生成原始的jar包
OneWorld-1.1-SNAPSHOT.jar是由maven-shade-plugin生成的可執行的jar測試
java -jar OneWorld-1.1-SNAPSHOT.jar
這樣,能夠方便的經過maven-shade-plugin生成可執行的jarui
搜索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>
將
<?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
進入target/site/cobertura目錄下,打開index.html,能夠查看結果
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() { App.main(null); assertEquals(6,new App().sum(1,2,3)); } }
再次執行mvn cobertura:cobertura,測試覆蓋率達到了100%