zoukankan      html  css  js  c++  java
  • 开发一个mavn 的plugin

    官方文档:https://maven.apache.org/guides/plugin/guide-java-plugin-development.html

     _____________________________

     

     

    1. java -version: 11

    java version "11.0.9" 2020-10-20 LTS
    Java(TM) SE Runtime Environment 18.9 (build 11.0.9+7-LTS)
    Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.9+7-LTS, mixed mode)

    2. mvn -version :

    Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
    Maven home: /data/tools/apache-maven-3.6.3
    Java version: 11.0.9, vendor: Oracle Corporation, runtime: /data/tools/jdk-11.0.9
    Default locale: en_US, platform encoding: UTF-8
    OS name: "linux", version: "4.4.0-62-generic", arch: "amd64", family: "unix"

     源码:pom.xml

     <project>
       <modelVersion>4.0.0</modelVersion>
     
       <groupId>sample.plugin</groupId>
       <artifactId>hello-maven-plugin</artifactId>
       <version>1.0-SNAPSHOT</version>
       <packaging>maven-plugin</packaging>
     
       <name>Sample Parameter-less Maven Plugin</name>
     
       <dependencies>
         <dependency>
           <groupId>org.apache.maven</groupId>
           <artifactId>maven-plugin-api</artifactId>
           <version>3.0</version>
         </dependency>
     
         <!-- dependencies to annotations -->
         <dependency>
           <groupId>org.apache.maven.plugin-tools</groupId>
           <artifactId>maven-plugin-annotations</artifactId>
           <version>3.4</version>
           <scope>provided</scope>
         </dependency>
       </dependencies>
        <properties>
         <maven.compiler.source>1.6</maven.compiler.source>
         <maven.compiler.target>1.6</maven.compiler.target>
       </properties>
     </project>
                                                                                                                                       
    

      src/main/java/GreetingMojo.java

    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.22
    " );
        }
    }
    

    Introduction

    This guide is intended to assist users in developing Java plugins for Maven.

    Important Notice: Plugin Naming Convention and Apache Maven Trademark

    You will typically name your plugin <yourplugin>-maven-plugin.

    Calling it maven-<yourplugin>-plugin (note "Maven" is at the beginning of the plugin name) is strongly discouraged since it's a reserved naming pattern for official Apache Maven plugins maintained by the Apache Maven team with groupId org.apache.maven.plugins. Using this naming pattern is an infringement of the Apache Maven Trademark.

    Your First Plugin

    In this section we will build a simple plugin with one goal which takes no parameters and simply displays a message on the screen when run. Along the way, we will cover the basics of setting up a project to create a plugin, the minimal contents of a Java mojo which will define goal code, and a couple ways to execute the mojo.

    Your First Mojo

    At its simplest, a Java mojo consists simply of a single class representing one plugin's goal. There is no requirement for multiple classes like EJBs, although a plugin which contains a number of similar mojos is likely to use an abstract superclass for the mojos to consolidate code common to all mojos.

    When processing the source tree to find mojos, plugin-tools looks for classes with either @Mojo Java 5 annotation or "goal" javadoc annotation. Any class with this annotation are included in the plugin configuration file.

    A Simple Mojo

    Listed below is a simple mojo class which has no parameters. This is about as simple as a mojo can be. After the listing is a description of the various parts of the source.

    1. package sample.plugin;
    2.  
    3. import org.apache.maven.plugin.AbstractMojo;
    4. import org.apache.maven.plugin.MojoExecutionException;
    5. import org.apache.maven.plugins.annotations.Mojo;
    6.  
    7. /**
    8. * Says "Hi" to the user.
    9. *
    10. */
    11. @Mojo( name = "sayhi")
    12. public class GreetingMojo extends AbstractMojo
    13. {
    14. public void execute() throws MojoExecutionException
    15. {
    16. getLog().info( "Hello, world." );
    17. }
    18. }
    • The class org.apache.maven.plugin.AbstractMojo provides most of the infrastructure required to implement a mojo except for the execute method.
    • The annotation "@Mojo" is required and control how and when the mojo is executed.
    • The execute method can throw two exceptions:
      • org.apache.maven.plugin.MojoExecutionException if an unexpected problem occurs. Throwing this exception causes a "BUILD ERROR" message to be displayed.
      • org.apache.maven.plugin.MojoFailureException if an expected problem (such as a compilation failure) occurs. Throwing this exception causes a "BUILD FAILURE" message to be displayed.
    • The getLog method (defined in AbstractMojo) returns a log4j-like logger object which allows plugins to create messages at levels of "debug", "info", "warn", and "error". This logger is the accepted means to display information to the user. Please have a look at the section Retrieving the Mojo Logger for a hint on its proper usage.

    All Mojo annotations are described by the Mojo API Specification.

    Project Definition

    Once the mojos have been written for the plugin, it is time to build the plugin. To do this properly, the project's descriptor needs to have a number of settings set properly:

    groupId This is the group ID for the plugin, and should match the common prefix to the packages used by the mojos
    artifactId This is the name of the plugin
    version This is the version of the plugin
    packaging This should be set to "maven-plugin"
    dependencies A dependency must be declared to the Maven Plugin Tools API to resolve "AbstractMojo" and related classes

    Listed below is an illustration of the sample mojo project's pom with the parameters set as described in the above table:

    1. <project>
    2. <modelVersion>4.0.0</modelVersion>
    3.  
    4. <groupId>sample.plugin</groupId>
    5. <artifactId>hello-maven-plugin</artifactId>
    6. <version>1.0-SNAPSHOT</version>
    7. <packaging>maven-plugin</packaging>
    8.  
    9. <name>Sample Parameter-less Maven Plugin</name>
    10.  
    11. <dependencies>
    12. <dependency>
    13. <groupId>org.apache.maven</groupId>
    14. <artifactId>maven-plugin-api</artifactId>
    15. <version>3.0</version>
    16. </dependency>
    17.  
    18. <!-- dependencies to annotations -->
    19. <dependency>
    20. <groupId>org.apache.maven.plugin-tools</groupId>
    21. <artifactId>maven-plugin-annotations</artifactId>
    22. <version>3.4</version>
    23. <scope>provided</scope>
    24. </dependency>
    25. </dependencies>
    26. </project>

    Building a Plugin

    There are few plugins goals bound to the standard build lifecycle defined with the maven-plugin packaging:

    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

    For more details, you can look at detailed bindings for maven-plugin packaging.

    Executing Your First Mojo

    The most direct means of executing your new plugin is to specify the plugin goal directly on the command line. To do this, you need to configure the hello-maven-plugin plugin in you project:

    1. ...
    2. <build>
    3. <plugins>
    4. <plugin>
    5. <groupId>sample.plugin</groupId>
    6. <artifactId>hello-maven-plugin</artifactId>
    7. <version>1.0-SNAPSHOT</version>
    8. </plugin>
    9. </plugins>
    10. </build>
    11. ...

    And, you need to specify a fully-qualified goal in the form of:

    1. mvn groupId:artifactId:version:goal

    For example, to run the simple mojo in the sample plugin, you would enter "mvn sample.plugin:hello-maven-plugin:1.0-SNAPSHOT:sayhi" on the command line.

    Tipsversion is not required to run a standalone goal.

    Shortening the Command Line

    There are several ways to reduce the amount of required typing:

    • If you need to run the latest version of a plugin installed in your local repository, you can omit its version number. So just use "mvn sample.plugin:hello-maven-plugin:sayhi" to run your plugin.
    • You can assign a shortened prefix to your plugin, such as mvn hello:sayhi. This is done automatically if you follow the convention of using ${prefix}-maven-plugin (or maven-${prefix}-plugin if the plugin is part of the Apache Maven project). You may also assign one through additional configuration - for more information see Introduction to Plugin Prefix Mapping.
    • Finally, you can also add your plugin's groupId to the list of groupIds searched by default. To do this, you need to add the following to your ${user.home}/.m2/settings.xml file:
      1. <pluginGroups>
      2. <pluginGroup>sample.plugin</pluginGroup>
      3. </pluginGroups>

    At this point, you can run the mojo with "mvn hello:sayhi".

    Attaching the Mojo to the Build Lifecycle

    You can also configure your plugin to attach specific goals to a particular phase of the build lifecycle. Here is an example:

    1. <build>
    2. <plugins>
    3. <plugin>
    4. <groupId>sample.plugin</groupId>
    5. <artifactId>hello-maven-plugin</artifactId>
    6. <version>1.0-SNAPSHOT</version>
    7. <executions>
    8. <execution>
    9. <phase>compile</phase>
    10. <goals>
    11. <goal>sayhi</goal>
    12. </goals>
    13. </execution>
    14. </executions>
    15. </plugin>
    16. </plugins>
    17. </build>

    This causes the simple mojo to be executed whenever Java code is compiled. For more information on binding a mojo to phases in the lifecycle, please refer to the Build Lifecycle document.

  • 相关阅读:
    如何编写Robot Framework测试用例1---(基本格式篇)
    如何编写Robot Framework测试用例2---(测试用例语法1)
    使用RobotFramework的DataBaseLibrary(Java实现)
    Python fabric远程自动部署简介
    Python之路【第二十三篇】:Django 初探--Django的开发服务器及创建数据库(笔记)
    Python之路【第二十二篇】:Django之Model操作
    Python之路【第二十一篇】:Django之Form组件
    第十一篇:web之Django之Form组件
    第十篇:web之前端之django一些feature
    第九篇:web之前端之web上传文件的方式
  • 原文地址:https://www.cnblogs.com/oxspirt/p/14173723.html
Copyright © 2011-2022 走看看