14

我正在寻找可用于从远程存储库检索 Maven 工件的 Java API。到目前为止,我已经找到了Eclipse Ather ,但它看起来对我的需求来说过于复杂,所以我正在寻找更简单的东西。

我需要的是:

  • 我必须指定远程 Maven 存储库的位置
  • 我喜欢根据它的 groupId + artifactId + version 来获取一个工件
  • API 必须提供工件的当前远程版本(考虑定期构建的 SNAPSHOT 工件,因此它们在其版本中具有生成的部分)
  • 返回工件的位置,首选 HTTP URL(我将使用例如 Apache HTTP 客户端自行获取)
  • 可选地检索作为所请求工件的依赖项的工件。
4

4 回答 4

10

jcabi-aether may help you (I'm a developer). It's a simple wrapper around Aether, that lets you find all transitive dependencies of a Maven artifact:

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

Thus, all you need to provide as an input is:

  • Local repo location, as a directory name
  • List of repote repositories (MavenProject#getRemoteRepositories())
  • Maven coordinates of the artifact
  • Maven scope to look for

Absolute paths of every dependency found can be obtained as Artifact#getPath()

于 2012-05-20T08:01:45.377 回答
1
    public List<Artifact> findDependencies(Artifact artifact) throws DependencyCollectionException {

    CollectRequest collectRequest = new CollectRequest();
    collectRequest.setRoot( new Dependency(artifact, "" ) );
    collectRequest.addRepository(repository);

    final MavenServiceLocator locator = new MavenServiceLocator();
    locator.addService( RepositoryConnectorFactory.class, FileRepositoryConnectorFactory.class );
    locator.addService( RepositoryConnectorFactory.class, WagonRepositoryConnectorFactory.class );
    locator.setServices( WagonProvider.class, new WagonProvider() {
        public Wagon lookup(String roleHint) throws Exception {
            if (Arrays.asList("http", "https").contains(roleHint)) {
                return new LightweightHttpWagon();
            }
            return null;
        }

        public void release(Wagon wagon) {

        }
    });

    final RepositorySystem system = locator.getService(RepositorySystem.class);
    MavenRepositorySystemSession session = new MavenRepositorySystemSession();

    session.setLocalRepositoryManager( system.newLocalRepositoryManager(localRepository) );
    session.setTransferListener( new LoggingTransferListener() );
    session.setRepositoryListener( new LoggingRepositoryListener() );

    final List<Artifact> artifacts = new ArrayList<Artifact>();

    system.collectDependencies(session, collectRequest).getRoot().accept( new DependencyVisitor() {
        public boolean visitEnter(DependencyNode dependencyNode) {
            artifacts.add(dependencyNode.getDependency().getArtifact());
            return true;
        }

        public boolean visitLeave(DependencyNode dependencyNode) {
            return true;
        }
    });
    return artifacts;
}
于 2013-03-04T17:38:22.847 回答
0

Aether jest 实际上非常简单和优雅的方式来做到这一点。这是 Maven 3 的主要增强功能之一,许多人都在寻找它。查看这个以获取一些可以使用的初始代码。我不记得有一种方法可以获取工件的确切 URL,但 AFAIR 支持其他要求。

于 2012-05-10T15:01:53.737 回答
0

你试过使用Apache Ivy吗?它支持获取 Maven 依赖项。

这个用例的文档有点稀疏,但我在这里找到了一些关于 Ivy 编程使用的信息。

于 2012-05-10T15:07:25.990 回答