Gradle shouldRunAfter not available for a task

Viewed 1434

I created this repo to reproduce exactly what I'm seeing. I have a project that I'm building with Gradle. I would like to configure my Gradle build so that running:

./gradlew build

Has the exact same effect as running:

./gradlew clean build scalafmt shadowJar fizzbuzz

Meaning Gradle calls tasks in the following order:

  1. clean
  2. build (compile & run unit tests)
  3. scalafmt (run a tool that formats my code to a styleguide)
  4. shadowJar (creates a self-contained executable "fat" jar)
  5. fizzbuzz (prints out "Fizzbuzz!" to the console)

According to the Gradle docs on ordering tasks, it seems that I can use shouldRunAfter to specify ordering on all tasks...

If you clone my repo above and then run ./gradlew build you'll get the following output:

./gradlew build
Fizzbuzz!

FAILURE: Build failed with an exception.

* Where:
Build file '/Users/myUser/thelab/idea-scala-hate-each-other/build.gradle' line: 65

* What went wrong:
A problem occurred evaluating root project 'idea-scala-hate-each-other'.
> Could not find method shouldRunAfter() for arguments [task ':build'] on cz.alenkacz.gradle.scalafmt.PluginExtension_Decorated@6b24ddd7.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 10.983 secs

So even though I specify fizzbuzz to run last...its running first! And there are (obviously) errors with the rest of my config. And I'm not sure how to "hook" clean so that even when I run ./gradlew build it first runs clean.

I'd be OK with a solution that requires me to write my own "wrapper task" to achieve the order I want, and then invoke it, say, via ./gradlew buildMyApp, etc. Just not sure how to accomplish what I want.


Update

I made some changes to build.gradle and am now seeing this:

./gradlew fullBuild
:compileJava UP-TO-DATE
:compileScala
:processResources UP-TO-DATE
:classes
:jar
:startScripts
:distTar
:distZip
:assemble
:compileTestJava UP-TO-DATE
:compileTestScala UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test UP-TO-DATE
:check UP-TO-DATE
:build
:clean
:fizzbuzz
Fizzbuzz!
:scalafmt
:shadowJar
:fullBuild

So when I run ./gradlew fullBuild task execution seems to be:

  1. build (which calls compileJava through check)
  2. clean
  3. fizzbuzz
  4. scalafmt
  5. shadowJar

So the ordering is still wrong even given my most recent changes...

2 Answers

One of the possible causes of that error could be because Gradle couldn't compile the task of the method shouldRunAfter or mustRunAfter.

Also it could happen when there is another entity of other type (not a task) with the same name of the task of the method shouldRunAfter or mustRunAfter. In this case you could use the syntax tasks['task1_name'].shouldRunAfter tasks['task2_name'] or tasks['task1_name'].mustRunAfter tasks['task2_name'] to make sure Gradle is referencing a task type entity.

Related