Skip to content

Instantly share code, notes, and snippets.

@kares
Created January 17, 2010 19:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kares/279512 to your computer and use it in GitHub Desktop.
Save kares/279512 to your computer and use it in GitHub Desktop.
def overrideResourcesDir = project.properties["override.resources.dir"]
if (new File( overrideResourcesDir ).exists()) {
log.info("cleaning contents of directory $overrideResourcesDir")
ant.delete { fileset(dir: overrideResourcesDir, includes: "**") }
}
else {
new File( overrideResourcesDir ).mkdirs() // webResource - should exist
}
def SOME_PORT = project.properties["SOME_PORT"]
if (SOME_PORT) { // if not set don't touch the web.xml
def warPluginConfig = project.build.plugins.find { p ->
p.artifactId == "maven-war-plugin"
}.configuration // class org.codehaus.plexus.util.xml.Xpp3Dom
def warSourceDir = warPluginConfig.getChildren("warSourceDirectory")
warSourceDir = warSourceDir ?
warSourceDir[0].value : "${project.basedir}/src/main/webapp"
// read the web.xml content :
def webXmlFile = new File("$warSourceDir/WEB-INF/web.xml")
def webXml = webXmlFile.readLines().join("\n")
// should parse XML but this will serve to make the point :
def match = webXml =~ /(?s)(.*?SOME_PORT.*?value.)\d+(.\/param\-value.*+)/
log.info("Replacing web.xml SOME_PORT parameter-value with: $SOME_PORT")
webXml = match[0][1] + SOME_PORT + match[0][2]
// pu the modified web.xml in the web resource dir :
new File("$overrideResourcesDir/WEB-INF").mkdirs()
webXmlFile = new File( "$overrideResourcesDir/WEB-INF/web.xml" )
webXmlFile.createNewFile()
webXmlFile.write( webXml )
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment