From 6792316a150ad9cb27de53565c7214e7af651a79 Mon Sep 17 00:00:00 2001 From: Sean Gilligan Date: Sat, 25 Nov 2023 12:42:20 -0800 Subject: [PATCH] Use GraalVM Gradle Plugin for command-line tools * cj-btc-cli * consensusj-jsonrpc (MathTool simple test) * consensusj-jsonrpc-cli Previously these were built by calling `native-image` with `Exec`. Because of these two issues: * https://github.com/micronaut-projects/micronaut-gradle-plugin/issues/706 * https://github.com/gradle/gradle/issues/17559 we're referencing the Micronaut application plugin in the three subprojects rather than the GraalVM Gradle plugin directly. --- cj-btc-cli/build.gradle | 39 ++++++++++++++------------- consensusj-jsonrpc-cli/build.gradle | 41 ++++++++++++++++------------- consensusj-jsonrpc/build.gradle | 38 ++++++++++++++------------ 3 files changed, 64 insertions(+), 54 deletions(-) diff --git a/cj-btc-cli/build.gradle b/cj-btc-cli/build.gradle index 58f6ac0b..24358ace 100644 --- a/cj-btc-cli/build.gradle +++ b/cj-btc-cli/build.gradle @@ -4,6 +4,12 @@ */ plugins { id 'java-library' + // Instead of using 'org.graalvm.buildtools.native' directly we need to include + // the micronaut application plugin to avoid a Gradle bug: + // See: https://github.com/micronaut-projects/micronaut-gradle-plugin/issues/706 + // and: https://github.com/gradle/gradle/issues/17559 + //id 'org.graalvm.buildtools.native' version '0.9.28' + id("io.micronaut.application") version "${micronautAppGradlePluginVersion}" } ext.moduleName = 'org.consensusj.bitcoin.cli' @@ -23,7 +29,7 @@ dependencies { api project(':cj-btc-jsonrpc') // Add SLF4J runtime adapter for JDK logging for GraalVM native-image build of bitcoin CLI tool - nativeToolImplementation "org.slf4j:slf4j-jdk14:${slf4jVersion}" + nativeImageClasspath "org.slf4j:slf4j-jdk14:${slf4jVersion}" testImplementation "org.apache.groovy:groovy:${groovyVersion}" @@ -41,25 +47,22 @@ jar { } } - -// Compile a native image using GraalVM's native-image tool -// Graal must be installed at $JAVA_HOME -tasks.register('nativeCompile', Exec) { - dependsOn jar - workingDir = projectDir - executable = "${System.env.JAVA_HOME}/bin/native-image" - args = ['--verbose', - '--no-fallback', - '-cp', "${-> configurations.nativeToolImplementation.asPath}", // Lazy configuration resolution - '-jar', jar.archiveFile.get(), - '-H:Path=build', - '-H:Name=cj-bitcoin-cli', - '-H:EnableURLProtocols=http,https', - '-H:+ReportUnsupportedElementsAtRuntime' - ] +graalvmNative { + binaries { + main { + mainClass = mainClassName + imageName = 'cj-bitcoin-cli' + } + test { + imageName = 'cj-bitcoin-cli-dbg' + } + } + binaries.all { + sharedLibrary = false + buildArgs.add('--verbose') + } } - // Test Structure sourceSets { integrationTest { diff --git a/consensusj-jsonrpc-cli/build.gradle b/consensusj-jsonrpc-cli/build.gradle index 368d2b1c..afbfdbe2 100644 --- a/consensusj-jsonrpc-cli/build.gradle +++ b/consensusj-jsonrpc-cli/build.gradle @@ -4,6 +4,12 @@ */ plugins { id 'java-library' + // Instead of using 'org.graalvm.buildtools.native' directly we need to include + // the micronaut application plugin to avoid a Gradle bug: + // See: https://github.com/micronaut-projects/micronaut-gradle-plugin/issues/706 + // and: https://github.com/gradle/gradle/issues/17559 + //id 'org.graalvm.buildtools.native' version '0.9.28' + id("io.micronaut.application") version "${micronautAppGradlePluginVersion}" } tasks.withType(JavaCompile).configureEach { @@ -20,7 +26,7 @@ dependencies { api 'commons-cli:commons-cli:1.6.0' // Add SLF4J runtime adapter for JDK logging for GraalVM native-image build of jsonrpc tool - nativeToolImplementation "org.slf4j:slf4j-jdk14:${slf4jVersion}" + nativeImageClasspath "org.slf4j:slf4j-jdk14:${slf4jVersion}" testImplementation "org.apache.groovy:groovy:${groovyVersion}" @@ -39,22 +45,19 @@ jar { } } -// Compile a native image using GraalVM's native-image tool -// Graal must be installed at $JAVA_HOME -tasks.register('nativeCompile', Exec) { - dependsOn jar - workingDir = projectDir - executable = "${System.env.JAVA_HOME}/bin/native-image" - args = ['--verbose', - '--no-fallback', - '-cp', "${-> configurations.nativeToolImplementation.asPath}", // Lazy configuration resolution - '-jar', jar.archiveFile.get(), - '-H:Path=build', - '-H:Name=jsonrpc', - '--initialize-at-build-time=com.fasterxml.jackson.annotation.JsonProperty$Access', - '-H:IncludeResources=logging.properties', - '-H:EnableURLProtocols=http,https', - '-H:+ReportUnsupportedElementsAtRuntime', - '-H:+ReportExceptionStackTraces' - ] +graalvmNative { + binaries { + main { + mainClass = mainClassName + imageName = 'jsonrpc' + } + test { + imageName = 'jsonrpc-dbg' + } + } + binaries.all { + sharedLibrary = false + buildArgs.add('--initialize-at-build-time=com.fasterxml.jackson.annotation.JsonProperty$Access') + buildArgs.add('--verbose') + } } diff --git a/consensusj-jsonrpc/build.gradle b/consensusj-jsonrpc/build.gradle index 50445d73..c8b948e2 100644 --- a/consensusj-jsonrpc/build.gradle +++ b/consensusj-jsonrpc/build.gradle @@ -1,5 +1,11 @@ plugins { id 'java-library' + // Instead of using 'org.graalvm.buildtools.native' directly we need to include + // the micronaut application plugin to avoid a Gradle bug: + // See: https://github.com/micronaut-projects/micronaut-gradle-plugin/issues/706 + // and: https://github.com/gradle/gradle/issues/17559 + //id 'org.graalvm.buildtools.native' version '0.9.28' + id("io.micronaut.application") version "${micronautAppGradlePluginVersion}" } configurations { @@ -11,7 +17,7 @@ dependencies { api "com.fasterxml.jackson.core:jackson-databind:${jacksonVersion}" // Add SLF4J runtime adapter for JDK logging for GraalVM native-image build of MathService/Tool - nativeSampleImplementation "org.slf4j:slf4j-jdk14:${slf4jVersion}" + nativeImageClasspath "org.slf4j:slf4j-jdk14:${slf4jVersion}" testImplementation project(':cj-btc-jsonrpc') @@ -44,20 +50,18 @@ test { } } -// Compile a native image of the MathService tool sample using Graal's native-image tool -// Graal must be installed at $JAVA_HOME -tasks.register('nativeCompile', Exec) { - dependsOn jar - workingDir = projectDir - executable = "${System.env.JAVA_HOME}/bin/native-image" - args = ['--verbose', - '--no-fallback', - '-cp', "${-> configurations.nativeSampleImplementation.asPath}", // Lazy configuration resolution - '-jar', jar.archiveFile.get(), - '-H:Path=build', - '-H:Name=MathTool', - '-H:+AllowIncompleteClasspath', - '-H:+ReportUnsupportedElementsAtRuntime', - '-H:+ReportExceptionStackTraces' - ] +graalvmNative { + binaries { + main { + mainClass = mainClassName + imageName = 'MathTool' + } + test { + imageName = 'MathTool-dbg' + } + } + binaries.all { + sharedLibrary = false + buildArgs.add('--verbose') + } }