Created
April 24, 2012 09:19
-
-
Save dahernan/2478195 to your computer and use it in GitHub Desktop.
Groovy XML pom manipulation
This file contains hidden or 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
/* | |
Script to increase the version number of the project and remove snapshots | |
Using Gmaven: http://docs.codehaus.org/display/GMAVEN/Executing+Groovy+Code | |
Execute standalone | |
$ mvn groovy:execute | |
<plugin> | |
<groupId>org.codehaus.gmaven</groupId> | |
<artifactId>gmaven-plugin</artifactId> | |
<version>1.3</version> | |
<configuration> | |
<source>${pom.basedir}/src/main/script/release_pom.groovy</source> | |
</configuration> | |
</plugin> | |
Execute in a lifecycle | |
<plugin> | |
<groupId>org.codehaus.gmaven</groupId> | |
<artifactId>gmaven-plugin</artifactId> | |
<version>1.3</version> | |
<executions> | |
<execution> | |
<phase>???????</phase> | |
<goals> | |
<goal>execute</goal> | |
</goals> | |
<configuration> | |
<source>${pom.basedir}/src/main/script/release_pom.groovy</source> | |
</configuration> | |
</execution> | |
</executions> | |
</plugin> | |
*/ | |
import groovy.xml.StreamingMarkupBuilder | |
import groovy.util.XmlNodePrinter | |
import org.codehaus.groovy.tools.xml.DomToGroovy | |
import groovy.xml.XmlUtil | |
println "Release Script Start -----" | |
def dir = new File(project.basedir, 'pom.xml') | |
// copy a backup of the pom | |
println "A copy of the old pom is saved in pom.backup.xml" | |
ant.copy(file: dir, tofile: "pom.backup.xml") | |
def pom = new XmlSlurper().parse(dir) | |
// increase version number | |
def version = pom.version.toString().replace("-SNAPSHOT", "").split("\\.") | |
version[-1] = version[-1].toInteger()+1 | |
println "Previoues version of pom: ${pom.version}" | |
pom.version = version.join('.') | |
println "New Version of pom: ${pom.version}" | |
// remove snapshots in properties | |
print "remove snapshots in properties " | |
pom.properties.childNodes().each{ | |
it.replaceBody(it.text().replace("-SNAPSHOT", "")) | |
} | |
println "OK" | |
// remove snapshots in dependencies | |
print "remove snapshots in dependencies " | |
pom.dependencies.dependency.each{ | |
it.version.replaceBody(it.version.text().replace("-SNAPSHOT", "")) | |
} | |
println "OK" | |
// output the pom | |
print "Writing new pom.xml " | |
def outputBuilder = new StreamingMarkupBuilder() | |
String result = outputBuilder.bind{ | |
mkp.declareNamespace("": "http://maven.apache.org/POM/4.0.0") | |
mkp.yield pom | |
} | |
def writer = dir.newWriter() | |
writer << XmlUtil.serialize(result) | |
writer.close() | |
println "OK" | |
println "Release Script End -----" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment