如何以编程方式从本地存储库获取Maven工件的最高版本?
在许多帖子中,我看到该项目有助于使用工件存储库。我想要的是仅检索指定的groupId和artifactId的最高版本 在Aether wiki中,他们展示了org.apache.maven:maven profile:2.2.1工件的一个案例,其中还指定了版本:如何以编程方式从本地存储库获取Maven工件的最高版本?,maven,artifact,aether,Maven,Artifact,Aether,在许多帖子中,我看到该项目有助于使用工件存储库。我想要的是仅检索指定的groupId和artifactId的最高版本 在Aether wiki中,他们展示了org.apache.maven:maven profile:2.2.1工件的一个案例,其中还指定了版本: Dependency dependency = new Dependency( new DefaultArtifact("org.apache.maven:maven-profile:2.2.1"),
Dependency dependency =
new Dependency(
new DefaultArtifact("org.apache.maven:maven-profile:2.2.1"),
"compile"
);
但是我需要得到版本,一个工件的最高版本。我怎样才能做到这一点呢?如果您可以阅读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;
}
你想干什么?你的用例是什么?我有一个定制的Maven插件,我知道一个工件的groupId和artifactId,我想在本地存储库中获得它的最高安装版本。最基本的方法是将本地存储库作为路径传递,并根据已知ID遍历它。但如果有一个优雅的解决方案,我更愿意这样做。SK