廖雪峰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
可以看到有2个jar包 original-OneWorld-1.1-SNAPSHOT.jar是maven自己的jar插件生成原始的jar包 OneWorld-1.1-SNAPSHOT.jar是由maven-shade-plugin生成的可执行的jar ```#shell java -jar OneWorld-1.1-SNAPSHOT.jar ``` 这样,可以方便的通过maven-shade-plugin生成可执行的jar

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方法未执行,完善测试覆盖率 ```#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()
      {
      App.main(null);
      assertEquals(6,new App().sum(1,2,3));
      }
      }
再次执行mvn cobertura:cobertura,测试覆盖率达到了100%
<img src="https://img2018.cnblogs.com/blog/1418970/201907/1418970-20190704102059370-1421298262.png" width="500" />


##    4总结:
*    maven通过自定义插件可以执行项目构建时需要的额外功能
*    在pom.xml中声明插件及配置
*    插件会在某个Phase被执行
*    插件的配置和用法需参考插件官方文档
posted on 2019-06-26 19:35  singleSpace  阅读(373)  评论(0编辑  收藏  举报