Skip to content

Commit

Permalink
Fix GraalVM download URL
Browse files Browse the repository at this point in the history
This is the Gradle's counterpart to micronaut-projects/micronaut-maven-plugin#1219

The fix is slightly different since we already had a way to define the base path, but not the full path.
Now the plugin provides the option to completely override the GraalVM download URL, in case the derivation
from the JDK and arch properties is not good enough.
  • Loading branch information
melix committed Oct 22, 2024
1 parent eb9d2b3 commit 0e507f5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ public abstract class NativeImageDockerfile extends Dockerfile implements Docker
private static final String ARM_ARCH = "aarch64";
private static final String X86_64_ARCH = "x64";

private static final String GRAALVM_DOWNLOAD_BASE_URL = "https://download.oracle.com/graalvm";
private static final String GRAALVM_DISTRIBUTION_PATH = "/%s/%s/graalvm-jdk-%s_linux-%s_bin.tar.gz";
//Latest version of GraalVM for JDK 17 available under the GraalVM Free Terms and Conditions (GFTC) licence
private static final String GRAALVM_FOR_JDK17 = "17.0.12";

/**
* @return The JDK version to use with native image. Defaults to the toolchain version, or the current Java version.
*/
Expand Down Expand Up @@ -132,6 +137,17 @@ public abstract class NativeImageDockerfile extends Dockerfile implements Docker
@Optional
public abstract Property<String> getGraalReleasesUrl();

/**
* Return the full URL of the GraalVM distribution to use. By default, it
* uses the base URL from {@link #getGraalReleasesUrl()} and searches in a
* location known to exist when the plugin was built.
*
* @return the URL of the GraalVM distribution to use
*/
@Input
@Optional
public abstract Property<String> getGraalVMDistributionUrl();

@Input
@Override
public abstract Property<String> getTargetWorkingDirectory();
Expand Down Expand Up @@ -188,6 +204,17 @@ public NativeImageDockerfile() {
getTargetWorkingDirectory().convention(DEFAULT_WORKING_DIR);
getExposedPorts().convention(Collections.singletonList(8080));
getGraalImage().convention(getJdkVersion().map(NativeImageDockerfile::toGraalVMBaseImageName));
getGraalReleasesUrl().convention(GRAALVM_DOWNLOAD_BASE_URL);
var distributionPath = getJdkVersion().zip(getGraalArch(), (jdk, arch) -> {
if ("17".equals(jdk)) {
getLogger().warn("You are using the latest release of GraalVM available under the GraalVM Free Terms and Conditions (GFTC) licence (" + GRAALVM_FOR_JDK17 + "). Consider upgrading to Java 21.");
return GRAALVM_DISTRIBUTION_PATH.formatted(jdk, "archive", GRAALVM_FOR_JDK17, arch);
}
return GRAALVM_DISTRIBUTION_PATH.formatted(jdk, "latest", jdk, arch);
});
getGraalVMDistributionUrl().convention(
getGraalReleasesUrl().zip(distributionPath, (base, path) -> base + path)
);
getNativeImageOptions().convention(project
.getTasks()
.named(NativeImagePlugin.NATIVE_COMPILE_TASK_NAME, BuildNativeImageTask.class)
Expand Down Expand Up @@ -436,8 +463,8 @@ private void setupInstructions(List<Instruction> additionalInstructions) {
String graalArch = getGraalArch().get();
// https://download.oracle.com/graalvm/17/latest/graalvm-jdk-17_linux-aarch64_bin.tar.gz
String fileName = "graalvm-jdk-" + jdkVersion + "_linux-" + graalArch + "_bin.tar.gz";
String releasesUrl = getGraalReleasesUrl().getOrElse("https://download.oracle.com/graalvm");
runCommand("curl -4 -L " + releasesUrl + "/" + jdkVersion + "/latest/" + fileName + " -o /tmp/" + fileName);
String graalvmDistributionUrl = getGraalVMDistributionUrl().get();
runCommand("curl -4 -L " + graalvmDistributionUrl + " -o /tmp/" + fileName);
runCommand("tar -zxf /tmp/" + fileName + " -C /tmp && ls -d /tmp/graalvm-jdk-"+ jdkVersion + "* | grep -v \"tar.gz\" | xargs -I_ mv _ /usr/lib/graalvm");
runCommand("rm -rf /tmp/*");
if (toMajorVersion(jdkVersion) < 21) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class DockerNativeFunctionalTest extends AbstractEagerConfiguringFunctionalTest
@Lazy
String defaultDockerFrom = "FROM $defaultBaseImage"

def "test build docker native image for runtime #runtime"() {
def "test build docker native image for runtime #runtime (JDK #jdk)"() {
given:
settingsFile << "rootProject.name = 'hello-world'"
println settingsFile.text
Expand Down Expand Up @@ -45,6 +45,7 @@ class DockerNativeFunctionalTest extends AbstractEagerConfiguringFunctionalTest
dockerfileNative {
args('-Xmx64m')
instruction \"\"\"HEALTHCHECK CMD curl -s localhost:8090/health | grep '"status":"UP"'\"\"\"
jdkVersion = "$jdk"
}
graalvmNative.binaries.all {
Expand Down Expand Up @@ -101,10 +102,11 @@ micronaut:
task.outcome == TaskOutcome.SUCCESS

where:
runtime | nativeImage
"netty" | "FROM ghcr.io/graalvm/native-image-community:17-ol${DefaultVersions.ORACLELINUX}"
"lambda_provided" | 'FROM amazonlinux:2023 AS graalvm'
"jetty" | "FROM ghcr.io/graalvm/native-image-community:17-ol${DefaultVersions.ORACLELINUX}"
runtime | jdk | nativeImage
"netty" | 17 | "FROM ghcr.io/graalvm/native-image-community:17-ol${DefaultVersions.ORACLELINUX}"
"lambda_provided" | 17 | 'FROM amazonlinux:2023 AS graalvm'
"lambda_provided" | 21 | 'FROM amazonlinux:2023 AS graalvm'
"jetty" | 17 | "FROM ghcr.io/graalvm/native-image-community:17-ol${DefaultVersions.ORACLELINUX}"
}

void 'build mostly static native images when using distroless docker image for runtime=#runtime'() {
Expand Down Expand Up @@ -649,7 +651,7 @@ micronaut:
build "dockerfileNative"
def dockerFile = normalizeLineEndings(file("build/docker/native-main/DockerfileNative").text)
dockerFile = dockerFile.replaceAll("[0-9]\\.[0-9]+\\.[0-9]+", "4.0.0")
.trim()
.trim()

then:
dockerFile == """
Expand Down

0 comments on commit 0e507f5

Please sign in to comment.