我一直在尝试解析 maven pom.xml。我在一定程度上是成功的。但我的问题是我无法获得默认值。我必须手动注入默认值。例如,如果 pom.xml 中没有指定版本号,那么很明显会使用父版本。如果没有指定构建目录,那么它将是目标。
我需要知道,我应该使用哪些类来自动填充这些东西。除此之外,我想为我构建依赖关系图。我用来解析的代码是
Model model = null;
FileReader reader = null;
MavenXpp3Reader mavenreader = new MavenXpp3Reader();
try {
reader = new FileReader(pomfile);
model = mavenreader.read(reader);
model.setPomFile(pomfile);
if (model.getBuild() != null) {
// location of output directory basically target
if (model.getBuild().getDirectory() == null) {
//set the build directory
} // location of compiled classes
if (model.getBuild().getOutputDirectory() == null) {
// set the classes directory
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
throw new RuntimeException("pom file not found " + pomfile);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("pom file is not accessible " + pomfile);
} catch (XmlPullParserException e) {
e.printStackTrace();
throw new RuntimeException("unable to parse pom " + pomfile);
} catch (NullPointerException nullPointerException)
System.out.println("Exception setting build dir "+model.getPackaging());
} finally {
if (reader != null) {
reader.close();
}
}
谢谢。