2011-05-25 60 views
2

嗨 有没有一种简单的方法,我可以得到所有依赖关系的Maven项目使用Java(而不是mvn控制台)或什么是在mvn中使用的算法?感谢得到一个Maven项目的所有依赖关系

伊夫发现模型(org.apache.maven.model) 下面的代码工作,直到它找到一个变量(例如 '< '版本> $ {mavenVersion}' <'/版本>)

 
    System.out.println("dependency" + d.getArtifactId()); 
     if(d.isOptional()){ 
      return; 
     } 
     if(d.getVersion()==null){ 
      if(mod.getParent()!=null){ 

      try{ 

       MavenArtifact mart=new MavenArtifact(); 
       mart.setArtifactId(mod.getParent().getArtifactId()); 
       mart.setGroupId(mod.getParent().getGroupId()); 
       mart.setVersion(mod.getParent().getVersion()); 
       mart.setRepositoryUrl(mA.getRepositoryUrl()); 
       Model fMod = getModelFromPom(mart, p); 
       if(fMod.getDependencyManagement()!=null){ 
        for (Dependency dep : fMod.getDependencyManagement().getDependencies()) { 
         if(dep.getArtifactId().equals(d.getArtifactId())&dep.getGroupId().equals(d.getGroupId())){ 
          getDependency(fMod, mart, p, dep); 
         } 
        } 

        for (Dependency dep : fMod.getDependencies()) { 
         if(dep.getArtifactId().equals(d.getArtifactId())&dep.getGroupId().equals(d.getGroupId())){ 
          getDependency(fMod, mart, p, dep); 
         } 
        } 
       }} 


       catch(Exception e){ 
        e.printStackTrace(); 
        view.showWarning("Could not find: groupId:" 
          + d.getGroupId() + " artifactId" 
          + d.getArtifactId() + " version:" 
          + d.getVersion()); 
       } 
      }else{ 
       view.showWarning("Could not find: groupId:" 
         + d.getGroupId() + " artifactId" 
         + d.getArtifactId() + " version:" 
         + d.getVersion()); 


      } 
     }else{ 
     MavenArtifact m = proxy.findDependency(d.getGroupId(), d 
       .getArtifactId(), d.getVersion()); 

     if (m == null) { 
      view.showWarning("Could not find: groupId:" 
        + d.getGroupId() + " artifactId" 
        + d.getArtifactId() + " version:" 
        + d.getVersion()); 

      return; 
     } 
     importMavenArtifact(m, p); 



回答

2

如果要匹配2.x的行为,请阅读maven-dependency-plugin的源代码。如果你需要匹配3.x,我认为你必须学习以太。

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 
); 

我可能是错的细节,但一般来说,这是它应该如何工作。

+0

看起来不错,但我无法测试它,因为我的项目被遗弃+1 – sherif 2012-10-28 20:41:37