我正在编写一个Maven 3插件,需要知道给定org.apache.maven.model.Dependency
的传递依赖性。我怎么能这样做?
答案 0 :(得分:7)
在Maven 3中,依靠maven-dependency-tree
共享组件访问基于树的表单中的所有依赖项:
用于解决Maven项目依赖关系的基于树的API。
该组件引入了DependencyGraphBuilder
,可以为给定的Maven项目构建依赖关系树。您还可以使用ArtifactFilter
过滤工件,其中有几个内置实现可按groupId,artifactId(IncludesArtifactFilter
和ExcludesArtifactFilter
),范围(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);
}
}
}
这样可以访问依赖关系树的根节点,该树是当前项目。从该节点,您可以通过调用getChildren()
方法访问所有孩子。因此,如果要列出所有依赖项,可以递归遍历该图。该组件确实提供了使用CollectingDependencyNodeVisitor
执行此操作的工具。它会将所有依赖项收集到List
中,以便轻松遍历它。
对于Maven插件,因此需要以下依赖项:
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-dependency-tree</artifactId>
<version>3.0</version>
</dependency>
答案 1 :(得分:1)
因此,以下代码应该给您一个如何做的印象。
@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();
}
}
上面的代码将为您提供已解析的工件以及依赖项。您需要在依赖项之间做出区分(在这种情况下,项目依赖项不带传递,而工件是已解决的工件,包括传递。)。
最重要的是requiresDependencyResolution = ResolutionScope.COMPILE
,否则null
会获得getArtifacts()
。
Tunaki的建议适用于任何不属于您项目的工件......问题是您真正需要的是什么?