Skip to content

Instantly share code, notes, and snippets.

@jrichardsz
Created January 19, 2017 00:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jrichardsz/78aca4ca14b0dec477913ae2b2b72b0c to your computer and use it in GitHub Desktop.
Save jrichardsz/78aca4ca14b0dec477913ae2b2b72b0c to your computer and use it in GitHub Desktop.
How to edit a maven POM at runtime? How to edit a maven POM file? Add dinamically plugins to maven pom file.
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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment