Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 284928 - Exporting maven project as runnable Jar puts resources in wrong directory
Summary: Exporting maven project as runnable Jar puts resources in wrong directory
Status: RESOLVED NOT_ECLIPSE
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 3.5   Edit
Hardware: PC Windows XP
: P3 normal with 2 votes (vote)
Target Milestone: ---   Edit
Assignee: JDT-UI-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-07-28 16:10 EDT by Jerome Waibel CLA
Modified: 2022-08-04 09:22 EDT (History)
9 users (show)

See Also:


Attachments
Test project for reproducing this issue (5.60 KB, application/x-zip-compressed)
2009-08-03 14:25 EDT, Jerome Waibel CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Jerome Waibel CLA 2009-07-28 16:10:37 EDT
Build ID: 20090621-0832

Steps To Reproduce:
1. Create a new maven project
2. Put some Foo class in src/main/java
3. Put some bar.xml in src/main/resources
4. bar.xml can be reached from Foo by "classpath:bar.xml" (e.g. in a ClasspathResource from Spring)
5. Export this project as a runnable jar
6. Lo and behold: bar.xml is *not* in the root directory of the jar but in a subdirectory called "resources". Your class doesn't work any more, bar.xml can't be found.
Comment 1 Olivier Thomann CLA 2009-07-29 09:44:54 EDT
Move to JDT/UI.
JDT/Core doesn't do the export.
Comment 2 Markus Keller CLA 2009-07-30 09:33:57 EDT
That sounds like an issue of the maven plug-in you're using.

The Runnable JAR exporter just collects the contents of the output folders ('bin' directories) and puts them into the JAR together with the required libraries. It sounds like src/main/resources is not on the build path of your project.

Please reopen if you can provide an example that fails with a pure Eclipse SDK. 

