I've built a Spring Boot native executable using the command mvn clean package -Pnative
but it can't read files in /resources
.
Java code:
try (InputStream inputStream = Reader.class.getClassLoader()
.getResourceAsStream(filename)) {
if (inputStream == null) throw new RTException("Can't find file " + filename + " in /resources.");
fileString = IOUtils.toString(inputStream, UTF_8);
} catch (Exception e) {
throw new RTException("Issue reading or parsing file " + filename + "\n" + e.getMessage());
}
In pom.xml
, as required in this Baeldung article:
<profile>
<id>native</id>
<build>
<plugins>
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
<executions>
<execution>
<id>build-native</id>
<goals>
<goal>compile-no-fork</goal>
</goals>
<phase>package</phase>
<!-- closing tags -->
In META-INF/native-image/[groupId]/[artifactId]/native-image.properties
:
Args = -H:IncludeResources=".*/*.txt$"
When launching the native executable, I get
(...).RTException: Issue reading or parsing file file.txt
Can't find file file.txt in /resources.`
The file file.txt is in /resources and there is no problem reading it using a non-native build.
What am I missing here ?
Found it.
Adding native-image.properties
under META-INF
only works when running the command native-image -jar [app.jar]
(not tested though).
When building the native image with Maven, it doesn't read the content of META-INF
(tested with some invalid arg in native-image.properties
, no error thrown).
To make it work I've added <buildArgs>
this in the Maven plugin config (found in plugin doc):
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
<executions>
<execution>
<id>build-native</id>
(...)
<configuration>
<buildArgs>-H:IncludeResources=.*txt$</buildArgs>
<!-- closing tags -->