查看dependencyManagement的依赖关系树

时间:2016-02-25 14:45:13

标签: java maven dependency-management

我们都明白,依赖树对于解决传递依赖冲突至关重要。 dependencyManagement也是如此,但我找不到以与dependencies类似的方式为它打印依赖关系树的方法。

是否有插件或其他可以提供帮助的内容?

Maven版本:3.2.3

修改

对于认为此问题与其他问题重复的人,请考虑:

  1. 另一个问题是关于依赖管理的插件管理。

  2. 另一个问题没有说明生成依赖树。

1 个答案:

答案 0 :(得分:6)

我找不到任何为依赖关系管理部分打印依赖关系树的插件。

但是,你可以为此编写自己的MOJO。下面的所有代码都是使用Maven 3.3.9编写和测试的,通过修改新MOJO的依赖性,可以很容易地将它调整到当前的Maven版本。

以下是Maven插件的POM:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>sample.plugin</groupId>
    <artifactId>test-maven-plugin</artifactId>
    <version>1.0.0</version>
    <packaging>maven-plugin</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-plugin-api</artifactId>
            <version>3.3.9</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.plugin-tools</groupId>
            <artifactId>maven-plugin-annotations</artifactId>
            <version>3.4</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-core</artifactId>
            <version>3.3.9</version>
        </dependency>
    </dependencies>
</project>

而MOJO本身就是:

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.DefaultArtifact;
import org.apache.maven.artifact.handler.ArtifactHandler;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.building.ModelBuildingRequest;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Component;
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.DefaultProjectBuildingRequest;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.ProjectBuilder;
import org.apache.maven.project.ProjectBuildingException;
import org.apache.maven.project.ProjectBuildingRequest;
import org.eclipse.aether.transfer.ArtifactNotFoundException;

@Mojo(name = "foo", requiresDependencyResolution = ResolutionScope.TEST)
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
    private ArtifactHandler artifactHandler;

    @Component
    private ProjectBuilder projectBuilder;

    public void execute() throws MojoExecutionException, MojoFailureException {
        Set<Artifact> visitedArtifacts = new HashSet<Artifact>();
        for (Dependency dependency : project.getDependencyManagement().getDependencies()) {
            printDependencyTree(toArtifact(dependency), "", visitedArtifacts);
        }
    }

    private void printDependencyTree(Artifact artifact, String level, Set<Artifact> visitedArtifacts) throws MojoExecutionException {
        getLog().info(level + "+ " + artifact);
        for (Dependency transitive : getTransitiveDependencies(artifact)) {
            Artifact transitiveArtifact = toArtifact(transitive);
            if (!visitedArtifacts.contains(transitiveArtifact)) {
                visitedArtifacts.add(transitiveArtifact);
                printDependencyTree(transitiveArtifact, level + "  ", visitedArtifacts);
            }
        }
    }

    private List<Dependency> getTransitiveDependencies(Artifact artifact) throws MojoExecutionException {
        try {
            ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(session.getProjectBuildingRequest());
            buildingRequest.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL);
            buildingRequest.setProject(null);
            MavenProject mavenProject = projectBuilder.build(artifact, buildingRequest).getProject();
            return mavenProject.getDependencies();
        } catch (ProjectBuildingException e) {
            if (e.getCause() != null && e.getCause().getCause() instanceof ArtifactNotFoundException) {
                //ignore
                return new ArrayList<Dependency>();
            }
            throw new MojoExecutionException("Error while building project", e);
        }
    }

    private Artifact toArtifact(Dependency dependency) {
        return new DefaultArtifact(dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion(), dependency.getScope(), dependency.getType(), dependency.getClassifier(), artifactHandler);
    }

}

这有点复杂,但主要成分是:

  • 我们注入了当前的Maven项目,为此我们使用project.getDependencyManagement().getDependencies()解决了依赖关系管理部分中的依赖项。
  • 对于每个依赖项,我们通过使用ProjectBuilder API在内存中构建Maven项目来解决其传递依赖性。这需要将Dependency转换为Artifact(这是在toArtifact的帮助下完成的),构建ProjectBuildingRequest将项目设置为null(I注意到我们还需要将验证级别设置为minimal,以便API在稍微不符合的POM上不会失败)并最终调用projectBuilder.build来构建项目。有了项目,我们就可以使用mavenProject.getDependencies()返回其依赖项。
  • 所有这些都是在递归方法中完成的,该方法只是打印工件的基本信息。递归级别是通过在每次下降一个级别时预先添加两个空格来实现的。
  • 此外,由于我们可能会重新访问已经受过欢迎的工件(因此最终会进入无限循环),因此我会保留一组访问过的工件,并在再次遇到已经受过欢迎的工件时结束树。
  • 如果我们尝试下载某些未找到的工件,可能会发生的ArtifactNotFoundException例外情况被忽略。
  • 第一次启动时,项目构建器API将从配置的销售代理中下载所有依赖项,因此会使日志混乱。您将需要再次启动它以更好地查看输出。

