gpt4 book ai didi

java - 您如何使用以太从 Java 中找到最新版本的 Maven Artifact ?

转载 作者:搜寻专家 更新时间:2023-11-01 01:50:28 24 4
gpt4 key购买 nike

他们的文档非常薄,我无法理解。

我找到了部分答案 here ,但它没有所有代码。

如何使用 aether 从 Java 中找到最新版本的 Maven Artifact ?

最佳答案

Aether 团队维护着一个包含这样一个示例的演示页面:FindNewestVersion .

稍微简化一下,这就是它的归结。

将 Aether 依赖项添加到您的 POM:

<dependencies>
<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-impl</artifactId>
<version>${aetherVersion}</version>
</dependency>
<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-connector-basic</artifactId>
<version>${aetherVersion}</version>
</dependency>
<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-transport-file</artifactId>
<version>${aetherVersion}</version>
</dependency>
<dependency>
<groupId>org.eclipse.aether</groupId>
<artifactId>aether-transport-http</artifactId>
<version>${aetherVersion}</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-aether-provider</artifactId>
<version>${mavenVersion}</version>
</dependency>
</dependencies>
<properties>
<aetherVersion>1.1.0</aetherVersion>
<mavenVersion>3.3.9</mavenVersion>
</properties>

然后,您可以像这样使用它:

public static void main(String[] args) {
RemoteRepository central = new RemoteRepository.Builder("central", "default", "http://repo1.maven.org/maven2/").build();
RepositorySystem repoSystem = newRepositorySystem();
RepositorySystemSession session = newSession(repoSystem);
Artifact artifact = new DefaultArtifact("groupId:artifactId:(0,]");
VersionRangeRequest request = new VersionRangeRequest(artifact, Arrays.asList(central), null);
try {
VersionRangeResult versionResult = repoSystem.resolveVersionRange(session, request);
System.out.println(versionResult.getHighestVersion());
} catch (VersionRangeResolutionException e) {
e.printStackTrace();
}
}

private static RepositorySystem newRepositorySystem() {
DefaultServiceLocator locator = MavenRepositorySystemUtils.newServiceLocator();
locator.addService(RepositoryConnectorFactory.class, BasicRepositoryConnectorFactory.class);
locator.addService(TransporterFactory.class, FileTransporterFactory.class);
locator.addService(TransporterFactory.class, HttpTransporterFactory.class);
return locator.getService(RepositorySystem.class);
}

private static RepositorySystemSession newSession(RepositorySystem system) {
DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession();
LocalRepository localRepo = new LocalRepository("target/local-repo");
session.setLocalRepositoryManager(system.newLocalRepositoryManager(session, localRepo));
return session;
}

这会创建对 Maven 中央存储库的引用,并使用版本范围 [0,) 来指定我们对具有无限最大值的所有版本感兴趣。最后,执行版本范围查询,这使我们能够确定最新版本。

关于java - 您如何使用以太从 Java 中找到最新版本的 Maven Artifact ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35488167/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com