Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle plugin task ordering

What I have?

  1. Java source file with Main class (MainApp)
  2. gradle build script

    apply plugin: 'application' mainClassName = "MainApp" sourceSets.main.java.srcDirs = ['.']

So when I do gradle run, it executes main method and everything works just perfectly.

C:\tmp\gradle-fun>gradle run
:compileJava
:processResources UP-TO-DATE
:classes
:run
Hello MainApp !!
BUILD SUCCESSFUL

What I want to do?

Now I was wondering about clean task (common build tasks) to clean the build directory before run task executes.

There is reason behind that, I want to make sure that every time gradle should compile the java files and all .class file should be refreshed (its some requirement)

What I have tried?

Added a wrapper task which executes clean task and run task in order.

apply plugin: 'application'
mainClassName = "MainApp"
sourceSets.main.java.srcDirs = ['.'] 
task exec(dependsOn: ['clean', 'run'])

So when I run gradle exec, it does it job properly. However I feel that its patch work when you have extra tasks just for ordering execution.

C:\tmp\gradle-fun>gradle run
:clean
:compileJava
:processResources UP-TO-DATE
:classes
:run
Hello MainApp !!
:exec
BUILD SUCCESSFUL

What I would like to know?

Is there any way to avoid writing wrapper task and do some Gradle magic to achieve the requirement?

like image 660
Kunal Avatar asked Dec 25 '22 00:12

Kunal


1 Answers

Just have the run task depend on clean. This will ensure your project is cleaned before every run. If you want to be more specific in regards to your use case, you can simply clean the compileJava task.

run.dependsOn 'cleanCompileJava'

Edit: To avoid deleting your classes before the run add:

classes.mustRunAfter 'cleanCompileJava'
like image 78
Mark Vieira Avatar answered Dec 31 '22 12:12

Mark Vieira