Skip to content

Commit

Permalink
Use GraalVM Gradle Plugin for command-line tools
Browse files Browse the repository at this point in the history
* 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:

* micronaut-projects/micronaut-gradle-plugin#706
* gradle/gradle#17559

we're referencing the Micronaut application plugin in the three
subprojects rather than the GraalVM Gradle plugin directly.
  • Loading branch information
msgilligan committed Nov 25, 2023
1 parent dac3bdf commit 6792316
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 54 deletions.
39 changes: 21 additions & 18 deletions cj-btc-cli/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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}"

Expand All @@ -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 {
Expand Down
41 changes: 22 additions & 19 deletions consensusj-jsonrpc-cli/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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}"

Expand All @@ -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')
}
}
38 changes: 21 additions & 17 deletions consensusj-jsonrpc/build.gradle
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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')

Expand Down Expand Up @@ -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')
}
}

0 comments on commit 6792316

Please sign in to comment.