Created
January 19, 2017 00:40
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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