ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2021-05-29 15:01:20  阅读:200  来源: 互联网

标签:plugin 05 jar 29 maven apache org Mojo


目录

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.

  • plugin的作用

    1. create jar files (创建jar)
    2. create war files(创建war)
    3. compile code (编译源代码)
    4. unit test code (单元测试)
    5. create project documentation (创建项目文档)
    6. on and on(等等…)

什么是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

  • 可以自定义一些丰富的parameter:比如做生成jar和后续解析jar的操作,来适应丰富的变化。

关于参数的使用介绍: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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有