当前位置:   article > 正文

maven plugin 简单介绍和实战 (2021-05-29)

maven plugin

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 的几个标准阶段

阶段 含义
compile Compiles the Java code for the plugin
process-classes Extracts data to build the plugin descriptor
test Runs the plugin’s unit tests
package Builds the plugin jar
install Installs the plugin jar in the local repository
deploy Deploys 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." );
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/216778
推荐阅读
相关标签
  

闽ICP备14008679号