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

后端 未结 2 346
一整个雨季
一整个雨季 2020-11-29 12:41

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

相关标签:
2条回答
  • 2020-11-29 13:03

    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.

    0 讨论(0)
  • 2020-11-29 13:12

    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 ) );
    }
    
    0 讨论(0)
提交回复
热议问题