其他分享
首页 > 其他分享> > maven plugin 简单学习(2021-05-29)

maven plugin 简单学习(2021-05-29)

作者:互联网

目录

maven plugin 开发: https://maven.apache.org/plugin-developers/index.html

什么是plugin?

“Maven” is really just a core framework for a collection of Maven Plugins. In other words, plugins are where much of the real action is performed, plugins are used to: create jar files, create war files, compile code, unit test code, create project documentation, and on and on. Almost any action that you can think of performing on a project is implemented as a Maven plugin.

什么是Mojo?

A Mojo is really just a goal in Maven, and plug-ins consist of any number of goals (Mojos). Mojos can be defined as annotated Java classes or Beanshell script. A Mojo specifies metadata about a goal: a goal name, which phase of the lifecycle it fits into, and the parameters it is expecting.

MOJO is a play on POJO (Plain-old-Java-object), substituting “Maven” for “Plain”. Mojo is also an interesting word (see definition). From Wikipedia, a “mojo” is defined as: “…a small bag worn by a person under the clothes (also known as a mojo hand). Such bags were thought to have supernatural powers, such as protecting from evil, bringing good luck, etc.”

mojo理解为一个执行目标,maven本身就是很多mojo组成的。mojo实现plugin,让用户自已在compileprocess-classed,test,package,install, deploy等阶段实现自定的执行逻辑

maven plugin 的标准阶段介绍

阶段含义
compileCompiles the Java code for the plugin
process-classesExtracts data to build the plugin descriptor
testRuns the plugin’s unit tests
packageBuilds the plugin jar
installInstalls the plugin jar in the local repository
deployDeploys the plugin jar to the remote repository

@Mojo的使用参考:https://maven.apache.org/developers/mojo-api-specification.html#The_Descriptor_and_Annotations

简单的定义plugin和使用plugin

如下就能简单的定义一个plugin的逻辑

package sample.plugin;
 
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Mojo;
 
/**
 * Says "Hi" to the user.
 *
 */
@Mojo( name = "sayhi")
public class GreetingMojo extends AbstractMojo
{
    public void execute() throws MojoExecutionException
    {
        getLog().info( "Hello, world." );
    }
}

plugin的pom依赖

<dependencies>
       <!-- https://mvnrepository.com/artifact/org.apache.maven/maven-plugin-api -->
       <dependency>
           <groupId>org.apache.maven</groupId>
           <artifactId>maven-plugin-api</artifactId>
           <version>3.8.1</version>
       </dependency>

       <!-- https://mvnrepository.com/artifact/org.apache.maven.plugin-tools/maven-plugin-annotations -->
       <dependency>
           <groupId>org.apache.maven.plugin-tools</groupId>
           <artifactId>maven-plugin-annotations</artifactId>
           <version>3.6.1</version>
           <scope>provided</scope>
       </dependency>

   </dependencies>

在另一个工程模块中的pom.xml引入,然后就能使用该plugin了

 <build>
      <plugins>
           <plugin>
               <groupId>org.example</groupId>
               <artifactId>hello-maven-plugin</artifactId>
               <version>1.0-SNAPSHOT</version>
           </plugin>
       </plugins>
   </build>

参考:https://maven.apache.org/guides/plugin/guide-java-report-plugin-development.html

plugin重要功能之parameter

关于参数的使用介绍:https://maven.apache.org/guides/plugin/guide-java-plugin-development.html#parameters

It is unlikely that a mojo will be very useful without parameters. Parameters provide a few very important functions:

  1. It provides hooks to allow the user to adjust the operation of the plugin to suit their needs.
  2. It provides a means to easily extract the value of elements from the POM without the need to navigate the objects.

使用例子:

/**
   * The greeting to display.
    */
   @Parameter( property = "sayhi.greeting", defaultValue = "Hello World!" )
   private String greeting;
<plugin>
  <groupId>sample.plugin</groupId>
  <artifactId>hello-maven-plugin</artifactId>
  <version>1.0-SNAPSHOT</version>
  <configuration>
  	<!--定义greeting参数的值-->
    <greeting>Welcome</greeting>
  </configuration>
</plugin>

需要说明的是,可以在plugin中使用getLog() 来打印 log 已方便调试或观察plugin执行阶段情况

parameter的类型是丰富的,eg:

boolean、Numbers、Dates、Files and Directories、URLs、Plain Text、Enums,Types With Multiple Values、
Other Object Classes

即我们可以自己定义参数类型来极大的方便了我们在构建jar的灵活性

指定plugin执行的阶段

例如指定默认deploy阶段完成某个Mojo:

@Mojo(name = "deploy", defaultPhase = LifecyclePhase.DEPLOY,
    requiresDependencyResolution = ResolutionScope.RUNTIME)
public class DeployMojo extends AbstractMojo {
	//...
}

参考:https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html

标签:plugin,05,jar,29,maven,apache,org,Mojo
来源: https://blog.csdn.net/qq_26437925/article/details/117385250