Expecting JavaExec to fail when an exception is thrown by command line application

I am using the JavaExec task type to execute a command line application.

When the application throws a Runtime Exception, I am expecting the task to fail, but it passes as successful. Is this the expected behavior?

The output was

.\gradlew validate
Configuration on demand is an incubating feature.
Calculating task graph as configuration cache cannot be reused because file ‘build.gradle’ has changed.

Task :validate
There was 1 error validating the JSON Schema
Validation Error : The required property ‘commonCropName2’ is not found in the list of properties of ‘Crop’, expecting one of ‘additionalInfo, cropCode, cropDbId, cropPUI, commonCropName, taxonIds, subCropDbIds, cropGroup, externalReferences’
org.brapi.schematools.cli.BrAPICommandException: There was 1 error validating the JSON Schema
at org.brapi.schematools.cli.ValidateSubCommand.printErrors(ValidateSubCommand.java:47)
at org.brapi.schematools.core.response.Response.onFailDoWithResponse(Response.java:708)
at org.brapi.schematools.cli.ValidateSubCommand.run(ValidateSubCommand.java:31)
at picocli.CommandLine.executeUserObject(CommandLine.java:2026)
at picocli.CommandLine.access$1500(CommandLine.java:148)
at picocli.CommandLine$RunLast.executeUserObjectOfLastSubcommandWithSameParent(CommandLine.java:2461)
at picocli.CommandLine$RunLast.handle(CommandLine.java:2453)
at picocli.CommandLine$RunLast.handle(CommandLine.java:2415)
at picocli.CommandLine$AbstractParseResultHandler.execute(CommandLine.java:2273)
at picocli.CommandLine$RunLast.execute(CommandLine.java:2417)
at picocli.CommandLine.execute(CommandLine.java:2170)
at org.brapi.schematools.cli.BrAPICommand.main(BrAPICommand.java:26)

BUILD SUCCESSFUL in 3s
2 actionable tasks: 2 executed

Here is the build.gradle

apply plugin : “java”
ext {
javaMainClass = ‘org.brapi.schematools.cli.BrAPICommand’
brapiSchemaToolsVersion = ‘0.6.0-SNAPSHOT’
}

repositories {
mavenCentral()
maven {url ‘Index of /repositories/snapshots’}
}

dependencies {
implementation “org.brapi:brapi-schema-tools-cli:$brapiSchemaToolsVersion”
}

tasks.register(‘validate’, JavaExec) {
group = ‘BrAPI’
description = ‘Validate the Json Schema’
classpath = sourceSets.main.runtimeClasspath
mainClass = javaMainClass
workingDir = ‘…/’
args = [‘validate’, ‘Specification/BrAPI-Schema’, ‘-x’]
}

Here are the details of the gradle version


Gradle 8.9

Build time: 2024-07-11 14:37:41 UTC
Revision: d536ef36a19186ccc596d8817123e5445f30fef8

Kotlin: 1.9.23
Groovy: 3.0.21
Ant: Apache Ant™ version 1.10.13 compiled on January 4 2023
Launcher JVM: 21.0.2 (Oracle Corporation 21.0.2+13-58)
OS: Windows 11 10.0 amd64

Ok, after posting I found the problem. With the Picocli library the application does not return a non-zero exit status when an exception occurs. I have overridden that now and the task fails when it should.

1 Like