Skip to content

Support Build Cancellation in JavaExec Task #1128

@lacasseio

Description

@lacasseio

Edit: Improved description to reference the more generic solution (build cancel event) as oppose to the specific usage of this feature (Ctrl-D) explained by Gary's comment.

Expected Behavior

When JavaExec task waits for the completion of the Java application, it should support cancel event and terminate the Java application only. This exposes a deterministic build cancellation to Java shutdown hook execution delay to the user as well as avoid killing a perfectly healthy daemon. It would also be possible to support continuous mode for any JavaExec task.

Current Behavior

JavaExec doesn't support cancel event. The only way to cancel an application ran through that task is to send a Ctrl-C which will cause the daemon to exit as the JavaExec task doesn't return:

  1. There is a, up to, 10 seconds delay between Ctrl-C and the execution of the shutdown hook in the Java application
  2. The System.{out|err} output of the Java application isn't piped to the console.

Context

Initial discussion in #1109

Steps to Reproduce (for bugs)

build.gradle:

apply plugin: 'java'
apply plugin: 'application'

mainClassName = 'Runner'

src/main/java/Runner.java

import java.io.File;
import java.util.Random;
import java.util.concurrent.CompletableFuture;

public class Runner {
    public static void main(String[] args) throws Exception {
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            System.out.println("Clean shutdown");
        }));
        File lock = new File("LOCK" + new Random().nextInt(20));
        lock.createNewFile();
        lock.deleteOnExit();
        System.out.println("Created lock");
        new CompletableFuture<Void>().get();
    }
}

Metadata

Metadata

Assignees

Labels

a:featureA new functionality

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions