Maven插件可以在作为构建一部分创建的JAR上运行吗

时间:2019-03-08 13:24:23

标签: java maven maven-plugin

我想创建一个Maven插件,该插件仅在发布jar版本后才能运行。目的是识别JAR中所有使用自定义注释进行注释的类。然后,它将使用这些类的详细信息调用REST API。

我一直在寻找使用javaparser扫描源代码。是否可以从Maven插件检查生成的jar文件。这样,我可以使用Reflections库(getTypesAnnotatedWith),这会使过程变得更容易。

编辑,我曾经尝试过使用Aether,但是遇到了问题

package mypackage.maven.plugins;

import java.net.MalformedURLException;
import java.util.Set;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.apache.maven.repository.internal.MavenRepositorySystemUtils;
import org.eclipse.aether.DefaultRepositorySystemSession;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.artifact.DefaultArtifact;
import org.eclipse.aether.collection.CollectRequest;
import org.eclipse.aether.collection.DependencyCollectionException;
import org.eclipse.aether.connector.basic.BasicRepositoryConnectorFactory;
import org.eclipse.aether.graph.Dependency;
import org.eclipse.aether.graph.DependencyNode;
import org.eclipse.aether.impl.DefaultServiceLocator;
import org.eclipse.aether.repository.LocalRepository;
import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.aether.resolution.ArtifactRequest;
import org.eclipse.aether.resolution.ArtifactResolutionException;
import org.eclipse.aether.resolution.ArtifactResult;
import org.eclipse.aether.spi.connector.RepositoryConnectorFactory;
import org.eclipse.aether.spi.connector.transport.TransporterFactory;
import org.eclipse.aether.transport.file.FileTransporterFactory;
import org.eclipse.aether.transport.http.HttpTransporterFactory;
import org.reflections.Reflections;
import org.reflections.scanners.Scanner;
import org.reflections.scanners.SubTypesScanner;
import org.reflections.scanners.TypeAnnotationsScanner;
import org.reflections.util.ConfigurationBuilder;

import my.package.annotations.MyAnnotation;

@Mojo(name = "identify-checks")
public class MyCheckMojo extends AbstractMojo
{
    @Parameter(defaultValue = "${project}", required = true, readonly = true)
    MavenProject project;

    @Override
    public void execute() throws MojoExecutionException, MojoFailureException
    {                        
        RepositorySystem repoSystem = newRepositorySystem();

        RepositorySystemSession session = newSession( repoSystem );
        String coords = project.getGroupId() + ":" + project.getArtifactId() + ":" + project.getVersion();


        Dependency dependency = new Dependency( new DefaultArtifact(coords), "compile" );
        RemoteRepository central = new RemoteRepository.Builder( "central", "default", "http://myrepo:8081/nexus/content/repositories/" ).build();

        CollectRequest collectRequest = new CollectRequest();
        collectRequest.setRoot( dependency );
        collectRequest.addRepository( central );
        DependencyNode node;
        try
        {            
            node = repoSystem.collectDependencies( session, collectRequest ).getRoot();

            Artifact artifact = node.getArtifact();
            artifact.getArtifactId();

            ArtifactRequest artifactRequest = new ArtifactRequest();
            artifactRequest.setArtifact(artifact);
            ArtifactResult artifactResult = repoSystem.resolveArtifact(session, artifactRequest);
            artifact = artifactResult.getArtifact();

            ConfigurationBuilder configBuilder = new ConfigurationBuilder()
                    .addUrls(artifact.getFile().toURI().toURL())
                    .filterInputsBy(new FilterBuilder().include("my.package"))
                    .setScanners(new SubTypesScanner(false), new TypeAnnotationsScanner());

            Set<Scanner> scanners = configBuilder.getScanners();
            //SubTypesScanner = new SubTypesScanner(false);

            Reflections reflections = new Reflections(configBuilder);
                        //.setScanners(new SubTypesScanner(), new TypeAnnotationsScanner().filterResultsBy(myClassAnnotationsFilter)));
            Set<Class<?>> checks = reflections.getTypesAnnotatedWith(MyAnnotation.class, true);
            System.out.println("Number of checks is " + checks.size());
            for(Class<?> check : checks)
            {
                System.out.println(check.getName());
            }
            System.out.println("sdf");

        }
        catch (DependencyCollectionException e1)
        {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        catch (ArtifactResolutionException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (MalformedURLException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private static RepositorySystem newRepositorySystem()
    {
        DefaultServiceLocator locator = MavenRepositorySystemUtils.newServiceLocator();
        locator.addService( RepositoryConnectorFactory.class, BasicRepositoryConnectorFactory.class );
        locator.addService( TransporterFactory.class, FileTransporterFactory.class );
        locator.addService( TransporterFactory.class, HttpTransporterFactory.class );

        return locator.getService( RepositorySystem.class );
    }

    private static RepositorySystemSession newSession( RepositorySystem system )
    {
        DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession();

        LocalRepository localRepo = new LocalRepository( "C://maven_repository//repository" );
        session.setLocalRepositoryManager( system.newLocalRepositoryManager( session, localRepo ) );

        return session;
    }
}

1 个答案:

答案 0 :(得分:1)

是,可以通过引用工件位置(通常由${project.build.directory}/${project.build.finalName}属性表示)或通过在插件中使用ArtifactResolver来实现。您可以查看Apache Tomcat Maven Plugin source code,它从本地或远程Maven存储库中查找工件。

请注意,您必须在package阶段之后(通常在verify阶段之后)绑定自定义插件。