How to get programmatically the highest version for a Maven artifact from my local repository?

in many posts I saw that the Aether project helps in working with artifact repositories. What I would like is to retrieve the highest version for a specified groupId and artifactId only.

In Aether wiki they present a case for the org.apache.maven:maven-profile:2.2.1 artifact where they specify also the version:

Dependency dependency = 
    new Dependency(
        new DefaultArtifact("org.apache.maven:maven-profile:2.2.1"),
        "compile"
    );

But I need to get back the version, the highest version for an artifact. How could I do this?

Answers


If you can read the pom.xml file, you can do it with bare xml parsing.

public static String getVersionOf(File pomFile) throws ParserConfigurationException, IOException, SAXException {
    String version = "";

    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(pomFile);

    NodeList nodeList = doc.getElementsByTagName("project");

    for(int i = 0; i < nodeList.getLength(); i++) {
        Element node = (Element) nodeList.item(i);

        version = node.getElementsByTagName("version").item(0).getTextContent();
    }

    return version;
}

Need Your Help