Search code examples
javadeploymentaether

List available versions from sonatype nexus with aether-api


my goal is to develop an internal tool for artifact deploying. The artifacts are located in a local repository management system (sonatype nexus). After researching, I tried to implement the task, with Aether-Api. But I failed at reprogramming one of their examples at my own. I can't evaluate the error.

public class SourceMaven 
{
    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("/usr/local/home/myusername/tmp/aether");
        session.setLocalRepositoryManager(system.newLocalRepositoryManager(session, localRepo));

        return session;        
    }
    private static List<RemoteRepository> newRepositories( RepositorySystem system, RepositorySystemSession session)
    {
        return new ArrayList<RemoteRepository>(Arrays.asList(newCentralRepository()));
    }
    private static RemoteRepository newCentralRepository()
    {
        return new RemoteRepository.Builder("sonanexus", "default", "http://ournexusservername:8081/nexus/#nexus").build();
    }


    public List<String> getReleaseList(String url) 
    {
        RepositorySystem system = newRepositorySystem();
        RepositorySystemSession session = newSession(system);

        Artifact artifact = new DefaultArtifact("org.eclipse.aether:aether-util:[0,)");

        VersionRangeRequest rangeRequest = new VersionRangeRequest();
        rangeRequest.setArtifact(artifact);
        rangeRequest.setRepositories(newRepositories(system, session));

        try 
        {
            VersionRangeResult rangeResult = system.resolveVersionRange(session, rangeRequest);
            List<Version> versions = rangeResult.getVersions();

            System.out.println("available versions " + versions);
        } 
            catch (VersionRangeResolutionException ex) 
        {
            System.out.println("failed ...");
        }

        return null;
    }
}

As output I only get an empty List without an error.

available versions []

The requestested artifact coordinates are linked in our nexus and can be found in the webinterface.


Solution

  • Code above is working.

    It is important to clarify the url more specific so instead of

    http://hostname:8081/nexus/#nexus

    i needed to change it to:

    http://hostname:8081/nexus/content/groups/public