Otherwise, you can file a bug against the maven plug-in you use to see if there's something they could do. But be aware that the Runnable JAR exporter can only work for relatively simple Java projects. If you use frameworks that have their own classloading mechanisms, this won't work, and you will have to build your project depending on those frameworks' requirements.
Comment 3 Jerome Waibel CLA 2009-07-30 17:11:25 EDT
(In reply to comment #2)
> The Runnable JAR exporter just collects the contents of the output folders
> ('bin' directories) and puts them into the JAR together with the required
> libraries. It sounds like src/main/resources is not on the build path of your
> project.

Yes, it is on the build path:

<classpathentry kind="src" output="target/classes" path="src/main/java"/>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources"/>

Both class files and resources go into target/classes/. That directory has the right structure (properties in the root directory like it should be in the jar, no trace of a sub-directory called resources).

Nonetheless, I opened a bug in the m2eclipse project. If someone wants to follow this issue, here is the link:

https://issues.sonatype.org/browse/MNGECLIPSE-1542
Comment 4 Markus Keller CLA 2009-08-03 08:20:23 EDT
> <classpathentry excluding="**" kind="src" output="target/classes"
> path="src/main/resources"/>

If your project is correctly built, the 'excluding="**"' should prevent any child of src/main/resources from being copied to target/classes, so this should not even work if you run it from Eclipse.

Can you please attach the whole project?
Comment 5 Jerome Waibel CLA 2009-08-03 14:25:39 EDT
Created attachment 143308 [details]
Test project for reproducing this issue
Comment 6 Jerome Waibel CLA 2009-08-03 14:27:10 EDT
(In reply to comment #4)
> > <classpathentry excluding="**" kind="src" output="target/classes"
> > path="src/main/resources"/>
> 
> If your project is correctly built, the 'excluding="**"' should prevent any
> child of src/main/resources from being copied to target/classes, so this should
> not even work if you run it from Eclipse.

I think the resources are copied there by the maven plugin, at least that is what the ordinary maven command line is doing: copying files under src/main/resources into target/classes. Yet it still puzzles me how the folder called "resources" is ending up in the jar - if the export would be relying only on eclipse copying the files I could understand a completly missing resources file, but not ending up in the sub-folder.
Comment 7 Kevin CLA 2009-09-14 11:31:46 EDT
I had the same problem with a Maven project. I came across this bug report and I created a plain Java project with the Maven directory structure.  My exported jar was fine. 

So, back with my Maven project. I took the resources folder (src/main/resources) out of the build path and then put it back in again. Right-click, remove from build path and again for add to build path.  I clicked again on my resources in the desc dialog, rebuilt the Jar file and it now works!

The only difference I can see is that one of my resources is exported as a <javaElement> whereas before it was as a <folder>.

Kevin
Comment 8 T3rm1 CLA 2016-03-29 09:17:18 EDT
This still happens with Eclipse 4.5.2 and m2e 1.6.2
Comment 9 Robert Soti CLA 2016-08-10 04:42:01 EDT
The problem exists in Eclipse 4.6.0, too.
Comment 10 Andreas Wittmann CLA 2017-08-14 02:42:37 EDT
Can confirm this problem still exists in Eclipse Oxygen.

Can also confirm that the workaround provided by Kevin 2 comments above fixed the problem.
Comment 11 Atsushi Nakagawa CLA 2017-10-13 01:03:09 EDT
Maven's "Update Project..." creates Build Path entries with the "src/main/resources" entry having "Excluded: **" for some reason.

If I manually remove the exclusion pattern (or do as Kevin wrote and remove and recreate the entry, thereby removing the exclusion pattern), "Export..." creates jars with the *contents* of src/main/resources copied to the root.

If I put "Excluded: **" back in, the jar is created with a "resources" folder at the root and contents inside that folder.

I can manually remove "Excluded: **" but doing Maven -> "Update Project..." puts it right back.  So although I don't know the purpose, it seems to be intentional.
Comment 12 Stephan Herrmann CLA 2017-10-16 18:43:39 EDT
(In reply to Atsushi Nakagawa from comment #11)
> Maven's "Update Project..." creates Build Path entries with the
> "src/main/resources" entry having "Excluded: **" for some reason.
> 
> If I manually remove the exclusion pattern (or do as Kevin wrote and remove
> and recreate the entry, thereby removing the exclusion pattern), "Export..."
> creates jars with the *contents* of src/main/resources copied to the root.
> 
> If I put "Excluded: **" back in, the jar is created with a "resources"
> folder at the root and contents inside that folder.
> 
> I can manually remove "Excluded: **" but doing Maven -> "Update Project..."
> puts it right back.  So although I don't know the purpose, it seems to be
> intentional.

m2e devs believe this exclusion thing is a cool idea, but I definitely don't share their view on this. The ticket linked from comment 3 has not received a single reaction in 8 years. Since meanwhile the project has fully moved to Eclipse.org, you may try and file a new bug against m2e here in bugzilla.

I don't think JDT should change anything, because the rule is to package everything that is on the classpath, and with that exclusion filter no files under resources/ *are* on the classpath, effectively.

Just one more idea to try: invoke a maven build right before creating the jar, maybe this will cause maven to copy resources to target/classes as needed for the jar packager?
Comment 13 Andreas Lück CLA 2022-08-04 05:39:28 EDT
In case, that somebody has the same problem as me that the resources in the JAR are all in a sub-folder "resources" and not on root level:

In the build path of "some" of my projects, the "src/main/resources" had an exclusion setting of "**" (I don't know why nor where it comes from). I removed that everywhere, which fixed the incorrect packaging of my resources.
Comment 14 Stephan Herrmann CLA 2022-08-04 08:06:03 EDT
(In reply to Andreas Lück from comment #13)
> exclusion setting of "**" (I don't know why nor where it comes from).

See comment 11 and comment 12
Comment 15 Andreas Lück CLA 2022-08-04 09:22:25 EDT
(In reply to Stephan Herrmann from comment #14)
> See comment 11 and comment 12

Thanks, Stephan. I missed that.