I'm using the maven-assembly-plugin to build a Java 8 project, and package it as a JAR file.
I've got several resources located at src/main/resources
(usual maven resources directory).
When the JAR is created, all resources files under src/main/resources
are copied at the root of the JAR.
In the code, I'm trying to open files using FileInputStream
that are located in src/main/resources
, and it works great when running the project without packaging it as a JAR file. However, when I'm running the project from the JAR file, I get many FileNotFoundException
because my resources are not anymore at src/main/resources
, but at ${project.basedir}
instead.
I'd like to have them located at src/main/resources
in the JAR file. Is it possible?
My build configuration is as follow:
<build>
<sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
<plugins>
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>EmployProject.TestAlgo</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>TestAlgo</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
Edit
If it's not possible, what, in my code, should I use to access my resources files?