2016-02-25 96 views
3

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

有没有插件或可以帮助?

Maven的版本:3.2.3

编辑

对于人们认为这个问题是重复的其他问题,可以考虑:

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

  2. 另一个问题说不生成依赖树。

+0

如果使用的是Eclipse有一个dependencyManagement部分,这将列出所有与依赖传递依赖沿 – Pragnani

回答

4

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

但是,您可以为此编写自己的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项目,为此我们使用来解决依赖关系管理部分中的依赖关系。
  • 对于每个依赖项,我们通过在内存中使用ProjectBuilder API构建Maven项目来解决其传递依赖性问题。这需要将我们的Dependency转换为Artifact(这是在toArtifact的帮助下完成的),构建一个ProjectBuildingRequest将项目设置为null(我注意到我们还需要将验证级别设置为最小,以便API不会失败稍不符合要求的POMs),最后致电projectBuilder.build建立该项目。有了这个项目,我们可以用mavenProject.getDependencies()返回它的依赖关系。
  • 所有这些都是在递归方法内部完成的,它将简单地打印工件的基本信息。递归性级别是通过每次向下一级时预先添加两个空格来完成的。另外,由于我们可能会重新访问一个已经受到感染的工件(因此最终会陷入无限循环),所以我保留了一组访问的工件,并在已经受到鼓励的工件再次受到鼓励时结束树。
  • ArtifactNotFoundException例外情况下,如果我们尝试下载某种找不到的工件,则可能会发生这种情况。
  • 第一次启动时,项目构建器API将从您配置的replactors中下载所有依赖关系,因此会混淆日志。您将希望第二次启动它以更好地查看输出。

为样本,我用下面的依赖关系管理部分

<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。在具有上述

  • POM中的Maven插件

    1. 运行mvn clean install创建一个新的测试Maven项目,宣布插件。

    为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>