maven插件(2)-plugin编写

1 新建java普通的jar项目

2 添加maven-plugin的pom依赖


    
        junit
        junit
        test
    

    
        org.apache.maven
        maven-plugin-api
        3.5.2
    

    
        org.apache.maven
        maven-core
        3.5.2
    

    
        org.apache.maven.plugin-tools
        maven-plugin-annotations
        3.5.2
        provided
    

    
        com.alibaba
        fastjson
        1.2.28
    

3 编写插件代码

import org.apache.maven.execution.MavenSession;
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;
import org.apache.maven.project.MavenProject;
import java.util.List;

@Mojo(name="compile", defaultPhase= LifecyclePhase.COMPILE)
public class MavenPluginCompileMojo extends AbstractMojo {

    @Parameter
    private String describe;

    @Parameter
    private List extentParams;

    @Parameter
    private String path;

    @Parameter(defaultValue="${project}", readonly=true, required=true)
    private MavenProject project;

    @Parameter(defaultValue="${session}", readonly=true, required=true)
    private MavenSession session;

    /**
     * POM属性
     * 项目构建输出目录: 默认target/
     */
    @Parameter(defaultValue="${project.build.directory}", required=true)
    private String buildDirectory;

    /**
     * POM属性
     * 项目主源码目录: 默认 src/mian/java
     */
    @Parameter(defaultValue="${project.build.sourceDirectory}", readonly=true)
    private String sourceDirectory;

    /**
     * POM属性
     * 项目代码编译输出目录: 默认 target/classes/
     */
    @Parameter(defaultValue="${project.build.outputDirectory}", readonly=true)
    private String outputDirectory;

    /**
     * POM属性
     * 打包输出项目名: 默认 ${project.artifactId}-${project.version}
     */
    @Parameter(defaultValue="${project.build.finalName}", readonly=true)
    private String finalName;

    /**
     * POM属性
     */
    @Parameter(defaultValue="${project.build.sourceEncoding}",readonly = true)
    private String resourceEncoding;

    /**
     * POM属性
     */
    @Parameter(defaultValue="${project.groupId}", readonly=true)
    private String groupId;

    /**
     * POM属性
     */
    @Parameter(defaultValue="${project.artifactId}", readonly=true)
    private String artifactId;

    /**
     * POM属性
     * 项目版本
     */
    @Parameter(defaultValue = "${project.version}",readonly = true)
    private String projectVersion;

    /**
     * 内置属性
     * 项目根目录
     */
    @Parameter(defaultValue = "${basedir}",readonly = true)
    private String projectBaseDir;

    /**
     *自定义属性
     *
     *
     *   true
     *
     *
     */
    @Parameter(property="jar.useDefault", defaultValue="false")
    private boolean useDefault;

    /**
     * java 系统属性
     *
     * 备注:mvn help:system
     */
    @Parameter(defaultValue = "${user.home}",readonly = true)
    private String javaUserHome;

    /**
     * java 系统属性
     *
     * 备注:mvn help:system
     */
    @Parameter(defaultValue = "${java.home}",readonly = true)
    private String javaHome;

    /**
     * 环境变量
     *
     * 备注:mvn help:system
     */
    @Parameter(defaultValue = "${env.JAVA_HOME}",readonly = true)
    private String envJavaHome;

    @Parameter(defaultValue = "${env.MAVEN_HOME}",readonly = true)
    private String envMavenHome;

    /**
     * 执行入口
     * 调试:mvn Debug cn.chy:mavenplugindemo:2.0-SNAPSHOT:compile
     * @throws MojoExecutionException
     * @throws MojoFailureException
     */
    public void execute() throws MojoExecutionException, MojoFailureException {
        getLog().info("MavenPluginCompileMojo | compile");

        getLog().info("=======java系统属性=========>");
        getLog().info("javaUserHome" + javaUserHome);
        getLog().info("javaHome:" + javaHome);

        getLog().info("=======环境变量=========>");
        getLog().info("JAVA_HOME" + envJavaHome);
        getLog().info("MAVEN_HOME:" + envMavenHome);

        getLog().info("=======maven POM属性=========>");
        getLog().info("buildDirectory:" + buildDirectory);
        getLog().info("sourceDirectory:" + sourceDirectory);
        getLog().info("outputDirectory:" + outputDirectory);
        getLog().info("resourceEncoding:" + resourceEncoding);
        getLog().info("finalName:" + finalName);
        getLog().info("groupId:" + groupId);
        getLog().info("artifactId:" + artifactId);

        getLog().info("======maven 内置属性==========>");
        getLog().info("projectBaseDir:" + projectBaseDir);
        getLog().info("projectVersion:" + projectVersion);

        getLog().info("======maven 自定义属性==========>");
        getLog().info("useDefault:" + useDefault);

        getLog().info("======maven plugin 参数==========>");
        getLog().info("describe:" + describe);
        if (extentParams != null) {
            for (String string : extentParams) {
                getLog().info("extentParams:" + string);
            }
        }
        getLog().info("path:" + path);
    }
}

4 插件打包上传maven仓库

5 新建test项目引用maven插件


    
        
            cn.chy
            mavenplugindemo
            2.0-SNAPSHOT
            
            
                this is maven plugin demo
                
                    1
                    2
                
                ${basedir}/src/main/java
            
            
                
                    
                    compileid
                    
                    compile
                    
                        compile
                    
                    
                    
                        this is maven plugin compile demo
                        
                            3
                            4
                        
                    
                                
            
        
    

6 test项目执行maven compile

maven插件(2)-plugin编写_第1张图片

你可能感兴趣的:(Maven)