如何从Maven项目中使用Eclipse中的资源创建可运行jar [英] How to create runnable jar with resources in Eclipse from Maven project

查看:146
本文介绍了如何从Maven项目中使用Eclipse中的资源创建可运行jar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Eclipse(版本Kepler SR2)将Java项目打包到可运行的jar中.该项目正在使用Maven(已被强加给我,对此我不太了解).问题在于该项目在Eclipse中运行良好,但是在导出到Jar后,它不再找到任何必需的资源.

I am trying to package a Java project into a runnable jar using Eclipse (version Kepler SR2). The project is using Maven (which has been fored onto me and I don't know much about it). The problem is that the project runs fine within Eclipse, but after export to Jar it doesn't find any of the required resources anymore.

这是文件夹结构:

+src
  +main
     +java
       <packages with source code>
     +resources
       <folders with static resources>
  +test
     +java
       <unit test classes>

我还将资源文件夹添加到了构建路径.

I have also added the resource folder to the build path.

使用

File myFile = new File(getClass().getClassLoader().getResource("resourcePath").getPath());

当从Eclipse执行程序时,此行工作得很好,但是当使用我通过导出> Java>可运行的罐子"创建的可执行罐子时,此行失败并显示NPE.

This line works perfectly fine when executing the program from Eclipse, but it fails with an NPE when using the executable jar that I created with "Export>Java>Runnable Jar".

我已经使用标准Jar导出,并且选中了导出生成的类文件和资源"和添加目录项"选项.但是,这导致了同样的问题.

I have already to use the standard Jar export with the options "Export generated class files and resources" and "Add directory entries" checked. This leads to the same problem, though.

为了正确将资源导出到Jar中,我需要做什么?

What do I need to do in order to correctly export the resources into the Jar?

谢谢!

马丁

推荐答案

使用maven-assembly-plugin: 参考: 使用Maven 2构建可运行的jar http://www.javavids. com/video/how-to-create-runnable-jar-file-with-maven.html

Use maven-assembly-plugin: Reference: Building a runnable jar with Maven 2 http://www.javavids.com/video/how-to-create-runnable-jar-file-with-maven.html

这篇关于如何从Maven项目中使用Eclipse中的资源创建可运行jar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