views:

51

answers:

2

I'm trying to build an application that builds a resource file into a jar, but I'd like to have the project runnable within eclipse. I have a basic maven 2 structure for my project, and I'm unsure how to read in the file such that it's found and used when run from the JAR or from within eclipse. Thought?

Structure:

src/main/java
src/main/resources/file.txt

Current reading method:

getClass().getResourceAsStream("/file.txt")

Is there reading method that will pick up src/main/resources/*, as well as the root level of the JAR (where resources are deployed)?

A: 

Anything that is put in src/main/resources using maven2 will be placed in the root level of the jar. So the method you are currently using will satisfy both conditions. From the Maven page: http://maven.apache.org/guides/getting-started/index.html#How_do_I_add_resources_to_my_JAR

Vinny
+1  A: 

I don't understand your problem. Resources from src/main/resources are automatically copied over to target/classes and are thus available on the classpath under Maven and Eclipse relatively to the root level at the same location (unless your Eclipse project is not properly configured).

And when packaged inside a JAR, the content of target/classes is packaged "as is" so nothing is changed.

In other words, accessing your file.txt like this is perfectly fine (and this is actually how things are documented):

// Retrieve resource
InputStream is = getClass().getResourceAsStream( "/file.txt" );

// Do something with the resource

...

If you have a problem somewhere, please clarify.

Update: I did a quick test with the maven-eclipse-plugin and I can't reproduce your problem:

$ mvn archetype:generate -DgroupId=com.stackoverflow -DartifactId=q2467362 -Dversion=1.0-SNAPSHOT
...
$ cd q2467362
$ mkdir -p src/main/resources
$ mvn eclipse:eclipse
...
$ cat .classpath
<classpath>
  <classpathentry kind="src" path="src/test/java" output="target/test-classes" including="**/*.java"/>
  <classpathentry kind="src" path="src/main/java" including="**/*.java"/>
  <classpathentry kind="src" path="src/main/resources" excluding="**/*.java"/>
  <classpathentry kind="output" path="target/classes"/>
  <classpathentry kind="var" path="M2_REPO/junit/junit/3.8.1/junit-3.8.1.jar" sourcepath="M2_REPO/junit/junit/3.8.1/junit-3.8.1-sources.jar"/>
  <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
</classpath>

The directory src/main/resources is added as source folder as expected. Can you show your POM (especially the resources element if you define one)?

Pascal Thivent
What must be done with the eclipse project to have it copy resources to target/classes during the automatic build step?
Stefan Kendall
@Stefan Setup `src/main/resource` as source folder. But this should be done by both the maven-eclipse-plugin or M2Eclipse.
Pascal Thivent
I tried regenerating the project with mvn eclipse:eclipse, but it still didn't take. Hmm. I guess I'll just add it manually this way, then.
Stefan Kendall
I just retried my own project, and now it's working. Must be some stray eclipse issue that not even I can reproduce.
Stefan Kendall