如何使用以太从 Java 中找到最新版本的 Maven 工件? [英] How can you find the latest version of a maven artifact from Java using aether?
问题描述
他们的文档非常少,我无法弄清楚.
Their documentation is really slim and I was unable to figure it out.
我找到了部分答案 这里,但它没有所有的代码.
I found a partial answer here, but it doesn't have all the code.
如何使用以太从 Java 中找到最新版本的 Maven 工件?
How can you find the latest version of a maven artifact from Java using aether?
推荐答案
Aether 团队维护了一个演示页面,例如:FindNewestVersion
.
The Aether Team maintains a demo page with such an example: FindNewestVersion
.
简化一点,归结起来就是这样.
Simplified a bit, this is what it comes down to.
将 Aether 依赖项添加到您的 POM:
Add to your POM the Aether dependencies:
<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>
然后,您可以像这样使用它:
And then, you can use it like such:
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,)
来指定我们对具有无限最大值的所有版本感兴趣.最后,执行版本范围查询,这使我们能够确定最新版本.
This creates a reference to the Maven Central repository and uses the version ranges [0,)
to specify that we're interested in all versions with an unbounded maximal value. Finally, a version range query is performed and that enables us to determine the latest version.
这篇关于如何使用以太从 Java 中找到最新版本的 Maven 工件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!