Skip to content

Commit

Permalink
v1.4.0 (#19)
Browse files Browse the repository at this point in the history
* replaced enableDocker with dockerImage - can now specify graalvm image url

---------

Co-authored-by: Mike Friesen - FormKiQ <mike@formkiq.com>
  • Loading branch information
mfriesen and formkiqMike authored Jan 29, 2023
1 parent bd89c4d commit beac66a
Show file tree
Hide file tree
Showing 8 changed files with 190 additions and 171 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Supports for building Java applications as GraalVM native images.
Using the [plugins DSL](https://docs.gradle.org/current/userguide/plugins.html#sec:plugins_block):
```groovy
plugins {
id 'com.formkiq.gradle.graalvm-native-plugin' version '1.2.0'
id 'com.formkiq.gradle.graalvm-native-plugin' version '1.4.0'
}
```

Expand All @@ -25,7 +25,7 @@ buildscript {
}
}
dependencies {
classpath "com.formkiq.gradle:graalvm-native-plugin:1.2.0"
classpath "com.formkiq.gradle:graalvm-native-plugin:1.4.0"
}
}
Expand All @@ -36,7 +36,7 @@ apply plugin: "com.formkiq.gradle.graalvm-native-plugin"
Using the [plugins DSL](https://docs.gradle.org/current/userguide/plugins.html#sec:plugins_block):
```kotlin
plugins {
id("com.formkiq.gradle.graalvm-native-plugin") version "1.2.0"
id("com.formkiq.gradle.graalvm-native-plugin") version "1.4.0"
}
```

Expand All @@ -49,7 +49,7 @@ buildscript {
}
}
dependencies {
classpath("com.formkiq.gradle:graalvm-native-plugin:1.2.0")
classpath("com.formkiq.gradle:graalvm-native-plugin:1.4.0")
}
}

