1
我一直在試圖解析maven pom.xml。我在一定程度上取得了成功。但我的問題是我無法獲得默認值。我必須手動注入默認值。 例如,如果未在pom.xml中指定版本號,那麼顯然將使用父版本。 如果沒有指定build目錄,那麼它將成爲目標。使用maven jar解析maven 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();
}
}
謝謝。