Skip to content

Commit cd0a6fc

Browse files
committed
Implement wams-spring-shell.
1 parent 68a3125 commit cd0a6fc

File tree

6 files changed

+121
-0
lines changed

6 files changed

+121
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
name: native-image/wasm-spring-shell
2+
on:
3+
push:
4+
paths:
5+
- 'native-image/wasm-spring-shell/**'
6+
- '.github/workflows/native-image-wasm-spring-shell.yml'
7+
pull_request:
8+
paths:
9+
- 'native-image/wasm-spring-shell/**'
10+
- '.github/workflows/native-image-wasm-spring-shell.yml'
11+
schedule:
12+
- cron: "0 0 1 * *" # run every month
13+
workflow_dispatch:
14+
permissions:
15+
contents: read
16+
jobs:
17+
run:
18+
name: Run 'native-image/wasm-spring-shell'
19+
runs-on: ubuntu-latest
20+
timeout-minutes: 15
21+
steps:
22+
- uses: actions/checkout@v4
23+
- name: Download GraalVM 25.1 EA builds
24+
run: curl -sL -o $RUNNER_TEMP/graalvm-jdk-25e1-linux-amd64.tar.gz https://github.com/graalvm/oracle-graalvm-ea-builds/releases/download/jdk-25e1-25.0.0-ea.01/graalvm-jdk-25e1-25.0.0-ea.01_linux-x64_bin.tar.gz
25+
- uses: actions/setup-java@v4
26+
with:
27+
java-version: '25'
28+
distribution: 'jdkfile'
29+
jdkFile: ${{ runner.temp }}/graalvm-jdk-25e1-linux-amd64.tar.gz
30+
- uses: actions/setup-node@v4
31+
with:
32+
node-version: 22
33+
- name: Set up Binaryen
34+
run: |
35+
curl -sLO https://github.com/WebAssembly/binaryen/releases/download/version_122/binaryen-version_122-x86_64-linux.tar.gz
36+
tar xzf binaryen-version_122-x86_64-linux.tar.gz
37+
echo "$(pwd)/binaryen-version_122/bin" >> "$GITHUB_PATH"
38+
- name: Run 'native-image/wasm-spring-shell'
39+
run: |
40+
cd native-image/wasm-spring-shell
41+
./mvnw --no-transfer-progress -Pnative package
42+
node target/wasm-spring-shell help
43+
node target/wasm-spring-shell hello Jane
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Compile Spring Shell into a Wasm Module
2+
3+
This demo illustrates how to use GraalVM Web Image to compile a Spring Shell application into a Wasm module that can then run on the command-line or in the browser. [Check out the live demo here](https://graalvm.github.io/graalvm-demos/native-image/wasm-spring-shell/).
4+
5+
## Prerequisites
6+
7+
This demo requires:
8+
9+
1. This [Early Access Build](https://github.com/graalvm/oracle-graalvm-ea-builds/releases/tag/jdk-25e1-25.0.0-ea.01) of Oracle GraalVM 25.1 or later.
10+
2. The [Binaryen toolchain](https://github.com/WebAssembly/binaryen) in version 119 or later and on the system path.
11+
For example, using Homebrew: `brew install binaryen`
12+
13+
## Run Spring Shell on Node
14+
15+
1. Build the Wasm module with the `native` profile:
16+
```bash
17+
$ ./mvnw -Pnative package
18+
```
19+
The demo uses the [Native Build Tools](https://graalvm.github.io/native-build-tools/latest/index.html) for building native images with GraalVM and Maven.
20+
This command generates a Wasm file and a corresponding JavaScript binding in the `target` directory.
21+
22+
2. Run the Wasm module on Node:
23+
```bash
24+
node target/wasm-spring-shell help
25+
node target/wasm-spring-shell hello Jane
26+
```
27+
This requires Node.js 22 or later.

native-image/wasm-spring-shell/pom.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,17 @@
6464
<plugin>
6565
<groupId>org.graalvm.buildtools</groupId>
6666
<artifactId>native-maven-plugin</artifactId>
67+
<configuration>
68+
<buildArgs>
69+
<!-- Enable Web Image -->
70+
<buildArg>--tool:svm-wasm</buildArg>
71+
<!-- Annotate Wasm module with debug info -->
72+
<buildArg>-g</buildArg>
73+
<!-- macOS-specific initialization policies -->
74+
<buildArg>--initialize-at-build-time=apple.security.AppleProvider</buildArg>
75+
<buildArg>--initialize-at-build-time=apple.security.AppleProvider$ProviderService</buildArg>
76+
</buildArgs>
77+
</configuration>
6778
</plugin>
6879
<plugin>
6980
<groupId>org.springframework.boot</groupId>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright (c) 2025, Oracle and/or its affiliates.
3+
*
4+
* Licensed under the Universal Permissive License v 1.0 as shown at https://opensource.org/license/UPL.
5+
*/
6+
7+
package com.example.wasm_spring_shell;
8+
9+
import org.springframework.shell.standard.ShellComponent;
10+
import org.springframework.shell.standard.ShellMethod;
11+
import org.springframework.shell.standard.ShellOption;
12+
13+
@ShellComponent
14+
public class MyCommands {
15+
16+
@ShellMethod(key = "hello")
17+
public String hello(@ShellOption(defaultValue = "Spring") String arg) {
18+
return "Hello " + arg + " from WebAssembly built with GraalVM!";
19+
}
20+
21+
}

native-image/wasm-spring-shell/src/main/java/com/example/wasm_spring_shell/WasmSpringShellApplication.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
1+
/*
2+
* Copyright (c) 2025, Oracle and/or its affiliates.
3+
*
4+
* Licensed under the Universal Permissive License v 1.0 as shown at https://opensource.org/license/UPL.
5+
*/
6+
17
package com.example.wasm_spring_shell;
28

39
import org.springframework.boot.SpringApplication;
10+
import org.springframework.boot.ansi.AnsiOutput;
11+
import org.springframework.boot.ansi.AnsiOutput.Enabled;
412
import org.springframework.boot.autoconfigure.SpringBootApplication;
513

614
@SpringBootApplication
@@ -10,4 +18,9 @@ public static void main(String[] args) {
1018
SpringApplication.run(WasmSpringShellApplication.class, args);
1119
}
1220

21+
static {
22+
/* Always enable colorful terminal output. */
23+
AnsiOutput.setEnabled(Enabled.ALWAYS);
24+
}
25+
1326
}

native-image/wasm-spring-shell/src/test/java/com/example/wasm_spring_shell/WasmSpringShellApplicationTests.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
/*
2+
* Copyright (c) 2025, Oracle and/or its affiliates.
3+
*
4+
* Licensed under the Universal Permissive License v 1.0 as shown at https://opensource.org/license/UPL.
5+
*/
6+
17
package com.example.wasm_spring_shell;
28

39
import org.junit.jupiter.api.Test;

0 commit comments

Comments
 (0)