基于apache的maven-model依赖包和org.codehaus.plexus的plexus-utils工具依赖包(如:plexus-utils-3.3.0.jar,否则提示“org.codehaus.plexus.util.xml.pull.XmlPullParserException”依赖),通过org.apache.maven.model.Model的模型和org.apache.maven.model.io.xpp3.MavenXpp3Reader读取解析pom文件,获取jar的groupId、version、artifactId等信息,代码示例如下所示
package com.xwood.craw.crawler.util;@b@@b@import java.io.File;@b@import java.io.FileInputStream;@b@import java.io.InputStream;@b@import org.apache.maven.model.Model;@b@import org.apache.maven.model.io.xpp3.MavenXpp3Reader;@b@@b@import com.xwood.craw.crawler.persist.pojo.JarInfoDTO;@b@@b@public class PomParseUtil { @b@ @b@ // org.codehaus.plexus.util.xml.pull.XmlPullParserException@b@ public static void main(String[] args) throws Exception {@b@ @b@ InputStream fis = new FileInputStream(new File("C:/temp/maven-model-3.0.5.pom"));@b@ MavenXpp3Reader reader = new MavenXpp3Reader();@b@ Model model = reader.read(fis);@b@ @b@ String artifactId = model.getArtifactId();@b@ System.out.println("artifactId@"+artifactId);@b@ @b@ String groupId = model.getParent().getGroupId();@b@ System.out.println("groupId@"+groupId);@b@ String version = model.getParent().getVersion();@b@ System.out.println("version@"+version);@b@@b@ }@b@@b@}
控制台打印结果
artifactId@maven-model@b@groupId@org.apache.maven@b@version@3.0.5