Added example showing how to build the executable from the main class file

This commit is contained in:
Erik C. Thauvin 2024-04-04 23:49:39 -07:00
parent fdbec5e22a
commit 5fa3c49f45
Signed by: erik
GPG key ID: 776702A6A2DA330E
5 changed files with 30 additions and 14 deletions

2
.idea/misc.xml generated
View file

@ -3,6 +3,8 @@
<component name="EntryPointsManager"> <component name="EntryPointsManager">
<pattern value="com.example.GraalNativeBuild" /> <pattern value="com.example.GraalNativeBuild" />
<pattern value="com.example.GraalNativeBuild" method="nativeExec" /> <pattern value="com.example.GraalNativeBuild" method="nativeExec" />
<pattern value="com.example.GraalNativeBuild" method="nativeClass" />
<pattern value="com.example.GraalNativeBuild" method="nativeJar" />
</component> </component>
<component name="PDMPlugin"> <component name="PDMPlugin">
<option name="skipTestSources" value="false" /> <option name="skipTestSources" value="false" />

View file

@ -8,22 +8,22 @@ Be sure to have the GraalVM `native-image` utility in your path, or set its loca
```console ```console
./bld compile ./bld compile
``` ```
## Create a Java archive ## Create the Java archive
```console ```console
./bld jar ./bld jar
``` ```
## Create the native application ## Create the native application from the Java archive
```console ```console
./bld native-exec ./bld native-jar
``` ```
## Combine the commands ## Create the native application from the main class
```console ```console
./bld compile jar native-exec ./bld compile native-class
``` ```
## Launch the application ## Launch the application

View file

@ -4,7 +4,7 @@ import rife.bld.BuildCommand;
import rife.bld.Project; import rife.bld.Project;
import rife.bld.extension.ExecOperation; import rife.bld.extension.ExecOperation;
import java.nio.file.Paths; import java.io.File;
import java.util.List; import java.util.List;
import java.util.jar.Attributes; import java.util.jar.Attributes;
@ -44,8 +44,22 @@ public class GraalNativeBuild extends Project {
new GraalNativeBuild().start(args); new GraalNativeBuild().start(args);
} }
@BuildCommand(value = "native-exec", summary = "Builds a native executable") @BuildCommand(value = "native-class", summary = "Builds a native executable")
public void nativeExec() throws Exception { public void nativeClass() throws Exception {
new ExecOperation()
.fromProject(this)
.timeout(120)
.workDir(buildMainDirectory().getAbsolutePath())
// The native image options documentation can be found at:
// https://www.graalvm.org/22.0/reference-manual/native-image/Options/
.command("native-image", // use its absolute path if not found
mainClass(),
new File(workDirectory(), "hello").getAbsolutePath())
.execute();
}
@BuildCommand(value = "native-jar", summary = "Builds a native executable from the JAR")
public void nativeJar() throws Exception {
new ExecOperation() new ExecOperation()
.fromProject(this) .fromProject(this)
.timeout(120) .timeout(120)
@ -53,7 +67,7 @@ public class GraalNativeBuild extends Project {
// https://www.graalvm.org/22.0/reference-manual/native-image/Options/ // https://www.graalvm.org/22.0/reference-manual/native-image/Options/
.command("native-image", // use its absolute path if not found .command("native-image", // use its absolute path if not found
"-jar", "-jar",
Paths.get(buildDistDirectory().getAbsolutePath(), jarFileName()).toString(), new File(buildDistDirectory(), jarFileName()).toString(),
"hello") "hello")
.execute(); .execute();
} }

View file

@ -1,11 +1,11 @@
package com.example; package com.example;
public class GraalNativeMain { public class GraalNativeMain {
public String getMessage() {
return "Hello World!";
}
public static void main(String[] args) { public static void main(String[] args) {
System.out.println(new GraalNativeMain().getMessage()); System.out.println(new GraalNativeMain().getMessage());
} }
public String getMessage() {
return "Hello World!";
}
} }

View file

@ -2,7 +2,7 @@ package com.example;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.assertEquals;
public class GraalNativeTest { public class GraalNativeTest {
@Test @Test