Java Dude
Java Dude

Reputation: 596

How to move code from gradle task configuration step to task execution

I have gradle task:

task immportMyData(type: Exec) {
def dumnp= ""

new File("${System.env.MY_HOME}/export").eachDir() { dir ->
    dumpName = dir.getName()
}

workingDir "${System.env.MY_HOME}/../test"
standardOutput = new ByteArrayOutputStream()

 ext.output = {
    return standardOutput.toString()
 } 
}

...

As I understand its task configuration, because its executed every time.

So, is there any way how to move code from conflagration step to task body (execution step) ? Some thinks like this.

task immportMyData(type: Exec) << {
   //code from configuration
}

another worlds 

   task immportMyData(type: Exec) **<<** {
def dumnp= ""

new File("${System.env.MY_HOME}/export").eachDir() { dir ->
    dumpName = dir.getName()
}

workingDir "${System.env.MY_HOME}/../test"
standardOutput = new ByteArrayOutputStream()

 ext.output = {
    return standardOutput.toString()
 } 
}

I've checked gradle docs, but no luck

Upvotes: 0

Views: 69

Answers (1)

user31601
user31601

Reputation: 2610

I think you may have misunderstood what the Exec task does. Exec is for running an external command, i.e. another process. As such, it needs you to specify its commandLine property in order for it to do anything at execution time. See here for more info.

It looks like what you're actually trying to do is "run some code during task execution", which can be achieved using a regular (non-Exec) task. Something like this:

task importMyData {
    //Code to configure task

    doLast {
        //Code to run at execution time, maybe this?:
        new File("${System.env.MY_HOME}/export").eachDir() { dir ->
            ext.dump = dir.getName()
        }
    }
}

Hope this helps.

Upvotes: 1

Related Questions