0

I have a custom task in Gradle (2.3)

task myCustomTask (dependsOn: [ jacocoTestReport ]) << {

   //Adding this didn't work, gives an error that options is not a property.
   //options.compilerArgs = ["-x compileJava -x classes -x test -x testClasses"]

   //Seems like the following line actually works!!! but still errors for "options" property. Strange!!
   //myCustomTask.options = [ "-x compileJava -x classes -x test -x testClasses" ]

   //..
   //...some...operation
   //..
}

How can I change the above custom task code in Gradle so that it can do what I'm doing at command line. I want that when someone calls myCustomTask and if it depends upon any of the Gradle's core tasks (like compileJava, classes etc), then it should not call those tasks (i.e. to mimic -x someTask behavior at command line).

The following works!!

$ gradle jacocoTestReport -x compileJava -x classes -x test -x testClasses

Then, what I want is: Running gradle myCustomTask should do the same (what the above command at command line is doing).

Error:

:compileJava UP-TO-DATE
:compileGroovy UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:jacocoTestReport
:myCustomTask FAILED

FAILURE: Build failed with an exception.

* Where:
Initialization script '/home/giga/gradle-2.3/init.d/extra1.common-thids.gradle' line: 450

* What went wrong:
Execution failed for task ':myCustomTask'.
> No such property: options for class: org.gradle.api.DefaultTask_Decorated
  Possible solutions: actions

cat -n on extra1..gradle file (init.d level file):

   449     task myCustomTask (dependsOn: [ jacocoTestReport ]) << {
   450          myCustomTask.options = [ " -x compileJava -x classes -x test -x testClasses " ]
   451     }
durron597
  • 31,968
  • 17
  • 99
  • 158
AKS
  • 16,482
  • 43
  • 166
  • 258

1 Answers1

0

I assume this is what you want:

  • Run the tests, generate a JaCoCo report and (optionally) do something with the report.
  • Do something with the JaCoCo report without running the tests again.

I think your best bet is to look at mustRunAfter instead of dependsOn: https://docs.gradle.org/2.3/userguide/more_about_tasks.html#sec:ordering_tasks

task myCustomTask() {
    mustRunAfter jacocoTestReport
    doLast {
        ...
    }
}

What can you do if you switch to mustRunAfter?

Run the tests and immediatly consume the JaCoCo report. Gradle will ensure that myCustomTask runs after the JaCoCo report task, so you can be sure that your custom tasks sees the latest JaCoCo output:

gradle jacocoTestReport myCustomTask

Consume a previously generated JaCoCo report. jacocoTestReport is not a dependency of myCustomTask, so jacocoTestReport, and the tasks it depends upon, will not be added to the task graph and won't be run. Only myCustomTask will be run:

gradle myCustomTask
Johan Stuyts
  • 3,607
  • 1
  • 18
  • 21
  • Thanks Johan. This is not what I want. I want how to "not call" some core tasks (while calling a custom task) which dependOn another core task that's dependent on my custom task. – AKS Jul 30 '15 at 21:39
  • OK, I found something and updated my question about my findings to better show what's it's doing. It seems like I'm close to what I want after the error is gone. – AKS Jul 30 '15 at 21:48
  • What you want is not a good idea. You are trying to do something with Gradle that Gradle was explicitly designed not to do: having builds that are an unreliable mess. I strongly advise you to not continue in the direction you are going. Tell us why you don't want the tasks that `jacocoTestReport` depends on to be run. And `options` is not available for every task, and even if it were for your task, you would not be able to add additional excludes in that way, because those are Gradle command line arguments and they have already been parsed and won't be parsed again. – Johan Stuyts Jul 31 '15 at 04:09
  • I have a situation! myCustomTask basically runs both jacocoTestReport and "sonarRunner" in it. thus, if I call myCustomTask in Gradle, it'll run both of these tasks. Good. Now, there are 400+ projects I have which are using Java6,7 and some newly upgraded ones are with Java8. We have Jenkins jobs all set to call the myCustomTask in Invoke Gradle. I found if don't run SonarQube instance with at least with JDK7/8, you can't publish. If you use Java7 to run myCustomTask, then jacocoTestReport task within myCustomTask doesn't do jacoco report generation (as classes are JDK8) for JDK8 project. – AKS Aug 05 '15 at 18:15
  • So what you want is: Run Gradle using Java 8, and compile some projects using Java 6 or 7. You can do this by specifying the source (and target) compatibility for the `CompileJava` task, and then setting the boot classpath in the `options` object of the task. See: http://stackoverflow.com/a/22681854/2622278 Make sure you include all relevant JARs from the JRE you want to compile against. Here is the documentation of `options`: https://docs.gradle.org/current/dsl/org.gradle.api.tasks.compile.JavaCompile.html#org.gradle.api.tasks.compile.JavaCompile:options – Johan Stuyts Aug 10 '15 at 18:30
  • I think it's a lot of work. I was just looking if there's a way to not call a task/customTask if it's dependent upon (dependsOn or is in the graph). – AKS Aug 10 '15 at 19:43