Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/EmptyTag/148.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Maven 如何获得一只耳朵';什么是完全依赖树?_Maven_Maven Plugin - Fatal编程技术网

Maven 如何获得一只耳朵';什么是完全依赖树?

Maven 如何获得一只耳朵';什么是完全依赖树?,maven,maven-plugin,Maven,Maven Plugin,下面是我的用例:我希望获得EAR的完整依赖关系树,包括以下范围:编译、运行时、测试 不幸的是,Maven的依赖插件只将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

下面是我的用例:我希望获得EAR的完整依赖关系树,包括以下范围:编译、运行时、测试

不幸的是,Maven的依赖插件只将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的依赖关系)

我用乙醚试过了,但没能把望远镜调好。接下来,我尝试使用“旧式”Maven依赖解析(查看依赖插件的代码),但是
org.apache.Maven.shared:Maven依赖树
仅适用于
MavenProject
,但我应该告诉
DependencyGraphBuilder
获取任何
工件的依赖项


有人能给我指出正确的方向吗?

最后,这就是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, ":");
}
/**
*获取工件的所有依赖项,包括来自WAR依赖项的依赖项。
*@param神器神器
*@返回依赖项,其中键是artifactId,值是其所有工件(不同
*版本,不同的范围)
*@如果无法获取依赖项,则抛出MojoExecutionException
*/
受保护的Multimap getAllDependencies(最终工件工件)引发MojoExecutionException异常{
Multimap projectDependencies=getDependencies(工件);
Multimap allDependencies=ArrayListMultimap.create(projectDependencies);
for(工件依赖项:projectDependencies.values()){
if(dependency.getType().equals(“war”)){
Multimap Wardependency=getDependencies(依赖项);
for(工件warArtifact:warDependencies.values()){
if(!containsArtifact(所有依赖项,warArtifact)){
allDependencies.put(warArtifact.getArtifactId(),warArtifact);
}
}
}
}
返回所有依赖项;
}
/**
*获取项目的依赖项。
*@param神器神器
*@返回依赖项,其中键是artifactId,值是其所有工件(不同
*版本,不同的范围)
*@如果无法获取依赖项,则抛出MojoExecutionException
*/
受保护的Multimap getDependencies(最终工件工件)引发MojoExecutionException异常{
试一试{
Artifact pomArtifact=this.repositorySystem.createProjectArtifact(Artifact.getGroupId(),
artifact.getArtifactId(),artifact.getVersion();
MavenProject MavenProject=this.mavenProjectBuilder.buildFromRepository(pomaartifact,this.remoteRepositories,
这是(localRepository);
Multimap dependencies=ArrayListMultimap.create();
DependencyNode rootNode=this.dependencyGraphBuilder.buildDependencyGraph(mavenProject,this.artifactFilter);
CollectionDependencyNodeVisitor visitor=新的CollectionDependencyNodeVisitor();
接受(访问者);
对于(DependencyNode DependencyNode:visitor.getNodes()){
Artifact dependencyArtifact=dependencyNode.getArtifact();
if(!containsArtifact(依赖项、依赖项工件)){
dependency.put(dependencyArtifact.getArtifactId(),dependencyArtifact);
}
}
返回依赖项;
}捕获(例外e){
抛出新的MojoExecutionException(“无法获取”+工件的依赖项,e);
}
}
/**
*显示给定工件中是否包含工件。
*@param工件-工件
*@param artifact要检查的工件
*@return{@code-true}如果工件被包含,否则{@code-false}
*/
受保护的布尔containsArtifact(最终多重映射工件,最终工件){
字符串artifactId=artifact.getArtifactId();
如果(!artifacts.containsKey(artifactId)){
返回false;
}否则{
字符串坐标=getCoordinates(工件);
for(Artifact existingArtifact:artifacts.get(artifactId)){
字符串existingCoordinates=getCoordinates(existingArtifact);
if(坐标等于(现有坐标)){
返回true;
}
}
返回false;
}
}
/**
*获取工件的Maven坐标。
*@param神器神器
*@返回Maven坐标
*/
受保护的静态字符串getCoordinates(最终工件){
List parts=Lists.newArrayList(artifact.getGroupId(),artifact.getArtifactId(),artifact.getType(),
artifact.getVersion(),artifact.getScope());
返回StringUtils.join(parts,“:”);
}

你能说得更准确一点吗:Maven插件(使用3.0.4)中的`是什么意思?这是你的maven版本还是maven依赖插件?可能是@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.2Ok我不够清楚-(…首先我指的是您正在使用的Maven版本,您回答了这个版本(3.0.4)…此外,您正在使用哪个Maven依赖插件版本…我正在使用Maven依赖插件的2.8版本。