mavenJAVA开发Java学习笔记

maven---7生命周期和插件

2016-11-21  本文已影响742人  zlcook

1Maven生命周期设计思想(模板方法模式)

package com.cnu.offline.template.method;

public abstract class AbstractBuild {
    /**
     * 项目构建
     */
    public void build(){
        initialize();
        compile();
        test();
        packagee();
        integrationTest();
        deploy();
    }

    protected abstract void initialize();
    protected abstract void compile();
    protected abstract void test() ;
    protected abstract void packagee();
    protected abstract void integrationTest();
    protected abstract void deploy();
}

2生命周期详解

2.1三套生命周期

2.2clean生命周期

2.3default生命周期

进一步了解其他阶段信息

2.4site生命周期

2.5命令行与生命周期

E:\Repository\iqasproject\iqasweb>mvn clean install
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building iqasweb Maven Webapp 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ iqasweb ---
[INFO] Deleting E:\Repository\iqasproject\iqasweb\target
[INFO]
[INFO] --- maven-resources-plugin:2.7:resources (default-resources) @ iqasweb --
-
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 14 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.3:compile (default-compile) @ iqasweb ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 388 source files to E:\Repository\iqasproject\iqasweb\target\cl
asses
[INFO]
[INFO] --- maven-resources-plugin:2.7:testResources (default-testResources) @ iq
asweb ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory E:\Repository\iqasproject\iqasweb\src
\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.3:testCompile (default-testCompile) @ iqasweb
 ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 8 source files to E:\Repository\iqasproject\iqasweb\target\test
-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.19:test (default-test) @ iqasweb ---
[INFO] Tests are skipped.
[INFO]
[INFO] --- maven-war-plugin:2.1.1:war (default-war) @ iqasweb ---
[INFO] Packaging webapp
[INFO] Assembling webapp [iqasweb] in [E:\Repository\iqasproject\iqasweb\target\
iqasweb]
[INFO] Processing war project
[INFO] Copying webapp resources [E:\Repository\iqasproject\iqasweb\src\main\weba
pp]
[INFO] Webapp assembled in [7681 msecs]
[INFO] Building war: E:\Repository\iqasproject\iqasweb\target\iqasweb.war
[WARNING] Warning: selected war files include a WEB-INF/web.xml which will be ig
nored
(webxml attribute is missing from war task, or ignoreWebxml attribute is specifi
ed as 'true')
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ iqasweb ---
[INFO] Installing E:\Repository\iqasproject\iqasweb\target\iqasweb.war to D:\Sof
t\maven\maven_jar\repository\com\cnu\iqas\iqasweb\0.0.1-SNAPSHOT\iqasweb-0.0.1-S
NAPSHOT.war
[INFO] Installing E:\Repository\iqasproject\iqasweb\pom.xml to D:\Soft\maven\mav
en_jar\repository\com\cnu\iqas\iqasweb\0.0.1-SNAPSHOT\iqasweb-0.0.1-SNAPSHOT.pom

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

3插件目标

4插件绑定

我们把插件的目标绑定到maven生命周期的某个阶段用于完成实际任务,叫做插件绑定。例如:maven-compiler-plugin这一插件的compile目标能够完成项目编译这一任务,因此就把该目标绑定到default生命周期的compile这一阶段。

4.1内置绑定

4.2自定义绑定

除了内置绑定外,用户还可以选择将某个插件目标绑定到生命周期的某个阶段。

需求:创建项目源码包
*内置的插件没有涉及这一任务,需要自己配置。

<build>
   <plugins>
         <plugin>
                 <groupId>org.apache.maven.plugins</groupId>
                 <artifactId>maven-source-plugin</artifactId>
                 <version>2.1.1</version>
                 <executions>
                    <execution>
                     <id>my-attach-sources</id>
                     <phase>verify</phase>
                     <goals>
                        <goal>jar-no-fork</goal>
                     </goals>
                    </execution>
                 </executions>
            </plugin>
    </plugins>
</build>

解释

[INFO] --- maven-source-plugin:2.1.1:jar-no-fork (my-attach-sources) @ iqasweb ---
[INFO] Building jar: E:\Repository\iqasproject\iqasweb\target\iqasweb-sources.jar

5插件配置

5.1命令行插件配置

案例
maven-surefire-plugin提供了一个maven.test.skip的命令行参数(在pom中参数为skip),当其值为true时会跳过执行测试。

$mvn install -Dmaven.test.skip=true

5.2POM中插件全局配置

案例

<plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.3</version>
          <configuration>
           <!-- if you want to use the Java 8 language features (-source 1.8) and also want the compiled classes to be compatible with JVM 1.8 (-target 1.8),  -->
              <source>1.7</source>
              <target>1.7</target>
          </configuration>
      </plugin>

5.3POM中插件任务配置

 <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.3</version>
          <executions>
            <execution>
                <id>my-compile</id>
                <phase>compile</phase>
                <goals>
                   <goal>compile</goal>
                </goals>
                <configuration>
              <source>1.7</source>
              <target>1.7</target>
          </configuration>
            </execution>
          </executions>
      </plugin>

6获取插件信息

6.1在线插件信息

一般来说,通过阅读插件文档中的使用介绍和实例就可以很好的使用插件了,如果想要了解目标参数,就需要访问插件的每个目标的文档。

6.2使用maven-help-plugin描述插件

常见使用方法

案例:查看maven-compiler-plugin的信息
方法1:
mvn help:describe -Dplugin=org.apache.maven.plugins:maven-compiler-plugin:2.1

Name: Maven Compiler Plugin
Description: The Compiler Plugin is used to compile the sources of your
  project.
Group Id: org.apache.maven.plugins
Artifact Id: maven-compiler-plugin
Version: 2.1
Goal Prefix: compiler

This plugin has 3 goals:

compiler:compile
  Description: Compiles application sources

compiler:help
  Description: Display help information on maven-compiler-plugin.
    Call
      mvn compiler:help -Ddetail=true -Dgoal=<goal-name>
    to display parameter details.
  
compiler:testCompile
  Description: Compiles application test sources.

方法2:
使用前缀,maven-compiler-plugin的前缀为compiler,在描述插件时可以省略版本信息,maven就会获取最新版的进行表达:
mvn help:describe -Dplugin=compiler

方法3:
如果想输出更详细信息,包括插件每个目标的参数,加上detail
mvn help:describe -Dplugin=compiler -Ddetail.

7从命令行调用插件

如果在命令行运行mvn -h来显示mvn命令帮助,就可以看到如下信息:告诉了mvn命令的基本用法

usage: mvn [options] [<goal(s)>] [<phase(s)>]

8插件解析机制

8.1插件仓库

<pluginRepositories>
    <pluginRepository>
      <id>central</id>
      <name>Central Repository</name>
      <url>https://repo.maven.apache.org/maven2</url>
      <layout>default</layout>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <releases>
        <updatePolicy>never</updatePolicy>
      </releases>
    </pluginRepository>
  </pluginRepositories>

8.2插件的默认groupId

8.3解析插件版本。

Paste_Image.png Paste_Image.png

8.4插件前缀

Paste_Image.png Paste_Image.png

有什么不懂的一起探讨一下吧,我也是在学习的路上。喜欢给我点个赞吧(哈哈),我会继续努力的。

上一篇 下一篇

猜你喜欢

热点阅读