Find Dependency Object Dependencies

I am writing a Maven 3 plugin that needs to know the transitive dependencies of a given org.apache.maven.model.Dependency. How can i do this?

+2
source share
2 answers

Therefore, the following code should give you an impression of how to do this.

@Mojo( name = "test", requiresDependencyResolution = ResolutionScope.COMPILE, defaultPhase = LifecyclePhase.PACKAGE ...)
public class TestMojo
    extends AbstractMojo
{
    @Parameter( defaultValue = "${project}", readonly = true )
    private MavenProject project;

    public void execute()
        throws MojoExecutionException, MojoFailureException
    {
        List<Dependency> dependencies = project.getDependencies();
        for ( Dependency dependency : dependencies )
        {
            getLog().info( "Dependency: " + getId(dependency) );
        }

        Set<Artifact> artifacts = project.getArtifacts();
        for ( Artifact artifact : artifacts )
        {
            getLog().info( "Artifact: " + artifact.getId() );
        }
    }

    private String getId(Dependency dep) {
        StringBuilder sb = new StringBuilder();
        sb.append( dep.getGroupId() );
        sb.append( ':' );
        sb.append( dep.getArtifactId() );
        sb.append( ':' );
        sb.append( dep.getVersion() );
        return sb.toString();
    }

}

The above code will give you resolved artifacts as well as dependencies. You need to make the difference between the dependencies (in this case, the project dependencies without transitive and artifacts that are resolved artifacts, including transient ones.).

The most important is requiresDependencyResolution = ResolutionScope.COMPILE, otherwise you will get nullfor getArtifacts().

Tunaki , ... , ?

+1

Maven 3 , maven-dependency-tree :

API Maven.

DependencyGraphBuilder, Maven. ArtifactFilter, groupId, artifactId (IncludesArtifactFilter ExcludesArtifactFilter), scope (ScopeArtifactFilter) .. fiter null, .

, , IncludesArtifactFilter groupId:artifactId . :

@Mojo(name = "foo")
public class MyMojo extends AbstractMojo {

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

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

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

    public void execute() throws MojoExecutionException, MojoFailureException {
        ArtifactFilter artifactFilter = new IncludesArtifactFilter(Arrays.asList("groupId:artifactId"));
        ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(session.getProjectBuildingRequest());
        buildingRequest.setProject(project);
        try {
            DependencyNode rootNode = dependencyGraphBuilder.buildDependencyGraph(buildingRequest, artifactFilter);
            CollectingDependencyNodeVisitor visitor = new CollectingDependencyNodeVisitor();
            rootNode.accept(visitor);
            for (DependencyNode node : visitor.getNodes()) {
                System.out.println(node.toNodeString());
            }
        } catch (DependencyGraphBuilderException e) {
            throw new MojoExecutionException("Couldn't build dependency graph", e);
        }
    }

}

node , . node , getChildren(). , , . CollectingDependencyNodeVisitor. List, .

Maven :

<dependency>
    <groupId>org.apache.maven.shared</groupId>
    <artifactId>maven-dependency-tree</artifactId>
    <version>3.0</version>
</dependency>
+5

Source: https://habr.com/ru/post/1664228/


All Articles