Problem with tasks flow (Gradle 1.4)

I have the following JavaExec task:

task(myMainTask, dependsOn: 'subtask', type: JavaExec) {
  println "run myMainTask!"
  main = 'com.Test'
  classpath = sourceSets.main.runtimeClasspath
   //....
}

The above task is ALWAYS run since it fails if I add the:

"(...) << {}"

The myMainTask depends on subtask which is given by:

task (subtask, type: Copy) <<
{
  println "we use << to only run subtask when runnig myMainTaskrunning"
}

This task should only be run if myMainTask is run. Therefore I use:

<<

But it does not work. Only if I remove:

<<

from subtask is it executed. But then its always removed.

How is a dependent task (subtask) ONLY run if its calling task (myMainTask) is run?

The thing you are missing is the distinction between configuration time and execution time.

Configuration time code is always executed for every build. In your very first example, the println is happening during the configuration phase. This is correct, because at configuration time you need to configure the task. In this case, it’s configure what will be "exec"d and how.

When the task actually gets executed, that’s when your Java process will run.

There’s some info about the lifecycle here: http://www.gradle.org/docs/current/userguide/build_lifecycle.html

I can’t quite understand what you are specifically asking. It might pay to have a read of that chapter.