后端——工具——构建工具——Maven——第五章节(内置插件)

1、clean

1.1  概念

  执行mvn help:describe -Dplugin=clean,引用结果中的描述。

  The maven clean plugin is a plugin that remove files generated at build time in a project’s directory

clean插件删除在构建阶段生成的所有文件和文件夹,这些文件和文件夹大部分都是在target目录下,所以clean相当于删除target目录。

1.2   goals

  clean插件的goal有两个。clean, help

1.2.1    clean

  引用执行结果的描述:

This attempts to clean a project’s working directory of the file that were generated at build-time. By default, it discovers and deletes the directories configured in project.build.directory, project.build.outputDirectory, project.build.testOutputDirectory, and  project.reporting.outputDirectory

  删除四个目录下的文件,分别是:

  1. project.build.directory:编译的输出路径
  2. project.build.outputDirectory: 编译的代码和资源文件。
  3. project.build.testOutputDirectory:编译的测试代码和测试资源文件
  4. project.reporting.outputDirectory:生成报告的目录文件。

  clean插件的主要功能就是由clean的goal提供的。clean(goal)与clean(phase)绑定的过程是在maven-core-version.jar下的components.xml中定义的。

  1.2.1.1    源码

  打开maven-clean-plugin-version.jar,在其中查找CleanMojo类,如下是它的execute方法, 参考CleanMojo.java。

  它主要删除directories(目录),这些是编译的目录结构。fileset(文件集),这些是由自己配置的。其中的参数可以由configuration或者是与参数同名的标签指定。

  它的参数可以查看plugin.xml,它的parameter标签。

  1.2.1.2    示例

  执行mvn clean:clean,既可以理解为执行clean生命周期的clean(phase),也可以理解为执行clean插件的clean(goal)

  1.2.2     help

  引用指向结果中的描述:

Display help information on maven-clean-plugin, call mvn clean:help -Ddetail=true -Dgoal=<goal-name> to display parameter details

clean插件的帮助手册。它没有关联的goal。

1.2.2.1    源码

  可以查看一下HelpMojo.java的execute方法,参考HelpMojo.java。它的内容大致就是在拼接字符串,类似于toString方法。

  1.2.2.2    示例

  执行mvn clean:help,查看结果会发现和之前的mvn help:describe -Dplugin=clean结果基本相同。

  可以添加detail,goal的参数。

2、compiler

2.1   概念

  引用帮助手册中的定义:

The compiler plugin is used to compile the sources of your project

用于编译项目,即source folder下面的内容,通常包含四个文件夹,src/main/java, src/main/resources, src/main/test, src/main/test-resources。

2.2  goals

  它有三个goal, compiler,test-compiler,help。compile(goal)对应Default生命周期的compile(phase), test-compiler对应Default生命周期的test-compiler(phase)。

  2.2.1    compile

  引用帮助手册中的定义:

Compiles application sources

编译项目的代码和配置文件。(不包含测试代码)

2.2.1.1    源码

  在maven-plugin-version.jar中打开CompileMojo.java,它继承自CompilerMojo.java,它又继承自AbstractCompilerMojo.java。参考CompilerMojo.java

  类中的属性,在pom中引入插件时,可以配置。它的方式有两种,

  第一种是直接<fieldName></fieldname>,fieldName为字段名称,标签的值为字段的值。

  第二种是配置CompilerArgument,它的值为1到多个【-fieldName value】。

  2.2.1.2   示例

  示例1,配置属性标签:配置source,target,encoding三个属性

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.1</version>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
        <encoding>utf-8</encoding>
    </configuration>
</plugin>

  示例2,第二种方式,CompilerArgument标签。其他的不修改,Configuration子标签修改如下 

<configuration>
  <compilerArgument>
    -source 1.8 -target 1.8 -encoding UTF8
  </compilerArgument>
</configuration>

  执行mvn compile:compile,执行compile插件的compile(goal)。

  执行mvn default:compile,执行default生命周期的compile(phase)

  执行mvn compile,等价于mvn default:compile。

  2.2.2    test-compile

  test-compile与compile基本相同,区别在于编译的资源变为测试的资源和测试的代码。

  2.2.3   help

  与clean中的Help作用是相同的,介绍compile插件,是compile插件的帮助手册。执行它与执行mvn help:describe -Dplugin=compile的结果是相同的。

3、install

3.1  概念

  引用帮助手册中的定义:

Copies the project artifacts to the user’s local repository

把项目打成Jar包,并复制到本地仓库。第一步需要经过package(phase),第二步需要配置本地仓库,默认是在user_home/.m2/repo

3.2  goals

  它有三个goal,install,install-file。install对应default生命周期的Install(phase), install-file没有对应的phase。

