2015-04-29 31 views
1

在很多文章中,我看到Aether项目有助于处理工件存储库。我想要的是只检索指定的groupIdartifactId的最高版本。如何从本地存储库以编程方式获取Maven工件的最高版本?

在奥德维基他们目前的org.apache.maven的情况下:Maven的配置文件:2.2.1神器,他们还指定版本:

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

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

+0

你想做什么?你的用例是什么? –

+0

我自定义的Maven插件我知道工件的groupId和artifactId,我想在本地存储库(.m2)中获得最高安装版本。最基本的方法是将本地存储库作为路径传递并根据已知的ID进行遍历。但如果可以用一个优雅的解决方案,我宁愿这样做。 SK –

回答

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; 
} 
相关问题