6

I have a project which should run a specific Gradle task (sonarqube task) only if a condition is met after build task is executed. This task can fail as it communicates with a remote server which is sometimes not available. If the server is unavailable, I'd like to silently handle the error an print some message to console instead of failing the whole build.

Currently I am able to do so with the following configuration:

build.doLast {
    if ('desired.value' == System.properties['some.prop']) {
        try {
            tasks.sonarqube.execute()
        } catch(e) {
            ...
        }
    }
}

However using this method I get deprecation messages. After reading up a bit I supposedly should not be using execute, so instead I came up with this:

if ('desired.value' == System.properties['some.property']) {
    build.finalizedBy sonarqube
}

However in this case, if sonarqube task fails my whole build will fail as well. How could I handle sonarqube task failures in such case?

I am using Gradle 4.5.1.

Edit

The builds are ran using the following command:

./gradlew build

Modifying it like the following will cause Gradle to ignore not only sonarqube failures, but also build failures which is not what I want:

./gradlew build --continue
Edd
  • 1,982
  • 2
  • 21
  • 29
  • Possible duplicate of [Ignore Gradle Build Failure and continue build script?](https://stackoverflow.com/questions/28998372/ignore-gradle-build-failure-and-continue-build-script) – Lukas Körfer Feb 12 '18 at 21:38
  • 1
    @lu.koerfer I'm running my builds using the following command `./gradlew build`. Adding `--continue` will ignore `build` task failures and not only `sonarqube` failures. – Edd Feb 13 '18 at 06:04
  • 1
    This discussion recommends to not run sonarqube task with all other tasks: https://discuss.gradle.org/t/how-do-i-ignore-failed-server-sonarqube-uploads-in-gradle/12640. That gradle forum is also a good place for you to ask about your issue. – tkruse Feb 13 '18 at 07:56

1 Answers1

0

As you've discovered, once a Gradle task fails it's not possible to elegantly recover from that. Therefore you need to avoid that happening.

I can think of three ways to do that:

  1. Find a way of executing the code you want to execute directly without it being wrapped by a Gradle task (eg via a command line or Java process). Then execute that code inside of a try-catch block in the normal fashion.
  2. Activate an option in the sonarqube task which means that task does not fail when there is a problem but reports back to a parent task in another way (eg by setting a property on the Task object), which can in turn deal with the problem. (Having looked quickly it does not appear any such option exists, but you could request it or write a pull request.)
  3. Run another task before the sonarqube task to check if the remote server is available first (and/or other potential failure conditions); then you can deal with the server not being available gracefully, including not running the sonarqube task.

The last option could work as follows by executing a new sonarqubeWrapper task:

tasks.register("sonarqubeWrapper") {
   doFirst {
      if (isServerAvailable()) {
         // Deal with the server not being available
         throw new StopExecutionException()
      }
   }
   doLast('sonarqube')
}

def isServerAvailable() {
   // Determine if server available
}
Simon Jacobs
  • 1,147
  • 7
  • 7