[英]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.
我想要实现的是创建一个jar,它将在运行时加载资源(打包在jar中),而不会在从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. 从Eclipse启动应用程序时,它工作正常。 However, when I export the runnable jar from the Eclipse, it doesn't works.
但是,当我从Eclipse导出可运行的jar时,它不起作用。
Exported jar have /resources/img folder in it's root directory. 导出的jar的根目录中有/ resources / img文件夹。 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? 从可运行的jar文件以及从Eclipse运行应用程序时,如何使其能够工作?
I was also trying to build jar with maven plugins, but with no luck. 我也试图用maven插件构建jar,但是没有运气。
<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. 关键是
src/main/resources
将自动复制到target/class
,它是您的类路径的根。 This will work in Eclipse as well from the packaged jar. 打包的jar也可以在Eclipse中使用。
An runnable jar will be created by maven via maven-assembly-plugin like this: 一个可运行的jar将由maven通过maven-assembly-plugin创建,如下所示:
<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: 添加完之后,您可以通过以下方式创建完整的可运行jar:
mvn clean package
This will produce a jar file in target
folder which looks like: ´whatever-1.0-SNAPSHOT-jar-with-dependencies.jar´. 这将在
target
文件夹中生成一个jar文件,如下所示:“ whatever-1.0-SNAPSHOT-jar-with-dependencies.jar”。
What i don't understand is the usage of maven-dependency-plugin in your build? 我不明白在您的构建中使用了maven-dependency-plugin?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.