【问题标题】:How to get an EAR's full dependency tree?如何获得 EAR 的完整依赖树?
【发布时间】:2015-08-03 13:47:04
【问题描述】:

这是我的用例:我想获取我的 EAR 的完整依赖关系树,具有以下范围:编译、运行时、测试。

不幸的是,Maven 的 Dependency Plugin 仅将 WAR 显示为简单的依赖项,没有传递依赖项。

foo:bar-ear:ear:1.13.0-SNAPSHOT
+- foo:bar-web:war:1.13.0-SNAPSHOT:compile
+- foo:bar-web-service:war:1.13.0-SNAPSHOT:compile
+- foo:bar-business:ejb:1.13.0-SNAPSHOT:compile
|  +- foo:base-api:ejb:2.22.0-SNAPSHOT:compile
...

所以我需要在 Maven 插件(使用 3.0.4)中获取完整的依赖关系树(包括 WAR 的依赖关系)。

我尝试使用 Aether,但无法正确设置示波器。接下来我尝试使用“旧式”Maven 依赖解析(查看依赖插件的代码),但org.apache.maven.shared:maven-dependency-tree 仅适用于MavenProject,但我应该告诉DependencyGraphBuilder 获取任何依赖项Artifact.

谁能指出我正确的方向?

【问题讨论】:

  • 您能否更准确一点:`in a Maven plugin (using 3.0.4).` 是什么意思?这是你的 maven 版本还是 maven-dependency-plugin 还是什么?
  • @khmarbaise:我使用的是 Maven 3.0.4。我不太确定这个版本的准确依赖关系是什么,这就是我所拥有的:maven-plugin-api 3.0.4、maven-core 3.0.4、maven-plugin-annotations 3.0、maven-dependency-tree 2.2
  • 好吧,我不够清楚 ;-(...首先我的意思是您正在使用的 Maven 版本,您回答了 (3.0.4)...此外,您是哪个 maven-dependency-plugin 版本使用....
  • 我使用的是 2.8 版的 maven-dependency-plugin。

标签: maven maven-plugin


【解决方案1】:

最后,这就是使用 Maven API 的方法。我使用了 Google Guava 和 Apache Commons Lang 3,所以如果您不熟悉这些库,请询问。

/**
 * Gets all dependencies for an artifact, including the ones from WAR dependencies.
 * @param artifact The artifact
 * @return The dependencies, where the key is the artifactId and the values are all of its artifacts (different
 * versions, different scopes)
 * @throws MojoExecutionException if the dependencies can't be fetched
 */
protected Multimap<String, Artifact> getAllDependencies(final Artifact artifact) throws MojoExecutionException {
  Multimap<String, Artifact> projectDependencies = getDependencies(artifact);
  Multimap<String, Artifact> allDependencies = ArrayListMultimap.create(projectDependencies);

  for (Artifact dependency : projectDependencies.values()) {
    if (dependency.getType().equals("war")) {
      Multimap<String, Artifact> warDependencies = getDependencies(dependency);
      for (Artifact warArtifact : warDependencies.values()) {
        if (!containsArtifact(allDependencies, warArtifact)) {
          allDependencies.put(warArtifact.getArtifactId(), warArtifact);
        }
      }
    }
  }
  return allDependencies;
}

/**
 * Gets the dependencies for an artifact.
 * @param artifact The artifact
 * @return The dependencies, where the key is the artifactId and the values are all of its artifacts (different
 * versions, different scopes)
 * @throws MojoExecutionException if the dependencies can't be fetched
 */
protected Multimap<String, Artifact> getDependencies(final Artifact artifact) throws MojoExecutionException {
  try {
    Artifact pomArtifact = this.repositorySystem.createProjectArtifact(artifact.getGroupId(),
        artifact.getArtifactId(), artifact.getVersion());
    MavenProject mavenProject = this.mavenProjectBuilder.buildFromRepository(pomArtifact, this.remoteRepositories,
        this.localRepository);

    Multimap<String, Artifact> dependencies = ArrayListMultimap.create();
    DependencyNode rootNode = this.dependencyGraphBuilder.buildDependencyGraph(mavenProject, this.artifactFilter);

    CollectingDependencyNodeVisitor visitor = new CollectingDependencyNodeVisitor();
    rootNode.accept(visitor);

    for (DependencyNode dependencyNode : visitor.getNodes()) {
      Artifact dependencyArtifact = dependencyNode.getArtifact();
      if (!containsArtifact(dependencies, dependencyArtifact)) {
        dependencies.put(dependencyArtifact.getArtifactId(), dependencyArtifact);
      }
    }
    return dependencies;
  } catch (Exception e) {
    throw new MojoExecutionException("Can't get dependencies for " + artifact, e);
  }
}

/**
 * Shows whether an artifact is contained in the given artifacts.
 * @param artifacts The artifacts
 * @param artifact The artifact to check
 * @return {@code true} if the artifact is contained, otherwise {@code false}
 */
protected boolean containsArtifact(final Multimap<String, Artifact> artifacts, final Artifact artifact) {
  String artifactId = artifact.getArtifactId();
  if (!artifacts.containsKey(artifactId)) {
    return false;
  } else {
    String coordinates = getCoordinates(artifact);
    for (Artifact existingArtifact : artifacts.get(artifactId)) {
      String existingCoordinates = getCoordinates(existingArtifact);
      if (coordinates.equals(existingCoordinates)) {
        return true;
      }
    }
    return false;
  }
}

/**
 * Gets the Maven coordinates for an artifact.
 * @param artifact The artifact
 * @return The Maven coordinates
 */
protected static String getCoordinates(final Artifact artifact) {
  List<String> parts = Lists.newArrayList(artifact.getGroupId(), artifact.getArtifactId(), artifact.getType(),
      artifact.getVersion(), artifact.getScope());
  return StringUtils.join(parts, ":");
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-10
    • 2013-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多