Running Jetty App and simple Java app at same time


I have a web app based on Jetty plugin and I want to write a profiler app that is a simple Java app that sends request to this web app. This means web app and profiler need to run at the same time. I’d like everything to be part of the same project and both launched by Gradle. But I don’t see how I can make Gradle run two tasks: if I start the web app (jettyRun), I don’t get a prompt back until it stops. If I start another instance of Gradle, it complains the build file is locked.

Is there a way? Thanks!

 Best, Oliver

Add the following contents into your build.gradle(following uses integTest as task name).

task integTest(type: Test) {
    // start jetty server
    doFirst {
        jettyRun.daemon = true
    // stop jetty server
    doLast {

Hey @curoli

What you want to use is a task dependency on the task that starts the Jetty application and a ‘finalizer’ task that stops the Jetty application.

So if jettyStart starts the Jetty application, runProfiler runs the profile application and jettyStop stops the Jetty application, you would do something like:

runProfiler.dependsOn 'jettyStart'
runProfiler.finalizedBy 'jettyStop'

This will ensure that Jetty is running before the profiler starts and Jetty stops when the build finishes. For the built-in Jetty plug-in, you can turn on ‘daemon’ mode, which will make the Jetty task non-blocking.

We generally encourage people to look at the Gretty plugin instead of using the built-in Jetty plugin because it’s much better and supports newer versions of Jetty. You can accomplish the same thing with it. Here’s some documentation for it:

1 Like

Hey @Shinya_Mochida

Thanks for your answer. I wanted to point out a couple of best practices. See my answer above for a more idiomatic way of doing the same thing.

  1. You shouldn’t call a task’s main action method directly. Gradle is responsible for making sure the task executes when it’s necessary. Calling the method directly might cause problems.
  2. If you have an action that should always be performed after another action (even when that action may fail), you should use a finalizer task.

I’m glad you love Gradle :smile:

Awesome, thanks for the advice, Shinya and Sterling!