Home > Enterprise >  Executing custom Gradle task using scripts
Executing custom Gradle task using scripts

Time:11-21

How to write the task in the build.gradle which will execute this code:

**gradle hideSecret -Pkey=test123456 -PkeyName=YourSecretKeyName**

I use this lib: https://github.com/klaxit/hidden-secrets-gradle-plugin and I want to make the hideSecrets automatically in pre build.

Smth like this:

task hideSecret {
    doLast {
        exec {
            workingDir "${rootDir}"
            commandLine "hideSecret -Pkey=${key} -PkeyName=${keyName}"
        }
    }
}

afterEvaluate {
    tasks.getByName("preBuild").dependsOn("hideSecret")
}

CodePudding user response:

You don't need to redefine the hideSecret task. Just set the properties as you wish and configure the dependency:

afterEvaluate {
    project.ext.key = 'test123456'
    project.ext.keyName = 'YourSecretKeyName'
    tasks.getByName("preBuild").dependsOn("hideTask")
}

CodePudding user response:

You can define a task with Exec type

task executeCMD(type:Exec) {
  workingDir '.'
  commandLine 'gradle', 'hideSecret', '-Pkey=test123456', '-PkeyName=YourSecretKeyName'
     doLast {
         println "Executed!"
     }
 }

OR you can use it with gradle project exec like this

 task executeCMD{
     doLast {
         exec {
             workingDir "."
             executable 'gradle'
             args 'hideSecret', '-Pkey=test123456'', '-PkeyName=YourSecretKeyName'
         }
         println "Executed!"
     }
 }

More information about excuting can be found at gradle official doc .

  • Related