Get dependencies of WAR Artifact using Maven 2 API

2019-03-30 15:18发布

I have found a few posts surrounding the question of artifact retrieval however the answers don't seem to work in my specific case.

I am writing a plugin that will help with skinny war EAR generation and I am running a plugin that I have written for my EAR maven module. In the plugin code I have got to the stage where I want to be able to get the dependencies / artifacts of the WAR dependencies - currently these are not coming through with anything I have tried. I presume this is because even running a dependency:tree on my EAR module doesn't include them, they are not 'transitive' dependencies.

// Neither of the two below return WAR transitive dependencies, just the WARs
project.getDependencies()
project.getArtifacts()

My new approach is currently this:

ArtifactRepository localRepository = project.getDistributionManagementArtifactRepository();
List remoteRepositories = project.getRemoteArtifactRepositories();

ResolutionGroup resolutionGroup = artifactMetadataSource.retrieve(warArtifact, localRepository, remoteRepositories);
Set<Artifact> artifactDependencies = resolutionGroup.getArtifacts();

(N.B. This is making use of the project's ArtifactMetadataSource component object and the dependencies maven-dependency-plugin:2.4)

This is not working. The artifactDependencies set is empty. Now this must be possible because running mvn dependency:tree in the directory for the module warArtifact works fine as expected.

Any ideas?

1条回答
干净又极端
2楼-- · 2019-03-30 15:55

I finally have an answer. Admittedly it doesn't properly answer my original question but it's what I've gone with.

Reason it doesn't completely answer my original post: it requires Maven 3.

Here it is anyway for those that are interested:

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
import org.sonatype.aether.RepositorySystem;
import org.sonatype.aether.RepositorySystemSession;
import org.sonatype.aether.artifact.Artifact;
import org.sonatype.aether.collection.CollectRequest;
import org.sonatype.aether.graph.Dependency;
import org.sonatype.aether.graph.DependencyNode;
import org.sonatype.aether.repository.RemoteRepository;
import org.sonatype.aether.resolution.DependencyRequest;
import org.sonatype.aether.util.artifact.DefaultArtifact;
import org.sonatype.aether.util.graph.PreorderNodeListGenerator;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @goal findShareables
 * @phase process-resources
 */
@SuppressWarnings("unchecked")
public class ShareableJarsInWarsExtractor extends AbstractMojo
{
    // ...

    /**
     * The MavenProject object.
     *
     * @parameter expression="${project}"
     * @readonly
     */
    private MavenProject project;

    /**
     * The entry point to Aether, i.e. the component doing all the work.
     *
     * @component
     */
    private RepositorySystem repoSystem;

    /**
     * The current repository/network configuration of Maven.
     *
     * @parameter default-value="${repositorySystemSession}"
     * @readonly
     */
    private RepositorySystemSession repoSession;

    /**
     * The project's remote repositories to use for the resolution of plugins and their dependencies.
     *
     * @parameter default-value="${project.remotePluginRepositories}"
     * @readonly
     */
    private List<RemoteRepository> remoteRepos;


    public void execute() throws MojoExecutionException
    {
        Artifact projectArtifact = getProjectArtifact();
        List<Dependency> projectDependencies = getArtifactsDependencies(projectArtifact);

        for (Dependency d : projectDependencies)
        {
            if (d.getArtifact() != null && "war".equals(d.getArtifact().getExtension()))
            {
                List<Dependency> warDependencies = getArtifactsDependencies(d.getArtifact());
                // I now have all of the WAR's dependencies!! Hooray!

                // ...
            }
        }

        // ...
    }


    private List<Dependency> getArtifactsDependencies(Artifact a)
    {
        List<Dependency> ret = new ArrayList<Dependency>();

        // Note: I get the POM artifact, not the WAR or whatever.
        DefaultArtifact pomArtifact = new DefaultArtifact(a.getGroupId(), a.getArtifactId(), "pom", a.getVersion());
        CollectRequest collectRequest = new CollectRequest();
        collectRequest.setRoot(new Dependency(pomArtifact, "compile"));
        collectRequest.setRepositories(remoteRepos);

        try
        {
            DependencyNode node = repoSystem.collectDependencies(repoSession, collectRequest).getRoot();
            DependencyRequest projectDependencyRequest = new DependencyRequest(node, null);

            repoSystem.resolveDependencies(repoSession, projectDependencyRequest);

            PreorderNodeListGenerator nlg = new PreorderNodeListGenerator();
            node.accept(nlg);

            ret.addAll(nlg.getDependencies(true));
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return ret;
    }


    private Artifact getProjectArtifact()
    {
        // Todo: There must be a better way!
        return new DefaultArtifact(project.getArtifact().toString());
    }


    // ...
}

My POM defines the following dependencies:

        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.4</version>
        </dependency>

        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-plugin-api</artifactId>
            <version>3.0.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-model</artifactId>
            <version>3.0.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-core</artifactId>
            <version>3.0.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-artifact</artifactId>
            <version>3.0.4</version>
        </dependency>
查看更多
登录 后发表回答