Eclipse和Maven - jar中的资源 [英] Eclipse and Maven - resources in jar
问题描述
我知道这个问题曾多次被问到,但我仍然无法管理它。
我想要实现的是创建jar,它将在运行时加载资源(打包在jar中),而不需要在从Eclipse执行应用程序的同时加载资源。
我的项目结构是标准的:
src / main / java
src / main / resources
src / test / java
src / test / resources
加载映像资源代码:
ClassLoader classLoader = getClass()。getClassLoader();
splashImage = ImageIO.read(new File(classLoader.getResource(img / splash.png)。getFile()));
从Eclipse启动App时,它工作正常。但是,当我从Eclipse导出可运行的jar时,它不起作用。
导出的jar在其根目录中有/ resources / img文件夹。但是当应用程序启动时,会抛出异常:
导致:javax.imageio.IIOException:无法读取输入文件!
如何从runnable jar文件和从Eclipse运行应用程序起作用?
我还试图用maven插件来构建jar,但没有运气。
code>< 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>
<执行>
< 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>
您应该以下列方式加载资源:
classLoader.getResourceAsStream(/ img / splash.png)
<要点是 src / main / resources
将自动复制到 target / class
这是您的类路径的根。这将在Eclipse中从打包的jar中工作。
maven-assembly-plugin , xml prettyprint-override> < 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>
添加完后,您可以通过以下方式创建完整的可运行的jar:
mvn clean package
将在 target
文件夹中生成一个jar文件,如下所示:'whatever-1.0-SNAPSHOT-jar-with-dependencies.jar'。
我不明白的是在你的构建中使用maven-dependency-plugin?
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?
这篇关于Eclipse和Maven - jar中的资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!