【问题标题】:Spring Boot and GraalVM native-imageSpring Boot 和 GraalVM 原生镜像
【发布时间】:2020-09-09 04:18:41
【问题描述】:

使用最新版本的 Spring Boot 2.3.0、spring-graalvm-native 0.7.0.BUILD-SNAPSHOT、GraalVM 20.1.0.r11 和相应的博文

我也开始玩弄我的一个应用程序。

幸运的是,我能够顺利编译我的应用程序。我的compile.sh 脚本如下所示

#!/usr/bin/env bash

echo "[-->] Detect artifactId from pom.xml"
ARTIFACT=$(mvn -q \
-Dexec.executable=echo \
-Dexec.args='${project.artifactId}' \
--non-recursive \
exec:exec);
echo "artifactId is '$ARTIFACT'"

echo "[-->] Detect artifact version from pom.xml"
VERSION=$(mvn -q \
  -Dexec.executable=echo \
  -Dexec.args='${project.version}' \
  --non-recursive \
  exec:exec);
echo "artifact version is '$VERSION'"

echo "[-->] Detect Spring Boot Main class ('start-class') from pom.xml"
MAINCLASS=$(mvn -q \
-Dexec.executable=echo \
-Dexec.args='${start-class}' \
--non-recursive \
exec:exec);
echo "Spring Boot Main class ('start-class') is '$MAINCLASS'"

GREEN='\033[0;32m'
RED='\033[0;31m'
NC='\033[0m'

echo "[-->] Cleaning target directory & creating new one"
rm -rf target
mkdir -p target/native-image

echo "Packaging $ARTIFACT with Maven"
mvn -ntp package > target/native-image/output.txt

echo "[-->] Expanding the Spring Boot fat jar"
JAR="$ARTIFACT-$VERSION.jar"
rm -f $ARTIFACT
echo "Unpacking $JAR"
cd target/native-image
jar -xvf ../$JAR >/dev/null 2>&1
cp -R META-INF BOOT-INF/classes

LIBPATH=`find BOOT-INF/lib | tr '\n' ':'`
CP=BOOT-INF/classes:$LIBPATH

GRAALVM_VERSION=`native-image --version`
echo "Compiling $ARTIFACT with $GRAALVM_VERSION"
{ time native-image \
  --verbose \
  --no-server \
  --no-fallback \
  --enable-all-security-services \
  -H:Name=$ARTIFACT \
  -Dspring.native.remove-unused-autoconfig=true \
  -Dspring.native.remove-yaml-support=true \
  -Dspring.native.remove-xml-support=true \
  -Dspring.native.remove-spel-support=true \
  -Dspring.native.remove-jmx-support=true \
  -cp $CP $MAINCLASS >> output.txt ; } 2>> output.txt

if [[ -f $ARTIFACT ]]
then
  printf "${GREEN}SUCCESS${NC}\n"
  mv ./$ARTIFACT ..
  exit 0
else
  cat output.txt
  printf "${RED}FAILURE${NC}: an error occurred when compiling the native-image.\n"
  exit 1
fi

但现在我的麻烦开始了:我的应用在启动期间依赖一些 CSV 来加载数据。 数据是这样加载的

InputStream is = CSVUtil.class.getResourceAsStream("/myData.csv");

文件存在于/src/main/resources/myData.csv

如前所述,编译工作没有问题,但一旦我启动应用程序,它就找不到 CSV。

Caused by: java.lang.NullPointerException: null
    at java.io.Reader.<init>(Reader.java:167) ~[na:na]
    at java.io.InputStreamReader.<init>(InputStreamReader.java:113) ~[na:na]
    at ch.aaap.raw.CSVUtil.getData(CSVUtil.java:33) ~[na:na]
...

它似乎不是编译的一部分。有什么提示可以让native-image 命令了解我需要这些 CSV 的事实吗?

【问题讨论】:

  • 是的,GraalVM 原生镜像编译需要了解资源文件,因为它们也必须被烘焙到生成的原生可执行文件中。但是您似乎已经使用-H:IncludeResources 找到了正确的配置标志:)

标签: java spring-boot graalvm graalvm-native-image


【解决方案1】:

看起来添加以下参数会有所帮助 -H:IncludeResources='.*/*.csv$'

【讨论】:

    猜你喜欢
    • 2021-05-10
    • 2020-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-09
    • 1970-01-01
    • 2021-01-07
    • 1970-01-01
    相关资源
    最近更新 更多