Running tar task multiple times for different environments?


(Paul Grove) #1

I have a problem where I need to create a tar file of properties files that have been filtered. I have the code below which works great for a single tar but I now need to produce multiple tar’s one per environment we support. Struggling to see the best way to do this?

task confTar(dependsOn:‘initConfig’, type:Tar){

def fileSuffix= “conf-${environment}”

archiveName = “asss_${version}.${svnRevision}-${fileSuffix}.tar”

from { “build/target/conf/${environment}”}

from(“build/target/${version}/${svnRevision}/conf/”){

include “${version}.${svnRevision}-conf.txt”

}

into ‘conf’

doFirst{

createVersionFile(‘conf’, svnRevision)

}

doLast{

checksum(archivePath)

} } task initConfig(type:Copy){

def filterProps

if(filterConfig){

println(‘Doing filtering’)

filterProps= new Properties()

new File(“src/assembly/filters/${environment}-filter.properties”).withInputStream {

stream -> filterProps.load(stream)

}

}

from(‘src/assembly/conf’) {

include ‘**/*.properties’

include ‘**/*.xml’

}

from(‘src/assembly/conf’) {

include ‘**/*.template’

if(filterConfig){

filter(ReplaceTokens, beginToken:’~’, endToken:’~’, tokens: filterProps)

rename (‘env-specific.properties.template’,‘env-specific.properties’)

rename (‘configuration.properties.template’,‘configuration.properties’)

rename (‘services_decoupled.xml.template’,‘services_decoupled.xml’)

}

}

from(“src/assembly/platforms/${platform}/conf”) {

include ‘**/*.template’

if(filterConfig){

filter(ReplaceTokens, beginToken:’~’, endToken:’~’, tokens: filterProps)

rename (‘wrapper.conf.template’,‘wrapper.conf’)

}

}

into ‘build/target/conf/’+environment

includeEmptyDirs = false }


(Luke Daley) #2

One of the benefits of having a programming language at your disposal is that you can use looping constructs…

["env1", "env2"].each { environment ->
   def enviromentCapitalized = environment.capitalize()
    task("conf${enviromentCapitalized}", dependsOn: "init${enviromentCapitalized}Config", type: Tar) {
    …
  }
    task("init${enviromentCapitalized}Config", type: Copy){
    …
  }
}

(Paul Grove) #3

Same thought came to me after I posted, thanks for the code example it help a lot. i now have this all working. Goodbye my Maven assembly xml files


(Luke Daley) #4

Glad it’s working for you.