【问题标题】:get all dependencies for a maven project获取 Maven 项目的所有依赖项
【发布时间】:2011-09-01 22:44:00
【问题描述】:

嗨 有没有一种简单的方法可以让我使用 java(不是 mvn 控制台)或 mvn 中使用的算法获取 maven 项目的所有依赖项?谢谢

我找到了模型 (org.apache.maven.model) 下面的代码一直有效,直到找到一个变量(例如 '${mavenVersion}''/version>)

System.out.println("依赖" + d.getArtifactId()); 如果(d.isOptional()){ 返回; } if(d.getVersion()==null){ 如果(mod.getParent()!=空){ 尝试{ MavenArtifact 市场=新的 MavenArtifact(); mart.setArtifactId(mod.getParent().getArtifactId()); mart.setGroupId(mod.getParent().getGroupId()); mart.setVersion(mod.getParent().getVersion()); mart.setRepositoryUrl(mA.getRepositoryUrl()); 模型 fMod = getModelFromPom(mart, p); if(fMod.getDependencyManagement()!=null){ for(依赖关系:fMod.getDependencyManagement().getDependencies()){ if(dep.getArtifactId().equals(d.getArtifactId())&dep.getGroupId().equals(d.getGroupId())){ getDependency(fMod, 市场, p, dep); } } for (依赖 dep : fMod.getDependencies()) { if(dep.getArtifactId().equals(d.getArtifactId())&dep.getGroupId().equals(d.getGroupId())){ getDependency(fMod, 市场, p, dep); } } }} 捕获(异常 e){ e.printStackTrace(); view.showWarning("找不到:groupId:" + d.getGroupId() + "artifactId" + d.getArtifactId() + "版本:" + d.getVersion()); } }别的{ view.showWarning("找不到:groupId:" + d.getGroupId() + "artifactId" + d.getArtifactId() + "版本:" + d.getVersion()); } }别的{ MavenArtifact m = proxy.findDependency(d.getGroupId(), d .getArtifactId(), d.getVersion()); 如果(m == null){ view.showWarning("找不到:groupId:" + d.getGroupId() + "artifactId" + d.getArtifactId() + "版本:" + d.getVersion()); 返回; } 进口MavenArtifact(m,p);

【问题讨论】:

    标签: java maven pom.xml aether


    【解决方案1】:

    尝试使用jcabi-aether,它是 Aether 的包装器。首先,您需要获取所有项目依赖项的列表:

    List<Artifact> deps = this.project.getDependencies();
    

    然后,对于它们中的每一个,您可以获得所有传递依赖项的列表:

    File repo = this.session.getLocalRepository().getBasedir();
    Collection<Artifact> deps = new Aether(this.getProject(), repo).resolve(
      new DefaultArtifact("junit", "junit-dep", "", "jar", "4.10"),
      JavaScopes.RUNTIME
    );
    

    我可能在细节上错了,但总的来说应该是这样的。

    【讨论】:

    • 看起来不错,但我无法测试它,因为我的项目无论如何都被放弃了 +1
    【解决方案2】:

    如果您想匹配 2.x 的行为,请阅读 maven-dependency-plugin 的源代码。如果你需要匹配 3.x,我认为你必须了解 aether。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-06
      • 1970-01-01
      • 1970-01-01
      • 2018-10-20
      相关资源
      最近更新 更多