How to run task after build is created in jenkins through gradle script

Viewed 307

I have a build.gradle script which includes a couple of tasks and one last task is as below. The last task will create a zip file and now I need to copy this into destination directory with the different zip file names. I tried couple of functions to accomplish this but the build is failing with different types of errors.

Task in build.gradle:

task createDBDeploy(type: Zip, dependsOn: 'copyToDbfiles') {
    System.out.println("Starts createDBDeploy");
    includeEmptyDirs = false
    zip64 true
    archiveName = "dbfiles.zip"
    destinationDir = file("${buildDir}")
    System.out.println("Destination directory :${buildDir}");
    from "${buildDir}/dbfiles"
}

After I ran the above task I need to run the below task. I need the above file dbfiles.zip to be copied into destination dir "${buildDir}" with different names called abc.zip, def.zip, feg.zip, ijk.zip. Could you please help me with the task which does the above?

I tried by using doLast and finalizedBy functions but my Jenkins builds are failing. So I am not sure how to execute the above task after my build is created.

I tried the below ways or methods:

1st method: Added the below task in gradle.build and ran the jenkins job but build is failing.

task copyFiles(type: Copy) {
    from file("$buildDir/dbfiles.zip")
    into file("$buildDir/abc.zip") 
    from file("$buildDir/dbfiles.zip")
    into file("$buildDir/def.zip")
    from file("$buildDir/dbfiles.zip")
    into file("$buildDir/feg.zip") 
    from file("$buildDir/dbfiles.zip")
    into file("$buildDir/ijk.zip")
}

build.dependsOn copyFiles

2nd method: Added the below task in gradle.build and ran the jenkins job but build is failing.

task renameArtifacts(type: Copy) { 
    from ('$buildDir/') include 'dbfiles.zip' 
    destinationDir file('$buildDir/abc.zip')
    from ('$buildDir/') include 'dbfiles.zip' 
    destinationDir file('$buildDir/def.zip')
    from ('$buildDir/') include 'dbfiles.zip' 
    destinationDir file('$buildDir/feg.zip')
    from ('$buildDir/') include 'dbfiles.zip' 
    destinationDir file('$buildDir/ijk.zip')
}

build.dependsOn renameArtifcats

3rd method: Added the below task in gradle.build and ran the jenkins job but build is failing.

task finalize {
    doLast {
        from file("$buildDir/dbfiles.zip")
        into file("$buildDir/abc.zip") 
        from file("$buildDir/dbfiles.zip")
        into file("$buildDir/def.zip")
        from file("$buildDir/dbfiles.zip")
        into file("$buildDir/feg.zip") 
        from file("$buildDir/dbfiles.zip")
        into file("$buildDir/ijk.zip")
    }
}

build.finalizedBy finalize

After I tried all the ways I am getting the error while running build when I ran the above steps.

Jenkins error log:

FAILURE: Build failed with an exception.
22:34:54  
22:34:54  * Where:
22:34:54  Build file '/apps/jenkins_slave/workspace/proj/mod1/build.gradle' line: 292
22:34:54  
22:34:54  * What went wrong:
22:34:54  A problem occurred evaluating root project 'mod1'.
22:34:54  > Could not get unknown property 'build' for root project 'mod1' of  type org.gradle.api.Project.
1 Answers

In other words, you’d like to copy the same ZIP file to four new files? The following Copy task implementation should do that (tested with Gradle 6.7.1):

task copyFiles(type: Copy) {
    into buildDir
    from(buildDir) {
      include 'dbfiles.zip'
      rename 'dbfiles.zip', 'abc.zip'
    }
    from(buildDir) {
      include 'dbfiles.zip'
      rename 'dbfiles.zip', 'def.zip'
    }
    from(buildDir) {
      include 'dbfiles.zip'
      rename 'dbfiles.zip', 'feg.zip'
    }
    from(buildDir) {
      include 'dbfiles.zip'
      rename 'dbfiles.zip', 'ijk.zip'
    }
}

Or more concisely:

task copyFiles(type: Copy) {
    into buildDir
    ['abc', 'def', 'feg', 'ijk'].each { name ->
        from(buildDir) {
          include 'dbfiles.zip'
          rename 'dbfiles.zip', "${name}.zip"
        }
    }
}
Related