zoukankan      html  css  js  c++  java
  • 自定义maven插件

    之前虽然一直知道maven插件是可以自定义的,不过一致没有用过。最近接触到了swagger项目中的codegen自动生成代码的功能,并且在codegen源码中,也是存在maven插件功能的,所以自己就尝试着自定义一个maven插件。并且从网上搜了一些资料自己写了一个demo。防止自己忘记,所以记录下来,哈哈哈!


    • maven插件工程

    新建一个maven工程,名字为maven-plugin-demo1


    maven????.png

    这里注意的是,打包方式packaging为maven-plugin。

    下面是我的maven的pom文件

    <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>site.wangxin520</groupId>
      <artifactId>maven-plugin-demo1</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <build>
    <!-- 这里注意的是,需要引用一个maven的plugin-plugin插件,并且配置好execution,这个是必须要有的,不然没用 -->   <plugins>   <plugin>   <groupId>org.apache.maven.plugins</groupId>   <artifactId>maven-plugin-plugin</artifactId>   <version>3.2</version>   <executions>   <execution>   <goals>   <goal>descriptor</goal>   </goals>   <phase>generate-resources</phase>   </execution>   </executions>   </plugin>   </plugins>
    <!-- 这里是用来防止eclipes报错的 -->   <pluginManagement>   <plugins>   <!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->   <plugin>   <groupId>org.eclipse.m2e</groupId>   <artifactId>lifecycle-mapping</artifactId>   <version>1.0.0</version>   <configuration>   <lifecycleMappingMetadata>   <pluginExecutions>   <pluginExecution>   <pluginExecutionFilter>   <groupId>   org.apache.maven.plugins   </groupId>
    <artifactId>   maven-plugin-plugin   </artifactId>   <versionRange>   [3.2,)   </versionRange>   <goals>   <goal>descriptor</goal>   </goals>   </pluginExecutionFilter>   <action>   <ignore></ignore>   </action>   </pluginExecution>   </pluginExecutions>   </lifecycleMappingMetadata>   </configuration>   </plugin>   </plugins>   </pluginManagement>  </build>  <!-- 注意打包方式 -->  <packaging>maven-plugin</packaging>

    <!-- 需要添加依赖,一个注解用的,一个是api用的 -->  <dependencies>   <dependency>   <groupId>org.apache.maven.plugin-tools</groupId>   <artifactId>maven-plugin-annotations</artifactId>   <version>3.2</version>   </dependency>   <dependency>   <groupId>org.apache.maven</groupId>   <artifactId>maven-plugin-api</artifactId>   <version>3.3.9</version>   </dependency>  </dependencies> </project>

    当配置好maven的pom文件后,下面就开始编写一个自定义的插件java类。

    这里需要注意的是该自定义插件类,需要继承AbstractMojo.java,然后实现其中的execute方法

    package site.wangxin520.maven.plugin.demo.test1;
    
    import java.util.List;
    
    import org.apache.maven.plugin.AbstractMojo;
    import org.apache.maven.plugin.MojoExecutionException;
    import org.apache.maven.plugin.MojoFailureException;
    import org.apache.maven.plugins.annotations.LifecyclePhase;
    import org.apache.maven.plugins.annotations.Mojo;
    import org.apache.maven.plugins.annotations.Parameter;
    
    /**
     * 
     * @author 王鑫
     * 
     *         该类就是maven自定义插件类
     *
     */
    // mojo注解就是maven插件的注解,具体什么我忘记了。name就是后面使用该插件的时候excuation里面的,后面配置的是生命周期,我这里配置了install,即默认是安装时候执行本插件
    @Mojo(name = "mojoDemo1", defaultPhase = LifecyclePhase.INSTALL)
    public class MojoDemo1 extends AbstractMojo {
    
    	// 配置的是本maven插件的配置,在pom使用configration标签进行配置 property就是名字,在配置里面的标签名字。在调用该插件的时候会看到
    	@Parameter(property = "name")
    	private String name;
    
    	@Parameter(property = "modules")
    	private List<String> modules;
    // 执行的相关代码,即插件执行的语句。 @Override public void execute() throws MojoExecutionException, MojoFailureException { System.out.println("!!!!!!!!!!!!!!!!!!!!"); System.out.println(name); System.out.println("~~~~~~~~~~~~~~~~~~~~"); System.out.println(modules); for (String string : modules) { System.out.println(string); } System.out.println("!!!!!!!!!!!!!!!!!!!!"); } }

    当着一切都弄好后,就执行maven install,将本插件安装到本地的maven仓库中。方便后面调用。

    这里需要注意一下,maven插件的配置参数什么的,可以在maven官网看到。这里就不多做赘述了,后面我会加上连接。


    • maven插件测试工程

    创建maven工程maven-plugin-test

    maven?plugin????.png

    为了方便后面使用,我弄成了组合工程,其中有两个模块。

    <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>site.wangxin520</groupId>
    	<artifactId>maven-plugin-test</artifactId>
    	<version>0.0.1-SNAPSHOT</version>
    	<packaging>pom</packaging>
    	<modules>
    		<module>maven-plugin-part1</module>
    		<module>maven-plugin-part2</module>
    	</modules>
    	<!-- 配置插件 -->
    <build> <plugins> <plugin> <!-- 这里写刚刚创建的maven插件的groupid和artifactid --> <groupId>site.wangxin520</groupId> <artifactId>maven-plugin-demo1</artifactId> <version>0.0.1-SNAPSHOT</version> <executions> <execution>    <!-- goals里面配置的就是刚刚在@mojo里面的name --> <goals> <goal>mojoDemo1</goal> </goals> <!-- 这里加入的是相关参数 --> <configuration> <name>wangxin</name> <modules>${project.modules}</modules> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>


    上面就是添加插件的配置。

    下面就进行安装了。就可以执行自定义maven配置中的excute方法

    QQ20180214-205806@2x.png

    以上就是maven自定义插件的配置和方法。是不是很简单!

    转载自Nixgnaw:www.wangxin520.site
  • 相关阅读:
    MYSQL limit用法
    mybaties mapping中if
    执行数据库同时又调接口
    WITH (NOLOCK)
    SpringMVC转发和重定向区别!
    MyBatis的foreach语句详解
    SSM mapper.xml
    win7与virtualbox中centos文件共享
    PBOC2.0中消费交易流程
    PBOC2.0协议中电子存折/电子钱包中圈存交易流程
  • 原文地址:https://www.cnblogs.com/wangxinblog/p/8654400.html
Copyright © 2011-2022 走看看