4

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?

Maxime Flament
  • 721
  • 1
  • 7
  • 24
  • 1
    Hmm yeah, why should I use it? What are the benefits, and how would it solve my problem? – Maxime Flament Sep 11 '17 at 12:21
  • My resources files are included in the JAR. The problem is that they are put at the root of my project, and, when in the code I try to access them using getClass().getResource(fileName), it returns null because it can't find the file. – Maxime Flament Sep 11 '17 at 12:36
  • 1
    First removed `sourceDirectory` and resources configuration cause those are both the defaults...so don't configure it...Apart from that by default resources are at the root of your jar file which means you have to use `getResourceAsStream("/property.properties")` ? – khmarbaise Sep 11 '17 at 12:50

1 Answers1

1

Thanks to @khmarbaise and this post: Accessing a Java Resource as a File

I figured out I was using the wrong API to open resources. Resources in a JAR are not files, thus I changed:

XMLDecoder xmlDec = new XMLDecoder(new InputFileStream(this.getClass().getResourceAsStream(fileName))

to:

InputStream in = EnumTools.class.getResourceAsStream(fileName);
XMLDecoder decoder = new XMLDecoder(in);
Maxime Flament
  • 721
  • 1
  • 7
  • 24