获取maven项目的所有依赖项

时间:2011-05-25 16:48:31

标签: java maven pom.xml aether

喜 有一个简单的方法,我可以使用java(不是mvn控制台)获取maven项目的所有依赖项,或者mvn中使用的算法是什么?感谢

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

    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 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分: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
);

我的细节可能有问题,但总的来说这就是它应该如何运作。