I'm trying to figure out how to launch a JavaExec task that spawns a Jetty server without blocking subsequent tasks. Also, I will need to terminate this server after the build completes. Any idea how I can do this?
I know the thread is from 2011, but I still stumbled across the problem. So here's a solution working with Gradle 2.14:
import java.util.concurrent.Callable
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
class RunAsyncTask extends DefaultTask {
String taskToExecute = '<YourTask>'
@TaskAction
def startAsync() {
ExecutorService es = Executors.newSingleThreadExecutor()
es.submit({taskToExecute.execute()} as Callable)
}
}
task runRegistry(type: RunAsyncTask, dependsOn: build){
taskToExecute = '<NameOfYourTaskHere>'
}
I updated solution from @chrishuen because you cannot call execute on task anymore. Here is my working build.gradle
import java.time.LocalDateTime
import java.util.concurrent.Callable
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
group 'sk.bsmk'
version '1.0-SNAPSHOT'
apply plugin: 'java'
task wrapper(type: Wrapper) {
gradleVersion = '3.4'
}
class RunAsyncTask extends DefaultTask {
@TaskAction
def startAsync() {
ExecutorService es = Executors.newSingleThreadExecutor()
es.submit({
project.javaexec {
classpath = project.sourceSets.main.runtimeClasspath
main = "Main"
}
} as Callable)
}
}
task helloAsync(type: RunAsyncTask, dependsOn: compileJava) {
doLast {
println LocalDateTime.now().toString() + 'sleeping'
sleep(2 * 1000)
}
}
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