3.2.1     install

  引用帮助手册的定义:

Install the project’s main artifact, and any other artifacts attached by other plugins in the lifecycle, to the local repository

它是install插件的核心goal,功能主要靠install(goal)实现。Default生命周期中,它的顺序是在post-package之后,所以通常都是将当前项目打包,并存储到本地仓库中,通常还包含一些其他文件

3.2.1.1    源码

  在maven-install-version.jar包中,查看InstallMojo.java类的execute方法,参考InstallMojo.java

  这个插件都不是很复杂,一个Jar包下只有大约10个左右的类。都看完也不会花太长时间。

  3.2.1.1   示例

  执行mvn install,之后在本地仓库查看,它的目录结构为groupId,artifactId,version的多层结构,groupId有点号(.),会创建多个目录,例如com.org.example,会创建com/org/example的目录结构。

  3.2.2     install-file

  install-file与install的区别在于,不再限定文件为当前项目的打包文件。引用帮助手册中的定义。

Installs a file in the local repository

安装一个Jar文件到本地仓库。

  这个命令很有用,尤其是由于某些原因通过maven下载不到,通过网络在其他地方下载到依赖之后,安装依赖到本地仓库。

  它需要指定groupId, artifactId, version和Jar包文件的路径。

  3.2.2.1   源码

  打开maven-install-version.jar包,查看InstallFileMojo.java的execute方法。

  查看plugins.xml,可以查看相关参数,参数的配置方式和compiler相同。

  3.2.2.2   示例

  执行mvn install:install-file -Dfile=路径 -DgroupId=组标识 -DartifactId=包名 -Dversion=版本号

  指定参数时,前面需要添加-D  

  3.2.3   help

  与其他help插件相同,执行mvn install:help显示install插件的用法。

4、deploy

4.1  概念

  引用帮助手册的定义:

Uploads the project artifacts to the internal remote repository

上传Jar包到远程的仓库中。与install命令基本相同,区别只在于install是安装Jar包到本地仓库,deploy在于安装Jar包远程仓库。

4.2  goals

  deploy插件有三个goal,deploy,deploy-file, help。基本与install插件相同。

  在使用install时,需要有本地仓库,若不配置,默认的本地仓库地址是user_home/.m2/repo。

  在使用deploy时,需要有远程仓库,它没有默认值,不配置时,默认会失败。

<!-- 配置远程仓库 -->
<distributionManagement>
    <repository>
        <!-- 给远程仓库起名称 -->
        <id>private-maven-repo</id>
        <!-- 给远程仓库起名称 -->
        <name>XX Maven Repository</name>
        <!-- 远程仓库的URL地址 -->
        <url>remote_maven_repo_url</url>
    </repository>
</distributionManagement>

  之后在setting.xml中配置server标签,其中id为远程仓库的ID。远程仓库若需要验证,有两种方式,用户名和密码,SSH方式(提供证书),参考第三章节的server标签。

5、sunfire

5.1   goals

  surefire有两个goal,help和test,help显示surefire插件的用法,test是运行测试脚本的。

  5.1.1    test

  引用帮助手册中的定义:

Run tests using Surefire

使用surefire插件运行测试用例。它需要用到测试框架,通常是Junit框架,在项目创建时,Junit框架是唯一添加的依赖。

test(goal)与default生命周期的test(phase)关联。

5.1.1.1   示例

  示例:执行mvn test,运行src/main/test下面的所有测试脚本,运行失败会报异常。

  执行mvn surefire:test执行surefire的test(goal),与mvn test等价。

6、site

6.1   概念

  引用帮助手册中的定义:

The maven site plugin is a plugin that generates a site for the current project

site插件为当前项目生成一份报告(概览)。个人感觉没啥用。它与site生命周期的site(phase)关联。

6.2   goals

  6.2.1    attach-descriptor

  引用帮助手册中的定义:

Adds the site descriptor(site.xml) to the list of files to be installed/deployed

把配置文件(site.xml)添加到生成的报告中,默认行为。

6.2.2     deploy

  引用帮助手册中的定义

  Deploys the generated site using wagon supported protocols to the site URL specified in the <distributionManagement> section of the POM. For scp protocol, the website files are packaged by wagon into zip archive, then the archive is transfered to the remote host, next it is un-archived,which is much faster than making a file by file copy

把生成的报告部署到远程仓库,<distributionManagement>标签用于配置远程仓库。

6.2.3    effective-site

  引用帮助手册的定义:

Displays the effective site descriptor as an XML for this build, after inheritance and interpolation of site.xml.

 类似于effective-pom,显示最终的site.xml(配置文件)

6.2.4    help

  与其他插件的help(goal)相同,显示site插件的帮助手册。

  6.2.5    jar

  引用帮助手册中的定义:

