gabac
gabac

Reputation: 2078

Spring Boot and GraalVM native-image

With the latest releases of Spring Boot 2.3.0, spring-graalvm-native 0.7.0.BUILD-SNAPSHOT, GraalVM 20.1.0.r11 and the corresponding blog posts

I also started to play around with one of my apps.

Luckily I was able to compile my app without any big hurdles. My compile.sh script looks as follows

#!/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

But now my troubles start: My app relies on some CSVs during startup to load data. The data is loaded like this

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

The file is present at /src/main/resources/myData.csv

As said the compilation works without an issue but once I start the app it can't find the 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]
...

It seems that it's not part of the compilation. Any hints how I can make the native-image command aware about the fact that I need these CSVs?

Upvotes: 2

Views: 1230

Answers (1)

gabac
gabac

Reputation: 2078

Looks like adding following argument helps -H:IncludeResources='.*/*.csv$'

Upvotes: 0

Related Questions