我尝试使用带标志 resolveTransitively = true 的 ArtifactResolver.resolve(ArtifactResolutionRequest),我的项目的分离模块需要10分钟。
ArtifactResolutionRequest request = new ArtifactResolutionRequest()
.setArtifact( project.getArtifact() )
.setArtifactDependencies( project.getDependencyArtifacts() )
.setLocalRepository( localRepository )
.setManagedVersionMap( project.getManagedVersionMap() )
.setForceUpdate(false)
.setResolveTransitively(true)
.setOffline(true)
.setListeners(listeners);
result = artifactResolver.resolve(request);
如果我用mvn构建它需要大约4秒。
maven如何在编译之前解决所有传递依赖关系以及如何在代码中使用它?
//项目的单独模块大量使用父项,导入依赖项,最后有350个依赖项工件。那么可能是maven build会削减一些处理步骤还是ArtifactResolver另外做一些事情?
答案 0 :(得分:1)
这是我用来解析工件并获取文件引用的代码:
// get maven artifact representing this mojo artifact
org.apache.maven.artifact.Artifact ma = ...; // create artifact reference
// resolve the maven artifact
try {
resolver.resolve(ma, remoteRepositories, localRepository );
} catch (ArtifactNotFoundException ex) {
throw new MojoExecutionException("could not find deploy artifact ["+a+"]", ex);
} catch (ArtifactResolutionException ex) {
throw new MojoExecutionException("could not resolve deploy artifact ["+a+"]", ex);
}
File maFile = ma.getFile();
存储库引用是类级属性,通过maven中的依赖注入来解析。你只需要在你的班级中声明它们:
/**
* @parameter default-value="${localRepository}"
* @readonly
* @required
*/
protected ArtifactRepository localRepository;
/**
* @parameter default-value="${project.remoteArtifactRepositories}"
* @readonly
* @required
*/
protected java.util.List<ArtifactRepository> remoteRepositories;