欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

maven插件_Maven插件

程序员文章站 2022-06-01 12:46:19
...

maven插件

Maven插件 (Maven Plugins)

Plugin in maven is the one of the vital feature that is basically used to reuse the common build logic across different projects. Plugins are the ones through which all the tasks like compiling code, testing them with the junits, creating jar/war/ear files and documentation of the projects are carried out. Most of the work in maven is done using plugins, since the dependencies (jar files) are added only to the classpath while executing tasks.

maven插件是一项重要功能,基本上可用于在不同项目之间重用通用构建逻辑。 插件是用来执行所有任务的工具,例如编译代码,使用junit测试它们,创建jar / war / ear文件和项目文档。 由于依赖项(jar文件)仅在执行任务时添加到类路径中,因此maven中的大多数工作都是使用插件完成的。

So, maven is nothing but a plugin execution framework in which every tasks are accomplished by the usage of plugins.

因此,maven就是一个插件执行框架,其中每个任务都通过使用插件来完成。

Below are the basic list of tasks that are handled by plugins in maven :

以下是由maven中的插件处理的基本任务列表:

  • Creating jar/war/ear files.

    创建jar / war / ear文件。
  • Code compilation

    代码编译
  • Unit testing of the code.

    代码的单元测试。
  • Project documentation

    项目文件

Behavior of any plugin can be changed using a set of parameters exposed by each plugin goal (also known as Mojo). A Mojo in maven is just a goal, basically specifies the metadata of a goal - goal name, which phase it fits into and parameters expected by the goal.

可以使用每个插件目标(也称为Mojo)公开的一组参数来更改任何插件的行为。 Maven中的Mojo只是一个目标,基本上指定了目标的元数据-目标名称,该阶段适合目标的阶段以及目标期望的参数。

Maven插件的类型 (Types of Maven Plugins)

Basically 2 important types of plugins exist in maven as listed below :

基本上,Maven中存在两种重要的插件类型,如下所示:

  1. Build Plugins - Basically these plugins will execute during the build phase. These plugins are defined under the 构建插件 -基本上,这些插件将在构建阶段执行。 这些插件在pom.xml的<build> element in pom.xml<build>元素下定义
  2. Report Plugins - These plugins are executed during the site generation (report or javadocs generation) phase. These plugins will be configured under the 报告插件 -这些插件在网站生成(报告或javadocs生成)阶段执行。 这些插件将在pom.xml中的<reporting> element in pom.xml<reporting>元素下配置。

一些常见的Maven插件 (Some Common Maven Plugins)

Below are list of commonly used plugins in any java projects built using maven :

以下是使用maven构建的任何Java项目中的常用插件列表:

Plugin name Description
clean Used to delete the target directory in order to clean up the earlier build artifacts
compiler To compile java source files
surefile Executes the Junit tests and generate the reports.
jar Build the jar file of the project
war Build the war file of the project
javadoc Generates the javadocs of the project
antrun Runs a set of ant tasks specified in any stage/phase of the build
插件名称 描述
清洁 用于删除目标目录以清理较早的构建工件
编译器 编译Java源文件
确定文件 执行Junit测试并生成报告。
生成项目的jar文件
战争 建立项目的战争档案
Java文档 生成项目的javadocs
安特伦 运行在构建的任何阶段/阶段中指定的一组ant任务

pom.xml中的示例插件 (Sample plugin in pom.xml)

<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.orgname.orggroup</groupId>
  <artifactId>project</artifactId>
  <version>1.0</version>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>1.1</version>
        <executions>
          <execution>
            <id>id.clean</id>
            <phase>clean</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <tasks>
                <echo>Clean Phase</echo>
              </tasks>
            </configuration>
          </execution>     
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

  • Each plugin can have more than one goals to perform different set of tasks accordingly.

    每个插件可以有多个目标,以相应地执行不同的任务集。
  • Starting phase of any plugin can be defined.

    可以定义任何插件的开始阶段。
  • Tasks can be configured associated to each goal of the plugin defined.

    可以将任务配置为与定义的插件的每个目标相关联。

Maven插件的好处 (Benefits of Maven Plugins)

  • More detailed and well architecture.

    更详细和完善的架构。
  • A managed life cycle

    受控的生命周期
  • Implementation of multiple languages

    多种语言的实现
  • Reusability of the common build logic.

    通用构建逻辑的可重用性。
  • Ability to write maven plugins completely in java

    能够完全用Java编写Maven插件

翻译自: https://www.studytonight.com/maven/maven-plugins

maven插件