Bundles the site output into a Jar so that it can be deployed to a repository

把生成的报告打包成Jar,方便部署到远程。

6.2.6     run

  引用帮助手册中的定义:

Starts the site up, rendering documents as requested for faster editing. It uses Jetty as the web server

 通常生成的报告都是静态资源,无需启用web 服务器,直接访问index.html即可。所以run(goal)没有意义。

6.2.7    site

  引用帮助手册中的定义:

Generates the site for a single project. Note that links between module sites in a multi module build will not work, since local build directory structure doesn't match deployed site

它是site插件的核心goal, 执行它之后会生成项目的报告

6.2.8     stage

  TODO

  6.2.9    stage-deploy

  TODO

7、Jar

7.1 概念

  引用帮助手册中的定义:

  Builds a Java Archive (JAR) file from the compiled project classes and resources

       把当前编译的项目生成Jar包。

7.2   goals

它有三个goal,jar, test-jar, help。

jar用于把当前的项目生成jar包。它与default生命周期的packaging(phase)关联。

test-jar把测试资源打包成jar。

help显示jar插件的帮助手册

8、Source

8.1   概念

引用帮助手册中的定义:

The Maven Source plugin creates a Jar archive of the source file of the current Project

source把当前项目的源码打包成Jar包,与Jar插件基本相同,内容一个是字节码文件,一个是源码。

默认情况下,source没有与生命周期绑定,所以使用时,需要引入,引入之后,在source插件下定义的plugins.xml中定义了绑定关系。

在pom.xml中引入source插件的配置如下:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-source-plugin</artifactId>
    <version>版本号</version>
    <configuration>
        <outputDirectory>${project.build.outputDirectory}</outputDirectory>
        <finalName>${project.name}-jar</finalName>
    </configuration>
</plugin>

8.2   goals

  执行mvn source:help,结果中可以看到source插件有7个goal。

  8.2.1    aggregate

引用帮助手册中的定义:

Aggregate sources for all modules in an aggregator project

8.2.2     generated-test-jar

引用帮助手册中的定义:

This plugin bundles all the test source into a jar archive

把当前项目的测试资源打包成Jar包。

项目配置过source插件后,执行mvn source:generated-test-jar命令,查看target/classes目录,可以查看生成的Jar包内容。

8.2.3     help

显示source插件的帮助手册,执行mvn source:help和mvn help:describe -Dplugin=source的结果差不多。

8.2.4     test-jar

  与generated-test-jar相同。

  8.2.5     test-jar-no-fork

引用帮助手册中的定义:

This goal bundles all the test sources into a jar archive. This goal functions the same as the test-jar goal but does not fork the build, and is suitable for attaching to the build lifecycle.

在生成Jar包的过程中,无论source插件的test-jar-no-fork(phase)是否与goal进行绑定,都会执行generate-test-resources之前的所有goal。若将test-jar-no-fork(phase)与default生命周期绑定时,会导致重复执行generate-test-resources之前的goal。

test-jar-no-fork会先去查看是否已绑定goal,若绑定后,不会重复执行。

8.2.6     jar

  与test-jar的用法基本相同,包含的内容不同

  8.2.7    jar-no-fork

  与test-jar-no-fork是同样的道理。

8.3   示例

查看maven-source-plugin-version.jar包下的plugins.xml会发现goal已经与default生命周期的phase绑定,所以无需重复绑定。可以看到jar(goal)默认绑定的是package(phase),所以执行mvn source:jar会执行package之前的生命周期。

9、resources

9.1   概念

引用帮助手册中的定义:

The resources plugin handles the copying of project resources to the output directory. There are two different kinds of resources: main resources and test resources. The difference is that the main resources are the resources associated to the main source code while the test resources are associated to the test source code. Thus, this allows the separation of resources for the main source code and its unit tests

resources插件主要用于把资源拷贝到编译输出路径。它与process-resources和process-test-resource两个goal绑定。

9.2  goals

resources有四个goal。 help, resources, testResources, copy-resources。help显示resources插件的帮助手册

9.2.1    resources

引用帮助手册的定义:

copy resources for the main source code to the main output directory. Always uses the project.build.resources element to specify the resources to copy

把主目录的源码拷贝到${project.build.resources}变量指定的目录中。

9.2.2     testResources

  与resources基本相同,是把测试目录的源码到${project.build.testResources}变量指定的目录中。

  9.2.3    copy-resources

引用帮助手册中的定义:

Copy resources of the configured plugin attribute resources

将一系列定义的resource拷贝到${outputDirectory}变量指定的目录中,其中resource包含include,exclude用于添加过滤条件。   

参考官方文档

原文地址:https://www.cnblogs.com/rain144576/p/14468764.html