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.
POM is an acronym for Project Object Model. The pom. xml file contains information of project and configuration information for the maven to build the project such as dependencies, build directory, source directory, test source directory, plugin, goals etc. Maven reads the pom.
The pom. xml is placed in the projects root-folder.
Yes you can use Maven Profiles to manage this. Obviously you can tweak this approach to suit your needs however works best.
POM stands for Project Object Model, and it is the core of a project's configuration in Maven. It is a single configuration XML file called pom. xml that contains the majority of the information required to build a project.
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.
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.
For that, you would need to use the ModelBuilder class.
Use of this is quite simple, for example from Archiva is this code fragment:
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:
ModelBuilder
including its private fieldsHow best to do that depends on the DI framework you are already using, or whether you want to just embed Maven's default container.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With