Jens
02/25/2025, 12:25 PMcompileJava
task so that gradle caches the weaved classes as task output automatically. Weaving is done using compileJava { doLast { project.javaexec {.... } } }
. Because project.javaexec
is deprecated I migrated it to def execOutput = providers.javaexec
but now I am unable to get the process output/error. It always seems to be empty. Any Idea?Jens
02/25/2025, 12:26 PMcompileJava {
doLast {
def execOutput = providers.javaexec {
...
}
// trigger javaexec
def result = execOutput.getResult().get()
println execOutput.getStandardOutput().getAsText().get()
println execOutput.getStandardError().getAsText().get()
result.assertNormalExitValue()
}
}
Jens
02/25/2025, 12:46 PMVampire
02/25/2025, 2:28 PMJens
02/25/2025, 3:01 PMplugins {
id 'java-library'
}
java {
toolchain {
languageVersion = JavaLanguageVersion.of(23)
}
}
dependencies {
implementation "org.eclipse.persistence:org.eclipse.persistence.jpa:2.7.15"
}
// old version
//compileJava {
// doLast {
// project.javaexec {
// executable = javaToolchains.launcherFor {
// languageVersion = JavaLanguageVersion.of(23)
// }.map { it.executablePath }.get()
// mainClass = "org.eclipse.persistence.tools.weaving.jpa.StaticWeave"
// classpath = configurations.compileClasspath
// }
// }
//}
// migrated version
compileJava {
def execOutput = providers.javaexec {
executable = javaToolchains.launcherFor {
languageVersion = JavaLanguageVersion.of(23)
}.map { it.executablePath }.get()
mainClass = "org.eclipse.persistence.tools.weaving.jpa.StaticWeave"
classpath = configurations.compileClasspath
}
doLast {
try {
println "trigger javaexec"
def result = execOutput.getResult().get()
} catch (Exception e) {
println "trigger javaexec: exception caught: " + e.getMessage()
e.printStackTrace()
try {
def stdOut = execOutput.getStandardOutput().getAsText().get()
println stdOut
} catch (Exception e2) {
// "e2" is same instance as "e" as noted in the JavaDoc of ExecOutput.getResult()
println "trigger javaexec: Cannot access standard out of process: " + e2.getMessage()
}
try {
def stdErr = execOutput.getStandardError().getAsText().get()
println stdErr
} catch (Exception e2) {
// "e2" is same instance as "e" as noted in the JavaDoc of ExecOutput.getResult()
println "trigger javaexec: Cannot access standard error of process: " + e2.getMessage()
}
}
println "trigger javaexec: done"
}
}
Jens
02/25/2025, 3:02 PMJens
02/25/2025, 3:08 PMVampire
02/25/2025, 3:58 PMignoreExitValue
to true
.
Then you can check result
for the exit value and access stdout and stderr without problems or also let it rethrow the exception.Jens
02/25/2025, 4:19 PMVampire
02/25/2025, 4:23 PMVampire
02/25/2025, 4:23 PMVampire
02/25/2025, 4:24 PMJens
02/25/2025, 4:34 PMExecOperations
in an ad-hoc task. Using that one for javaexec
behaves the same as project.javaexec
even without the ignoreExitValue
. Looks like ExecOperations
is meant to be the drop-in replacement although it is a bit clunky to get a reference of it for ad-hoc tasks.Vampire
02/25/2025, 4:51 PMproject.javaexec { ... }
is just a convenience method for using ExecOperations
when you are not in a plugin or similar.Vampire
02/25/2025, 4:51 PMVampire
02/25/2025, 4:51 PMinterface ExecOperationsProvider {
@get:Inject
val execOperations: ExecOperations
}
objects.newInstance<ExecOperationsProvider>().execOperations.javaexec {
// ...
}
Vampire
02/25/2025, 4:53 PMinterface ExecOperationsProvider {
@Inject
ExecOperations getExecOperations()
}
objects.newInstance(ExecOperationsProvider).execOperations.javaexec {
// ...
}
Jens
02/25/2025, 5:26 PMVampire
02/25/2025, 5:49 PM