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?
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'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With