How to loop the tasks in gradle

apply plugin: 'base'


task tokenReplace(type:Copy){
    from "src/main/resources/AAA/"
    into ("$buildDir/tokenReplace/Instance1")
    def Instance1 = new Properties()
    file("Instance1.properties").withInputStream{
        Instance1.load(it);   
    }
	println "Packaging for Instance1"
    filter(org.apache.tools.ant.filters.ReplaceTokens, tokens: Instance1)
	
	 from "src/main/resources/BBB/"
    into ("$buildDir/tokenReplace/Instance2")
    def Instance2 = new Properties()
    file("Instance2.properties").withInputStream{
        Instance2.load(it);   
    }
    println "Packaging for Instance2"
	filter(org.apache.tools.ant.filters.ReplaceTokens, tokens: Instance2)
			
}

I want to create two sets of packages for Instance1 and Instance 2... Instance1 files are from folder AAA and Instance2 files are folder BBB. How can i achieve this in gradle ?

You can use Groovy code in your build script (build.gradle files are Groovy), so you have access to Groovy’s lists, maps, loops, etc.

def packages = [Instance1: 'AAA', Instance2: 'BBB']

packages.each { instance, resourceDir ->
    def trTask = tasks.register( "tokenReplace${instance}", Copy ) {
        from( "src/main/resources/${resourceDir}" )
        into( "${buildDir}/tokenReplace/${instance}" )
        def props = new Properties()
        file( "${instance}.properties" ).withInputStream {
            props.load( it );
        }
        filter( org.apache.tools.ant.filters.ReplaceTokens, tokens: props )
    }
    tasks.register( "package${instance}", Zip ) {
        baseName = instance
        destinationDir = buildDir
        from( trTask )
    }
}

EDIT: the above uses the new incubating task API (introduced in Gradle 4.9). To use the original/old API, replace tasks.register with tasks.create.

Hi Chris,
Thank you so much for your reply and guidance. Below is my changed code.
Still, I could not able to generate packages. Can you look into this. Thanks a lot in advance.

def packages = ['devl':'devl', 'test:test':'acpt':'acpt']
packages.each { envpropFile, outputDir ->
    def trTask = tasks.create( "tokenReplace${outputDir}", Copy ) {
        from( "src/main/resources/" )
        into( "${buildDir}/tokenReplace/${outputDir}" )
        def props = new Properties()
        file( "${envpropFile}.properties" ).withInputStream {
            props.load( it );
        }
 filter( org.apache.tools.ant.filters.ReplaceTokens, tokens: props )
 }
}

my source folder is same for all environments package…only the output directory will contain 3 directories as devl, test and acpt . Each environment has different property file.
I am running the command as C:> gradle build to run the script

One issue I see is that the packages map looks like it has some typos in it. I think you meant:

def packages = ['devl':'devl', 'test':'test', 'acpt':'acpt']

Is that the issue, or is there something else not working?
When running gradle build, what do you want to happen?

When I run gradle build, what I would like to see is… output directory should be created with token-replacement for each env property file. Eg
devl=property file
devl=folder name
read devl property file, do token replacement and create a folder with the name again devl and I should see expected files here… so this task I would like to repeat for all envs. Thanks again for your help !

What is going to be done with output files of the token replace tasks?
For example, do you want them included in the jar file instead of the original files in src/main/resources (I’m assuming this is a Java project)?

After the successful token replace, I stage them on the target server… from there another process kicks in for further processing… so important step is token replacement and staging. Yes, you are correct… this is Java project … but this component is not java files but other source files… hence I placed them in resources not in main directory