作为示例,我使用以下依赖关系管理部分

对此进行了测试
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-annotations</artifactId>
            <version>3.4.0.GA</version>
        </dependency>
    </dependencies>
</dependencyManagement>

结果是:

+ org.hibernate:hibernate-annotations:jar:3.4.0.GA
  + org.hibernate:ejb3-persistence:jar:1.0.2.GA:compile
  + org.hibernate:hibernate-commons-annotations:jar:3.1.0.GA:compile
    + org.slf4j:slf4j-api:jar:1.4.2:compile
      + junit:junit:jar:3.8.1:test
  + org.hibernate:hibernate-core:jar:3.3.0.SP1:compile
    + antlr:antlr:jar:2.7.6:compile
    + commons-collections:commons-collections:jar:3.1:compile
    + dom4j:dom4j:jar:1.6.1:compile
      + jaxme:jaxme-api:jar:0.3:compile
      + jaxen:jaxen:jar:1.1-beta-6:compile
        + dom4j:dom4j:jar:1.5.2:compile
          + jaxen:jaxen:jar:1.1-beta-4:compile
            + jdom:jdom:jar:b10:compile
            + xerces:xmlParserAPIs:jar:2.6.2:compile
            + xerces:xercesImpl:jar:2.6.2:compile
            + xom:xom:jar:1.0b3:compile
              + xerces:xmlParserAPIs:jar:2.6.1:compile
              + xerces:xercesImpl:jar:2.2.1:compile
              + com.ibm.icu:icu4j:jar:2.6.1:compile
              + xalan:xalan:jar:2.6.0:compile
                + xml-apis:xml-apis:jar:2.0.2:compile
                + xerces:xercesImpl:jar:2.6.0:compile
              + org.ccil.cowan.tagsoup:tagsoup:jar:0.9.7:compile
              + javax.servlet:servlet-api:jar:2.4:provided
          + msv:xsdlib:jar:20030807:compile
          + msv:relaxngDatatype:jar:20030807:compile
          + pull-parser:pull-parser:jar:2:compile
          + xpp3:xpp3:jar:1.1.3.3:compile
          + stax:stax-api:jar:1.0:compile
          + junitperf:junitperf:jar:1.8:test
          + stax:stax-ri:jar:1.0:test
          + xalan:xalan:jar:2.5.1:test
        + jdom:jdom:jar:1.0:compile
          + xml-apis:xml-apis:jar:1.0.b2:compile
          + jaxen:jaxen:jar:1.0-FCS:compile
          + saxpath:saxpath:jar:1.0-FCS:compile
          + xalan:xalan:jar:2.5.0:compile
    + javax.transaction:jta:jar:1.1:compile
    + javax.security:jaas:jar:1.0.01:provided
    + javax.security:jacc:jar:1.0:provided
    + ant:ant:jar:1.6.5:provided
      + xml-apis:xml-apis:jar:1.3.04:compile
    + javassist:javassist:jar:3.4.GA:compile
    + org.hibernate:hibernate-cglib-repack:jar:2.1_3:compile

要对此进行测试,您当然需要将MOJO绑定到测试Maven项目,并且您需要事先安装MOJO。

  1. 在上面有POM的Maven插件上运行mvn clean install
  2. 创建一个新的测试Maven项目,声明该插件。
  3. 上面MOJO的插件配置可以是:

    <plugin>
        <groupId>sample.plugin</groupId>
        <artifactId>test-maven-plugin</artifactId>
        <version>1.0.0</version>
        <executions>
            <execution>
                <id>t</id>
                <goals>
                    <goal>foo</goal>
                </goals>
                <phase><!-- something --></phase>
            </execution>
        </executions>
    </plugin>