6

Maven 2 mojoでは、次のようなものを使用して依存関係ツリーを取得できます。

DependencyNode rootNode;
try {
  rootNode = treeBuilder.buildDependencyTree(project, localRepository,
      artifactFactory, artifactMetadataSource, null, artifactCollector);
} catch (DependencyTreeBuilderException e) {
  throw new MojoExecutionException("Failed to create dependency tree.", e);
}
final CollectingDependencyNodeVisitor visitor = new CollectingDependencyNodeVisitor();
rootNode.accept(visitor);
final List<DependencyNode> nodes = visitor.getNodes();

Maven 3に相当するものは何ですか?その間に、関連する多くのクラスが非推奨になりました(、、ArtifactCollectorなどArtifactMetadataSource)。

4

3 に答える 3

3

Aetherを使用する必要があります(これはMaven 3が使用しているものです)。jcabi-aether(私はその開発者です)と呼ばれる単純なラッパーを使用することをお勧めします。

于 2012-05-20T07:55:17.423 に答える
1

Maven 3は、依存関係の解決にAetherを使用します。ドキュメントの下部近くに、MavenプラグインでAetherを使用するための別のリンクがあります。

于 2012-05-09T01:34:05.803 に答える
1

オンラインで実用的な解決策が見つからなかったので、Mavenのソースコードを調べて自分で解決策を考え出しました。次のコードのほとんどはMavenソースコードからコピーされたものです。Maven3.5.4でテストしたところ、完全に機能しました。

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.ArtifactUtils;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.DefaultDependencyResolutionRequest;
import org.apache.maven.project.DependencyResolutionResult;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.ProjectDependenciesResolver;
import org.eclipse.aether.graph.DependencyFilter;
import org.eclipse.aether.graph.DependencyNode;
import org.eclipse.aether.util.filter.AndDependencyFilter;
import org.eclipse.aether.util.filter.ScopeDependencyFilter;

import javax.inject.Inject;
import java.util.*;

@Mojo( name = "test-mojo", requiresDependencyResolution = ResolutionScope.RUNTIME)
public class TestMojo extends AbstractMojo {

    @Parameter(defaultValue = "${project}", readonly = true, required = true)
    private MavenProject project;
    @Parameter(defaultValue = "${session}", readonly = true, required = true)
    private MavenSession session;
    @Inject
    private ProjectDependenciesResolver dependenciesResolver;

    public void execute() throws MojoExecutionException {
        try {
            Collection<String> scopesToResolve = new HashSet<>();
            scopesToResolve.add("compile");
            scopesToResolve.add("provided");
            scopesToResolve.add("runtime");
            scopesToResolve.add("system");
            scopesToResolve.add("test");
            Collection<String> scopesToCollect = new HashSet<>();
            scopesToCollect.add("compile");
            scopesToCollect.add("provided");
            scopesToCollect.add("runtime");
            scopesToCollect.add("system");
            scopesToCollect.add("test");
            Collection<Artifact> projectArtifacts = new HashSet<>();
            projectArtifacts.add(project.getArtifact());
            DependencyFilter collectionFilter = new ScopeDependencyFilter( null, negate( scopesToCollect ) );
            DependencyFilter resolutionFilter = new ScopeDependencyFilter( null, negate( scopesToResolve ) );
            resolutionFilter = AndDependencyFilter.newInstance( collectionFilter, resolutionFilter );
            resolutionFilter = AndDependencyFilter.newInstance( resolutionFilter, new ReactorDependencyFilter( projectArtifacts ) );
            DefaultDependencyResolutionRequest request =
                    new DefaultDependencyResolutionRequest( project, session.getRepositorySession() );
            request.setResolutionFilter( resolutionFilter );
            DependencyResolutionResult result = dependenciesResolver.resolve( request );

            // Here is your dependency gragh, you can do whatever you want with it.
            List<DependencyNode> children = result.getDependencyGraph().getChildren();
            List<DependencyNode> grandChildren = children.get(0).getChildren();
        } catch (Exception e) {
            e.printStackTrace();
            throw new MojoExecutionException(e.getMessage());
        }
    }

    private Collection<String> negate( Collection<String> scopes )
    {
        Collection<String> result = new HashSet<>();
        Collections.addAll( result, "system", "compile", "provided", "runtime", "test" );

        for ( String scope : scopes )
        {
            if ( "compile".equals( scope ) )
            {
                result.remove( "compile" );
                result.remove( "system" );
                result.remove( "provided" );
            }
            else if ( "runtime".equals( scope ) )
            {
                result.remove( "compile" );
                result.remove( "runtime" );
            }
            else if ( "compile+runtime".equals( scope ) )
            {
                result.remove( "compile" );
                result.remove( "system" );
                result.remove( "provided" );
                result.remove( "runtime" );
            }
            else if ( "runtime+system".equals( scope ) )
            {
                result.remove( "compile" );
                result.remove( "system" );
                result.remove( "runtime" );
            }
            else if ( "test".equals( scope ) )
            {
                result.clear();
            }
        }

        return result;
    }
    private static class ReactorDependencyFilter
            implements DependencyFilter
    {

        private Set<String> keys = new HashSet<>();

        ReactorDependencyFilter( Collection<Artifact> artifacts )
        {
            for ( Artifact artifact : artifacts )
            {
                String key = ArtifactUtils.key( artifact );
                keys.add( key );
            }
        }

        public boolean accept(DependencyNode node, List<DependencyNode> parents )
        {
            org.eclipse.aether.graph.Dependency dependency = node.getDependency();
            if ( dependency != null )
            {
                org.eclipse.aether.artifact.Artifact a = dependency.getArtifact();
                String key = ArtifactUtils.key( a.getGroupId(), a.getArtifactId(), a.getVersion() );
                return !keys.contains( key );
            }
            return false;
        }

    }
}

また、それを機能させるには、いくつかの依存関係を追加する必要があることに注意してください。これが私のものです。

<dependencies>
    <dependency>
        <groupId>org.apache.maven</groupId>
        <artifactId>maven-plugin-api</artifactId>
        <version>3.6.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.maven.plugin-tools</groupId>
        <artifactId>maven-plugin-annotations</artifactId>
        <version>3.6.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.maven</groupId>
        <artifactId>maven-project</artifactId>
        <version>2.2.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.maven.shared</groupId>
        <artifactId>maven-common-artifact-filters</artifactId>
        <version>3.0.1</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.maven</groupId>
        <artifactId>maven-core</artifactId>
        <version>3.5.4</version>
    </dependency>
</dependencies>
于 2019-05-30T09:32:29.427 に答える