java 是否有用于读取 maven2/3 pom xml 文件的库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4838591/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a library for reading maven2/3 pom xml files?
提问by Skarab
I would like to read a pom.xml in Java code. I wonder if there is a library for that, so I can have an iterator for different sections, e.g., dependenes, plugins, etc. I want to avoid to build a parser by hand.
我想在 Java 代码中读取 pom.xml。我想知道是否有一个库,所以我可以有一个用于不同部分的迭代器,例如依赖项、插件等。我想避免手动构建解析器。
采纳答案by dogbane
You can try MavenXpp3Readerwhich is part of maven-model. Sample code:
您可以尝试MavenXpp3Reader,它是maven-model 的一部分。示例代码:
MavenXpp3Reader reader = new MavenXpp3Reader();
Model model = reader.read(new FileReader(mypom));
回答by Brett Porter
Firstly, I'm assuming you are not already running inside a Maven plugin, as there are easier ways to achieve that with the available APIs there.
首先,我假设您还没有在 Maven 插件中运行,因为有更简单的方法可以使用那里的可用 API 来实现。
The MavenXpp3Reader
solution posted earlier will allow you to read the POM easily, however does not take into account inheritance of the parent and interpolation of expressions.
MavenXpp3Reader
之前发布的解决方案将允许您轻松阅读 POM,但没有考虑父级的继承和表达式的插值。
For that, you would need to use the ModelBuilderclass.
为此,您需要使用ModelBuilder类。
Use of this is quite simple, for example from Archivais this code fragment:
使用这个非常简单,例如来自Archiva的这段代码片段:
ModelBuildingRequest req = new DefaultModelBuildingRequest();
req.setProcessPlugins( false );
req.setPomFile( file );
req.setModelResolver( new RepositoryModelResolver( basedir, pathTranslator ) );
req.setValidationLevel( ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL );
Model model;
try
{
model = builder.build( req ).getEffectiveModel();
}
catch ( ModelBuildingException e )
{
...
}
You must do two things to run this though:
你必须做两件事来运行它:
- instantiate and wire an instance of
ModelBuilder
including its private fields - use one of Maven's resolvers for finding the parent POMs, or write your own (as is the case in the above snippet)
- 实例化并连接一个
ModelBuilder
包含其私有字段的实例 - 使用 Maven 的解析器之一来查找父 POM,或编写自己的(如上述代码段中的情况)
How best to do that depends on the DI framework you are already using, or whether you want to just embed Maven's default container.
如何最好地做到这一点取决于您已经使用的 DI 框架,或者您是否只想嵌入 Maven 的默认容器。
回答by GKelly
This depends on what you're trying to achieve. If you just want to treat it as an XML with embedded XML files, go with suggestions already offered.
这取决于您要实现的目标。如果您只想将其视为带有嵌入式 XML 文件的 XML,请使用已经提供的建议。
If you are looking to implement some form of Maven functionality into your app, you could try the new aetherlibrary. I haven't used it, but it looks simple enough to integrate and should offer Maven functionality with little effort on your part.
如果您希望在您的应用程序中实现某种形式的 Maven 功能,您可以尝试新的aether库。我没有使用过它,但它看起来很简单,可以集成并且应该可以轻松提供 Maven 功能。
BTW, this library is a Maven 3 lib, not Maven 2 (as specified in your tag). Don't know if that makes much difference to you
顺便说一句,这个库是一个 Maven 3 库,而不是 Maven 2(在你的标签中指定)。不知道这对你是否有很大影响