yangyang12138

导航

mvn插件开发

一、pom设置
  packaging必须是maven-plugin形式

  <packaging>maven-plugin</packaging>

  必须添加依赖

<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>2.0</version>
</dependency>
<?xml version="1.0" encoding="UTF-8"?>
<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>org.example</groupId>
    <artifactId>XXX</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>maven-plugin</packaging>

    <dependencies>
        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-plugin-api</artifactId>
            <version>2.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.shared</groupId>
            <artifactId>maven-dependency-tree</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.shared</groupId>
            <artifactId>maven-common-artifact-filters</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.shared</groupId>
            <artifactId>maven-artifact-transfer</artifactId>
            <version>0.11.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.shared</groupId>
            <artifactId>maven-shared-utils</artifactId>
            <version>3.3.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.plugin-tools</groupId>
            <artifactId>maven-plugin-annotations</artifactId>
            <version>3.5.2</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.plexus</groupId>
            <artifactId>plexus-utils</artifactId>
            <version>3.3.0</version>
        </dependency>
    </dependencies>


</project>

二、开发过程

  开发过程主要是实现mojo类,首先必须继承AbstractMojo
  

package org.apache.maven.plugin;

import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugin.logging.SystemStreamLog;

import java.util.Map;

public abstract class AbstractMojo
    implements Mojo, ContextEnabled
{
    private Log log;
    private Map pluginContext;

    public void setLog( Log log )
    {
        this.log = log;
    }

    public Log getLog()
    {
        if ( log == null )
        {
            log = new SystemStreamLog();
        }

        return log;
    }

    public Map getPluginContext()
    {
        return pluginContext;
    }

    public void setPluginContext( Map pluginContext )
    {
        this.pluginContext = pluginContext;
    }
    
}

其中getLog打印的info就是maven执行时打印在控制台的信息,debug信息只有在执行maven时加了-X参数的才能在控制台显示

三、注解

  

@Mojo( name = "scopes-copy", requiresDependencyCollection = ResolutionScope.TEST, threadSafe = true )
name就是配置plugin时的那个goal
@Parameter( property = "outputDirectory", defaultValue = "${project.build.directory}/dev" )
private String outputDirctory;
property的值是配置plugin时设置的参数


依赖树获取
@Parameter( defaultValue = "${project}", readonly = true, required = true )
    private MavenProject project;

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

    @Parameter( defaultValue = "${reactorProjects}", readonly = true, required = true )
    private List<MavenProject> reactorProjects;

    @Component( hint = "default" )
    private DependencyGraphBuilder dependencyGraphBuilder;



    @Parameter( property = "outputDirectory", defaultValue = "${project.build.directory}/dev" )
    private String outputDirctory;


    Set<Artifact> targetArtifact = new HashSet<>();

    @Override
    public void execute() throws MojoExecutionException, MojoFailureException {

        targetArtifact.clear();

        if(session == null){
            getLog().info("session is null!!!");
            throw new MojoFailureException("session is null!!!");
        }

        ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest( session.getProjectBuildingRequest() );
        buildingRequest.setProject( project );

        try {
            DependencyNode rootNode = dependencyGraphBuilder.buildDependencyGraph( buildingRequest, new ScopeArtifactFilter("test"),
                    reactorProjects );

            getLog().info(rootNode.getArtifact().getGroupId() + ":" + rootNode.getArtifact().getArtifactId());

            getLog().info("children size is " + rootNode.getChildren().size());
        } catch (DependencyGraphBuilderException | IOException e) {
            getLog().info(e.getMessage());
            throw new MojoExecutionException(e.getMessage(),e);
        }
    }

 

 

 

 

  


  

posted on 2020-11-11 19:18  杨杨09265  阅读(176)  评论(0编辑  收藏  举报