廖雪峰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来完成编译。html

3.maven经常使用的标准插件

插件名称 对应执行的Phase
clean clea
compiler compile
surefire test
jar package

经常使用插件:java

  • maven-shade-plugin:打包全部依赖包并生成可执行jar
  • cobertura-maven-plugin:生成单元测试覆盖率报告
  • findbugs-maven-plugin:对Java源码进行静态分析以找出潜在问题

若是标准的插件没法知足需求,还能够自定义插件web

3.1maven-shade-plugin

例如:想要建立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
apache

<?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

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

... 之间的内容复制到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函数所在类-->
                                    <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,能够查看结果

3.2.2 main方法未执行,完善测试覆盖率

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%

4总结:

  • maven经过自定义插件能够执行项目构建时须要的额外功能
  • 在pom.xml中声明插件及配置
  • 插件会在某个Phase被执行
  • 插件的配置和用法需参考插件官方文档
相关文章
相关标签/搜索