Reputation: 607
What I have?
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?
Upvotes: 5
Views: 3960
Reputation: 13486
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'
Upvotes: 5
Reputation: 5072
You could create your own task, with clean
and run
as dependencies:
task cleanRun(dependsOn: [clean, run])
Or, you could follow Mark Vieira's answer and change the wiring:
run.dependsOn 'clean'
classes.mustRunAfter 'clean'
The second line makes sure that it doesn't clean the compiled classes.
Hope this helps =)
Upvotes: 5