5

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.

How can you find the latest version of a maven artifact from Java using aether?

Community
  • 1
  • 1
javydreamercsw
  • 5,363
  • 13
  • 61
  • 106

2 Answers2

6

The Aether Team maintains a demo page with such an example: FindNewestVersion.

Simplified a bit, this is what it comes down to.

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

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.

Tunaki
  • 132,869
  • 46
  • 340
  • 423
2

This is from the project's Aether Demonstration and Examples site. I didn't try to run it, but it should be your answer.

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 ) );
}
K.Nicholas
  • 10,956
  • 4
  • 46
  • 66
  • 1
    I linked to that example in my answer yes and simplified it with a full working code (because this code is not self-contained, there are references to `Booter.newRepositories` for example). – Tunaki Feb 19 '16 at 18:29
  • The link to the demonstrations and examples no longer works – ssimm Aug 08 '23 at 15:41