1

在许多帖子中,我看到Aether项目有助于处理工件存储库。我想要的是仅检索指定groupIdartifactId的最高版本。

在 Aether wiki 中,他们为org.apache.maven:maven-profile:2.2.1工件提供了一个案例,其中还指定了版本:

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

但我需要取回版本,一个工件的最高版本。我怎么能这样做?

4

1 回答 1

0

如果您可以读取 pom.xml 文件,则可以使用裸 xml 解析来完成。

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;
}
于 2015-12-03T13:20:48.977 回答