Setting up property from a task to use in another which depends on

Hi,

I am facing a problem for some time regarding properties set and how dependent tasks are used. I post here my use case.

I have a compileJava task customized to use or not Eclipse compiler based on a $project.ext property.

compileJava {

   dependsOn createDirs, processResources
   setDefaultProperty("useEclipseCompiler", false)

  doFirst {

    def useEclipseCompiler = Boolean.parseBoolean("$project.ext.useEclipseCompiler")

    if (useEclipseCompiler) {
      println "Compiling main Java source using Eclipse compiler..."
      ...
    } else {
      println "Compiling main Java source..."
      ...   
    }
  }
}

Then, when run in Eclipse, I execute Gradle using an specific aggregated task which invokes several others for a dev build, including compileJava. When running outside Eclipse, a different set of tasks is invoked surrounding compileJava. We don’t want to use command line properties for this. The task invoked for a devBuild from Eclipse is:

task vagrantBuildDeploy() {

   dependsOn stopVagrantTomcat, devBuild, startVagrantTomcat

   devBuild.mustRunAfter stopVagrantTomcat
   startVagrantTomcat.mustRunAfter devBuild

   doFirst {
     println "Restarting and launching Tomcat..."
   }
 }

devBuild calls in its chain compileJava

Making this using the Eclipse compiler would be as simple as set the property project.ext.useEclipseCompiler = true when this specific task is invoked. But this is not possible, as if I put this in the regular {} block, it is executed at configuration time (not valid) and if I put it in the doFirst block it runs after the dependent task, reading the wrong value.

I would like the best practice to do this. I come up with a way, setting up a specific task that sets the property and is run BEFORE all of the other sin the dependency chain:

     task setEclipseCompiler() {	
	doFirst {
		project.ext.useEclipseCompiler = true
	}
     }

    task vagrantBuildDeploy() {

       dependsOn setEclipseCompiler, stopVagrantTomcat, devBuild, startVagrantTomcat
   
       devBuild.mustRunAfter setEclipseCompiler
       devBuild.mustRunAfter stopVagrantTomcat
       startVagrantTomcat.mustRunAfter devBuild
   
       doFirst {
   	  println "Restarting and launching Tomcat..."
      }
   }

Isn’t there any other way to setup a execution-defined setup for a property so that, when a specific task is executed a property is setup before anything else in that execution so it can be used by dependent tasks? I know this could be possible by using command line parameters, but this would not be available in this case.

Thanks in advance.

If I understand you correctly, you want compileJava to use Eclipse when vagrantBuildDeploy is going to be executed?

This should help: http://stackoverflow.com/questions/10554950/custom-conditional-configuration-for-gradle-project

The idea is that you can use the taskGraph to know if a particular task is going to be executed. If it is, you can then configure compileJava in a special way.

Thank you @sterling, I’ll give it a go, didn’t know about taskGraph manipulation.