How to edit mob maven at runtime?

I need to edit POM at runtime. I used Dom4j to read pom and after that installed some data. But I need to know if there is another form for this. Are there maven utilities for this?

+7
maven runtime editing
source share
3 answers

Use MavenXpp3Reader to read and MavenXpp3Writer to write Model . A 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 note that any comment, extra spaces or lines will be deleted from the file.

+9
source share

Depending on what you are changing, there may be maven plugins. For example, the maven release plugin updates version information in the pom.xml file and checks for changes in version control.

Try to find the specific task you are trying to accomplish (for example, "updating the maven plugin version number"), rather than the more general "change pom.xml".

+1
source share

This code works for me:

 package or.jrichardsz; import java.io.FileReader; import java.io.FileWriter; import java.io.Writer; import org.apache.maven.model.Model; import org.apache.maven.model.Plugin; import org.apache.maven.model.io.xpp3.MavenXpp3Reader; import org.apache.maven.model.io.xpp3.MavenXpp3Writer; public class TestMavenPomEdit { public static void main(String[] args) throws Exception { //read initial pom Model model = parsePomXmlFileToMavenPomModel("C:\\Users\\User\\Desktop\\initial_pom.xml"); //add some pom modification Plugin plugin = new Plugin(); plugin.setGroupId("com.jelastic"); model.getBuild().addPlugin(plugin); //write new pom parseMavenPomModelToXmlString("C:\\Users\\User\\Desktop\\final_pom.xml", model); } public static Model parsePomXmlFileToMavenPomModel(String path) throws Exception { Model model = null; FileReader reader = null; MavenXpp3Reader mavenreader = new MavenXpp3Reader(); reader = new FileReader(path); model = mavenreader.read(reader); return model; } public static void parseMavenPomModelToXmlString(String path,Model model) throws Exception { MavenXpp3Writer mavenWriter = new MavenXpp3Writer(); Writer writer = new FileWriter(path); mavenWriter.write(writer, model); } } 

TestMavenPomEdit.java

NTN

0
source share

All Articles