Maven Custom Maven plugins

1 Introduction

We often when writing pom, it will build in a few inside with a variety of plugin, the plugin is a maven plugin, they are often called Mojo, the role of these plug-ins is in the process of building a project, perform some action, behavior, such as using the maven-jar-plugin plugin is used for packaging jar. As another example, when we use spring boot, why perform mvn spring-boot: run time, maven will do some spring-boot is built, because we are equipped with spring-boot-maven-plugin in the build inside plug-in, and execute the run classes. Here we customize a mojo.

 

2. Plug-naming rules

Before we begin to create a custom plug-in, let's take a look at the naming, in fact, from what name can officially recommended xxx-maven-plugin, see the following example

But there is one thing, when they need to execute a command, we must write: Group name: Module name: version: Running Name

 

Write no problem, just a little tedious, but if we end plug-ins for -maven-plugin then we can use to simplify the wording, and achieve the same effect: [module name removed -maven-plugin]: Run Name

 

3. Start writing custom components

Through all kinds of IDE, if the eclipse, or idea can create a maven project, the choice of modules, they can choose mojo module, IDE will quickly help you establish a mojo development environment in the development of inside pom pom is a simple structure, but if you want to develop mojo, we also need to introduce two plug-ins, is a maven-plugin-api, is a maven-plugin-annotations, spend two plug-ins, we can write belong to our custom plug-ins

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.test.maven</groupId>
  <artifactId>my-maven-plugin</artifactId>
  <packaging>maven-plugin</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>my-maven-plugin Maven Mojo</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>org.apache.maven</groupId>
      <artifactId>maven-plugin-api</artifactId>
      <version>2.0</version>
    </dependency>
    <dependency>
      <groupId>org.apache.maven.plugin-tools</groupId>
      <artifactId>maven-plugin-annotations</artifactId>
      <version>3.1</version>
    </dependency>
  </dependencies>
</project>

 

Here is a java implementation of the class, is we want to execute a write operation, where the need to inherit AbstractMojo class, and override the execute method, which is the logic we want to implement the specific, the key point here is the comment, we explained in detail later

package com.test.maven;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Execute;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

/**
 * Goal which touches a timestamp file.
 *
 * @goal touch
 * 
 * @phase process-sources
 */
@Mojo(name="hello",defaultPhase = LifecyclePhase.CLEAN)
public class MyMojo extends AbstractMojo
{


    @Parameter
    private String test;

    public void execute() throws MojoExecutionException {
       System.out.println(" ---------->> Hello My Plugins = " + test);
    }
}

 

After writing these two things, we can put this plugin installed to the repository, execute mvn install. Other projects so that we can use this plug-in to a custom of some of our operations on the project

 

pom other projects, other projects cited in this plugin as usual, write the information on the plug-in build inside

<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.test.maven.phase</groupId>
  <artifactId>my-maven-test-phase</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <build>
      <plugins>
      <plugin>
        <groupId>com.test.maven</groupId>
        <artifactId>my-maven-plugin</artifactId>
        <version>1.0-SNAPSHOT</version>
      </plugin>
    </plugins>
  </build>
</project>

 

Then run, hello corresponding here it is above our custom component inside @Mojo name attribute: mvn my: hello

 

4. The custom component properties described

Below find a few examples to illustrate, in two files, one of which is a component annotation, a project which is written pom configuration

 

Example 1:

//组件组件
@Mojo(name="hello")

 

<!-- 项目配置 如果这里不配置执行目标,或者执行所在的生命周期,执行后是不会产生效果的 -->
<
build> <plugins> <plugin> <groupId>com.test.maven</groupId> <artifactId>my-maven-plugin</artifactId> <version>1.0-SNAPSHOT</version> <executions> <execution> <goals> <!-- 执行目标 --> <goal>hello</goal> </goals> <!-- 执行这个目标所在的生命周期 --> <phase>clean</phase> </execution> </executions> <configuration> <test>peter</test> </configuration> </plugin> </plugins> </build>

 

例子2:

//自定义组件,名称为hello, 默认执行周期 clean, 
@Mojo(name="hello",defaultPhase = LifecyclePhase.CLEAN)

 

<!-- 这里就不需要想例子一当中 在pom里面指定生命周期的执行阶段了 -->
<
build> <plugins> <plugin> <groupId>com.test.maven</groupId> <artifactId>my-maven-plugin</artifactId> <version>1.0-SNAPSHOT</version> <executions> <execution> <goals> <!-- 执行目标 --> <goal>hello</goal> </goals> </execution> </executions> </plugin> </plugins> </build>

 

例子3: 

//声明参数,用于pom注入参数
@Parameter
private String test;

 

<build>
      <plugins>
      <plugin>
        <groupId>com.test.maven</groupId>
        <artifactId>my-maven-plugin</artifactId>
        <version>1.0-SNAPSHOT</version>
        <executions>
            <execution>
                <goals>
                    <!-- 执行目标 -->
                    <goal>hello</goal>
                </goals>
            </execution>
        </executions>
     <!-- 这里对应的test节点,就是对应的自定义组件中,参数名 里面的值就是要注入的值,使用场景是,当需要注入目标项目的相对路径或者项目名称等等一些信息 -->
     <configuration> <test>peter</test> </configuration> </plugin> </plugins> </build>

 

Guess you like

Origin www.cnblogs.com/oscar1987121/p/10959083.html