答案 0 :(得分:5)
Aether团队维护一个包含以下示例的演示页:FindNewestVersion
。
简化了一下,这就是它的归纳。
向您的POM添加Aether依赖项:
<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 Central存储库的引用,并使用版本范围[0,)
来指定我们对具有无限最大值的所有版本感兴趣。最后,执行版本范围查询,这使我们能够确定最新版本。
答案 1 :(得分:2)
这是来自项目的Aether Demonstration and Examples网站。我没有尝试过它,但它应该是你的答案。
public static void main( String[] args ) throws Exception
{
System.out.println( "------------------------------------------------------------" );
System.out.println( FindNewestVersion.class.getSimpleName() );
RepositorySystem system = Booter.newRepositorySystem();
RepositorySystemSession session = Booter.newRepositorySystemSession( system );
Artifact artifact = new DefaultArtifact( "org.eclipse.aether:aether-util:[0,)" );
VersionRangeRequest rangeRequest = new VersionRangeRequest();
rangeRequest.setArtifact( artifact );
rangeRequest.setRepositories( Booter.newRepositories( system, session ) );
VersionRangeResult rangeResult = system.resolveVersionRange( session, rangeRequest );
Version newestVersion = rangeResult.getHighestVersion();
System.out.println( "Newest version " + newestVersion + " from repository "
+ rangeResult.getRepository( newestVersion ) );
}