Expand Down Expand Up @@ -81,6 +81,7 @@ More configuration options can be found [here](https://github.com/formkiq/graalv
|----------|------|-------------|
| `imageVersion` | `String` | The GraalVM Community Edition version to download. Default to `20.2.0`. |
| `javaVersion` | `String` | The JDK version to be downloaded with GraalVM Community Edition. Default to `11`. |
| `dockerImage` | `String` | Graalvm Docker Image build using. |
| `imageFile` | `String` | A local Image File to instead of downloading a file based on imageVersion/javaVersion/architecture. |
| `mainClassName` (Required) | `String` | The fully qualified name of the Java class that contains a `main` method for the entry point of the Native Image executable. |
| `enableTraceClassInitialization` | `boolean` | Provides useful information to debug class initialization issues. |
Expand All @@ -91,7 +92,6 @@ More configuration options can be found [here](https://github.com/formkiq/graalv
| `enableHttp` | `boolean` | Enables HTTP support in the generated image. |
| `enableHttps` | `boolean` | Enables HTTPS support in the generated image. |
| `enableVerbose` | `boolean` | Makes image building output more verbose. |
| `enableDocker` | `String` | Enable using Graalvm Docker Image. |
| `enableAllowIncompleteClasspath` | `boolean` | Allow image building with an incomplete class path. |
| `enableNoFallback` | `boolean` | Build stand-alone image or report failure. |
| `enableAutoFallback` | `boolean` | Build stand-alone image if possible. |
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ plugins {
}

group 'com.formkiq.gradle'
version '1.3.2'
version '1.4.0'

java {
toolchain {
Expand Down
4 changes: 2 additions & 2 deletions samples/aws-lambda/HelloWorldFunction/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ dependencies {
}

nativeImage {
version = "20.2.0"
mainClassName = "com.formkiq.lambda.runtime.graalvm.LambdaRuntime"
dockerImage = "ghcr.io/graalvm/graalvm-ce:ol9-java17-22.3.1"
outputFileName = "server"
enableHttp = true
enableHttps = true
enableDocker = true
enableStatic = true
reflectionConfig = "$buildDir/classes/java/main/META-INF/graal/reflect.json"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ public void canRunTaskAsDocker() throws IOException {
writeString(new File(projectDir, "build.gradle"),
"plugins {" + " id('com.formkiq.gradle.graalvm-native-plugin')\n" + "id('java-library')\n"
+ " }\n" + "nativeImage {\n" + " addClasspath = '"
+ classpath.toFile().getCanonicalPath() + "'\n" + " enableDocker = true\n"
+ classpath.toFile().getCanonicalPath() + "'\n"
+ " dockerImage = 'ghcr.io/graalvm/graalvm-ce:ol9-java17-22.3.1'\n"
+ " mainClassName = 'com.formkiq.gradle.Test'\n" + "}");

// when
Expand Down
44 changes: 22 additions & 22 deletions src/main/java/com/formkiq/gradle/GraalvmNativeExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public class GraalvmNativeExtension {

/** Additional Classpaths comma separated. */
private Property<String> addClasspath;
/** Enable using Graalvm Docker Image. */
private Property<String> dockerImage;
/** Enable Add All Charsets. */
private Property<Boolean> enableAddAllCharsets;
/** Allow image building with an incomplete class path. */
Expand All @@ -38,8 +40,6 @@ public class GraalvmNativeExtension {
private Property<Boolean> enableAutoFallback;
/** Check if native-toolchain is known to work with native-image. */
private Property<Boolean> enableCheckToolchain;
/** Enable using Graalvm Docker Image. */
private Property<Boolean> enableDocker;
/** Force building of fallback image. */
private Property<Boolean> enableForceFallback;
/** Enable http support in the generated image. */
Expand Down Expand Up @@ -131,20 +131,29 @@ public GraalvmNativeExtension(final ObjectFactory objects) {
this.addClasspath = objects.property(String.class);
this.features = objects.property(String.class);
this.outputFileName = objects.property(String.class);
this.enableDocker = objects.property(Boolean.class);
this.dockerImage = objects.property(String.class);
this.enableAllowIncompleteClasspath = objects.property(Boolean.class);
this.enableNoFallback = objects.property(Boolean.class);
}

/**
* Returns additional classpaths.
*
*
* @return {@link String}
*/
public String getAddClasspath() {
return this.addClasspath.getOrNull();
}

/**
* Is Enable Docker Image Usage.
*
* @return {@link String}
*/
public String getDockerImage() {
return this.dockerImage.getOrNull();
}

/**
* Returns Features.
*
Expand Down Expand Up @@ -316,15 +325,6 @@ public Boolean isEnableCheckToolchain() {
return this.enableCheckToolchain.getOrElse(Boolean.FALSE);
}

/**
* Is Enable Docker Image Usage.
*
* @return {@link Boolean}
*/
public Boolean isEnableDocker() {
return this.enableDocker.getOrElse(Boolean.FALSE);
}

/**
* Is Fallback.
*
Expand Down Expand Up @@ -442,6 +442,15 @@ public void setAddClasspath(final String cp) {
this.addClasspath.set(cp);
}

/**
* Set Enable Docker Image usage.
*
* @param imageName {@link String}
*/
public void setDockerImage(final String imageName) {
this.dockerImage.set(imageName);
}

/**
* Set Enable AddAllCharsets.
*
Expand Down Expand Up @@ -487,15 +496,6 @@ public void setEnableCheckToolchain(final Boolean enabled) {
this.enableCheckToolchain.set(enabled);
}

/**
* Set Enable Docker Image usage.
*
* @param enabled {@link Boolean}
*/
public void setEnableDocker(final Boolean enabled) {
this.enableDocker.set(enabled);
}

/**
* Set Force Fallback.
*
Expand Down
11 changes: 4 additions & 7 deletions src/main/java/com/formkiq/gradle/GraalvmNativeTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,21 +70,18 @@ public void createImage() {
Path toFile = this.extension.getImageFile() != null ? Path.of(this.extension.getImageFile())
: Path.of(buildDir.getAbsolutePath(), getFilename());

if (!this.extension.isEnableDocker().booleanValue()
&& this.extension.getImageFile() == null) {
if (this.extension.getDockerImage() == null && this.extension.getImageFile() == null) {
new Downloader().download(getDownloadUrl(), toFile);
}

NativeImageExecutor executor = new NativeImageExecutor(this.extension);

boolean decompressed = this.extension.isEnableDocker().booleanValue()
boolean decompressed = this.extension.getDockerImage() != null
|| new ArchiveUtils().decompress(toFile.toFile(), buildDir);

if (this.extension.isEnableDocker().booleanValue()) {
if (this.extension.getDockerImage() != null) {
DockerUtils docker = new DockerUtils();
if (!docker.isDockerInstalled(getProject())) {
throw new ResourceException("Cannot find Docker command in path");
}
docker.isDockerInstalled(getProject());
}

if (decompressed) {
Expand Down
64 changes: 40 additions & 24 deletions src/main/java/com/formkiq/gradle/internal/DockerUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand All @@ -42,17 +41,27 @@ public class DockerUtils {
* Exec Docker.
*
* @param project {@link Project}
* @param workingDir {@link String}
* @param args {@link List}
* @return boolean
* @throws IOException IOException
*/
public boolean exec(final Project project, final List<String> args) throws IOException {
public boolean exec(final Project project, final String workingDir, final List<String> args)
throws IOException {

List<String> a = new ArrayList<>();
a.add("exec");

if (workingDir != null) {
a.add("--workdir");
a.add(workingDir);
}

a.add(this.containerId);
a.addAll(args);

project.getLogger().lifecycle("running: docker " + String.join(" ", a));

ExecResult result = project.exec(new Action<ExecSpec>() {
@Override
public void execute(ExecSpec arg0) {
Expand All @@ -61,31 +70,38 @@ public void execute(ExecSpec arg0) {
}
});

project.getLogger().debug(result.toString());
return result.getExitValue() == 0;
}
project.getLogger().lifecycle("result: " + result.toString());

private String getImageName(final String imageVersion, final String javaVersion) {
return MessageFormat.format("ghcr.io/graalvm/graalvm-ce:{0}-{1}", javaVersion, imageVersion);
return result.getExitValue() == 0;
}

/**
* Is Docker Installed in the system.
*
* @param project {@link Project}
* @return boolean
*
* @throws IOException IOException
*/
public boolean isDockerInstalled(final Project project) throws IOException {
ExecResult result = project.exec(new Action<ExecSpec>() {
@Override
public void execute(ExecSpec arg0) {
arg0.setCommandLine("docker");
arg0.args("--version");
public void isDockerInstalled(final Project project) throws IOException {

try {
ExecResult result = project.exec(new Action<ExecSpec>() {
@Override
public void execute(ExecSpec arg0) {
arg0.setCommandLine("docker");
arg0.args("--version");
}
});

if (result.getExitValue() != 0) {
throw new IOException(
"Cannot find a running 'Docker', exit code: " + result.getExitValue());
}
});
project.getLogger().debug(result.toString());
return result.getExitValue() == 0;

} catch (Exception e) {
e.printStackTrace();
throw new IOException("Cannot find a running 'Docker'");
}
}

/**
Expand All @@ -97,13 +113,12 @@ public void execute(ExecSpec arg0) {
* @return boolean
* @throws IOException IOException
*/
private boolean pullImage(final Project project, final String imageVersion,
final String javaVersion) throws IOException {
private boolean pullImage(final Project project, final String dockerImage) throws IOException {
ExecResult result = project.exec(new Action<ExecSpec>() {
@Override
public void execute(ExecSpec arg0) {
arg0.setCommandLine("docker");
arg0.args(Arrays.asList("pull", getImageName(imageVersion, javaVersion)));
arg0.args(Arrays.asList("pull", dockerImage));
}
});
project.getLogger().debug(result.toString());
Expand All @@ -122,9 +137,8 @@ public void execute(ExecSpec arg0) {
public boolean startImage(final Project project, final GraalvmNativeExtension extension,
final List<File> classPaths) throws IOException {

String imageVersion = extension.getImageVersion();
String javaVersion = extension.getJavaVersion();
pullImage(project, imageVersion, javaVersion);
String dockerImage = extension.getDockerImage();
pullImage(project, dockerImage);

ByteArrayOutputStream so = new ByteArrayOutputStream();

Expand Down Expand Up @@ -154,9 +168,11 @@ public void execute(ExecSpec arg0) {
throw new RuntimeException(e);
}

args.addAll(Arrays.asList(getImageName(imageVersion, javaVersion), "sleep", "infinity"));
args.addAll(Arrays.asList(dockerImage, "sleep", "infinity"));

arg0.args(args);

project.getLogger().lifecycle("docker " + String.join(" ", args));
}

/**
Expand Down
Loading

0 comments on commit beac66a

Please sign in to comment.