private void runBuild(CancellationToken cancelToken, BuildLauncher buildLauncher) {
// It is not possible to implement org.gradle.tooling.CancellationToken
// Attempting to do so will cause Gradle to throw a class cast exception
// somewhere.
final DefaultCancellationTokenSource cancelSource = new DefaultCancellationTokenSource();
buildLauncher.withCancellationToken(cancelSource.token());
ListenerRef cancelListenerRef = cancelToken.addCancellationListener(new Runnable() {
@Override
public void run() {
scheduleCancel(cancelSource);