graalvm 提供的native 模式,可以加速应用的启动,不同可以让应用不再依赖jvm 运行时环境,但是
也有一些限制 参考 https://github.com/oracle/graal/blob/master/substratevm/LIMITATIONS.md
项目准备
- 项目结构
├── Dockerfile
├── pom.xml
└── src
├── main
│ ├── java
│ │ └── com
│ │ └── dalong
│ │ └── Application.java
│ └── resources
└── test
└── java
- maven pom.xml
代码是一个简单的调用js 的代码,使用了graalvm 的polyglot 特性
同时包含了native-image 的maven plugin 方便生成native 模式的可运行软件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.dalong</groupId>
<artifactId>js-exec</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<encoding>UTF-8</encoding>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.graalvm.js</groupId>
<artifactId>js</artifactId>
<version>19.2.1</version>
</dependency>
</dependencies>
<build>
<finalName>native-image-js</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.dalong.Application</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>com.oracle.substratevm</groupId>
<artifactId>native-image-maven-plugin</artifactId>
<version>19.2.0.1</version>
<configuration>
<buildArgs>--language:js</buildArgs>
<imageName>${project.artifactId}</imageName>
</configuration>
<executions>
<execution>
<goals>
<goal>native-image</goal>
</goals>
<phase>package</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
- 代码
Application.java
package com.dalong;
import org.graalvm.polyglot.*;
/**
* @author dalong
*/
public class Application {
/**
* main for test graalvm exec js
* @param args
*/
public static void main(String[] args) {
System.out.println("Hello Java!");
try (Context context = Context.create()) {
context.eval("js", "print('Hello JavaScript!');");
}
}
}
- dockerfile
注意此构建是通过本机模式生成的(linux),我们也可以使用docker multi stage 的模式构建
FROM debian:9-slim
LABEL maintainer="dalongrong"
LABEL email = "1141591465@qq.com"
COPY target/js-exec /js-exec
CMD [ "/js-exec" ]
构建&&测试
- 本地构建
注意需要安装graalvm 可以通过sdkman 安装
mvn clean package
效果
[INFO] ImageClasspath Entry: org.graalvm.js:js:jar:19.2.1:compile (file:///Users/dalong/.m2/repository/org/graalvm/js/js/19.2.1/js-19.2.1.jar)
[INFO] ImageClasspath Entry: org.graalvm.regex:regex:jar:19.2.1:compile (file:///Users/dalong/.m2/repository/org/graalvm/regex/regex/19.2.1/regex-19.2.1.jar)
[INFO] ImageClasspath Entry: org.graalvm.truffle:truffle-api:jar:19.2.1:compile (file:///Users/dalong/.m2/repository/org/graalvm/truffle/truffle-api/19.2.1/truffle-api-19.2.1.jar)
[INFO] ImageClasspath Entry: org.graalvm.sdk:graal-sdk:jar:19.2.1:compile (file:///Users/dalong/.m2/repository/org/graalvm/sdk/graal-sdk/19.2.1/graal-sdk-19.2.1.jar)
[INFO] ImageClasspath Entry: org.ow2.asm:asm:jar:6.2.1:compile (file:///Users/dalong/.m2/repository/org/ow2/asm/asm/6.2.1/asm-6.2.1.jar)
[INFO] ImageClasspath Entry: org.ow2.asm:asm-tree:jar:6.2.1:compile (file:///Users/dalong/.m2/repository/org/ow2/asm/asm-tree/6.2.1/asm-tree-6.2.1.jar)
[INFO] ImageClasspath Entry: org.ow2.asm:asm-analysis:jar:6.2.1:compile (file:///Users/dalong/.m2/repository/org/ow2/asm/asm-analysis/6.2.1/asm-analysis-6.2.1.jar)
[INFO] ImageClasspath Entry: org.ow2.asm:asm-commons:jar:6.2.1:compile (file:///Users/dalong/.m2/repository/org/ow2/asm/asm-commons/6.2.1/asm-commons-6.2.1.jar)
[INFO] ImageClasspath Entry: org.ow2.asm:asm-util:jar:6.2.1:compile (file:///Users/dalong/.m2/repository/org/ow2/asm/asm-util/6.2.1/asm-util-6.2.1.jar)
[INFO] ImageClasspath Entry: com.ibm.icu:icu4j:jar:62.1:compile (file:///Users/dalong/.m2/repository/com/ibm/icu/icu4j/62.1/icu4j-62.1.jar)
[INFO] ImageClasspath Entry: com.dalong:js-exec:jar:1.0-SNAPSHOT (file:///Users/dalong/quarkus-cloud-native/quarkus-quickstarts/jsexec/target/native-image-js.jar)
[WARNING] Version mismatch between native-image-maven-plugin (19.2.0.1) and native-image executable (19.2.1)
[INFO] Executing: /Users/dalong/.sdkman/candidates/java/19.2.1-grl/jre/bin/native-image -cp /Users/dalong/.m2/repository/org/graalvm/js/js/19.2.1/js-19.2.1.jar:/Users/dalong/.m2/repository/org/graalvm/regex/regex/19.2.1/regex-19.2.1.jar:/Users/dalong/.m2/repository/org/graalvm/truffle/truffle-api/19.2.1/truffle-api-19.2.1.jar:/Users/dalong/.m2/repository/org/graalvm/sdk/graal-sdk/19.2.1/graal-sdk-19.2.1.jar:/Users/dalong/.m2/repository/org/ow2/asm/asm/6.2.1/asm-6.2.1.jar:/Users/dalong/.m2/repository/org/ow2/asm/asm-tree/6.2.1/asm-tree-6.2.1.jar:/Users/dalong/.m2/repository/org/ow2/asm/asm-analysis/6.2.1/asm-analysis-6.2.1.jar:/Users/dalong/.m2/repository/org/ow2/asm/asm-commons/6.2.1/asm-commons-6.2.1.jar:/Users/dalong/.m2/repository/org/ow2/asm/asm-util/6.2.1/asm-