Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to edit a maven POM at runtime?

I need editing POM at runtime. I used Dom4j for read pom and after that set some data. But i need know if exist another form for to do this. Exist a maven utilities for this?

like image 337
Daniela Avatar asked May 11 '10 14:05

Daniela


People also ask

How do you get Pom properties at runtime?

Use the properties-maven-plugin to write specific pom properties to a file at compile time, and then read that file at run time.

How do I edit a POM file?

Now when you edit the file in any good editor (including jEdit or Eclipse) the editor will actually download the . xsd file from the URL above and use it to guide you and validate the POM. e.g. in Eclipse, go somewhere in the POM and hit Ctrl-Space - it will bring up a list of valid elements for the current position.

How do I change POM xml version?

mvn scm:checkin -Dincludes=pom. xml -Dmessage="Setting version, preping for release." Then you can perform your release (I recommend the maven-release-plugin), after which you can set your new version and commit it as above. The versions plugin is your friend.


1 Answers

Use MavenXpp3Reader to read and MavenXpp3Writer to write Model objects. Simple example:

String baseDir = "/your/project/basedir/";

//Reading
MavenXpp3Reader reader = new MavenXpp3Reader();
Model model = reader.read(new FileInputStream(new File(baseDir, "/pom.xml")));

//Editing
model.setUrl("http://stackoverflow.com");

//Writing
MavenXpp3Writer writer = new MavenXpp3Writer();
writer.write(new FileOutputStream(new File(baseDir, "/pom.xml")), model);

And notice that any comment, extra white spaces or lines will be removed from the file.

like image 100
bluefoot Avatar answered Oct 04 '22 19:10

bluefoot