问题描述
|
你好
有没有一种简单的方法可以使用java(而不是mvn控制台)获取maven项目的所有依赖关系,或者mvn中使用的算法是什么?谢谢
Ive找到了模型(org.apache.maven.model)
直到找到变量为止,下面的代码才有效(例如\'<\'version> $ {mavenVersion} \'<\'/ version>)
System.out.println(\“ dependency \” + d.getArtifactId());
if(d.isOptional()){
返回;
}
if(d.getVersion()== null){
if(mod.getParent()!= null){
尝试{
MavenArtifact mart = new 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){
对于(依赖关系Dep:fMod.getDependencyManagement()。getDependencies()){
if(dep.getArtifactId()。equals(d.getArtifactId())&dep.getGroupId()。equals(d.getGroupId())){
getDependency(fMod,mart,p,dep);
}
}
对于(依赖关系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(\“找不到: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());
返回;
}
importMavenArtifact(m,p);
解决方法
如果要匹配2.x的行为,请阅读maven-dependency-plugin的源代码。如果您需要匹配3.x,我认为您必须了解以太。
,尝试使用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
);
我可能在细节上是错误的,但是总的来说,这是应该的。