简体   繁体   中英

Eclipse and Maven - resources in jar

I know this question was asked many times before, but I still can't manage it to work. What I want to achieve is to create jar which will load resource (packed in jar) in runtime, without broking resource loading while executing application from Eclipse.

My project structure is standard:

src/main/java
src/main/resources
src/test/java
src/test/resources

Code for load image resource:

        ClassLoader classLoader = getClass().getClassLoader();
        splashImage = ImageIO.read(new File(classLoader.getResource("img/splash.png").getFile()));

It is working fine when starting the App from the Eclipse. However, when I export the runnable jar from the Eclipse, it doesn't works.

Exported jar have /resources/img folder in it's root directory. But when the app starts, an exception is thrown:

Caused by: javax.imageio.IIOException: Can't read input file!

How it is possible to make it work from runnable jar file and when running the App from the Eclipse?

I was also trying to build jar with maven plugins, but with no luck.

 <build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.8</version>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/lib</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <mainClass>foo.bar.App</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>

You should go the following way for loading your resource:

classLoader.getResourceAsStream("/img/splash.png")

The point is that src/main/resources will automatically being copied to target/class which is the root of your classpath. This will work in Eclipse as well from the packaged jar.

An runnable jar will be created by maven via maven-assembly-plugin like this:

<project>
  [...]
  <build>
    [...]
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.5.3</version>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
        </configuration>
        [...]
</project>

After you have added this you can create the full runnable jar via:

mvn clean package

This will produce a jar file in target folder which looks like: ´whatever-1.0-SNAPSHOT-jar-with-dependencies.jar´.

What i don't understand is the usage of maven-dependency-plugin in your build?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM