一千萬個為什麽

搜索

如何解決maven插件內的maven依賴關系?

我試圖使用標記為 resolveTransitively = trueArtifactResolver.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構建會削減一些處理步驟還是ArtifactResolver另外做些什麽?

最佳答案

這是我用來解析工件並獲取文件引用的代碼:

// 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 remoteRepositories;

轉載註明原文: 如何解決maven插件內的maven依賴關系?