-1

When you build a fat jar using the maven-shade-plugin, there might be conflicts between files at the same path but originating from different dependencies. Similarly, it becomes hard to distinguish the origin of various dependency-specific files located at the root of the individual dependencies (LICENSE.txt, native libraries, ...) when they are placed side-by-side at common locations in the shaded jar.

maven-shade-plugin itself and the similar jarjar-maven-plugin appear to provide functionality for automatically relocating classes, but not other files from the dependencies, such as those seen in the following example:

Example of current messy structure

/
  - NOTICE
  - magic.gz
  - LICENSE.txt
  - LICENSE
  - asm-license.txt
  / META-INF
    - LICENSE.TXT
    - about.html
  / com.some.library
    <class files>
  / com.some.other.library
    <class files>
  / com.some.third.library
    <class files>

This is an example from a project I just started. None of the files above were added by me, but all come from various popular third party libraries I'm using. I would like to clean up this mess by having Maven reorganize files in a common package structure like in the following example:

Example of desired clean structure

/
  / com.some.library
    <class files>
    - NOTICE
    - LICENSE
    / META-INF
      - about.html
  / com.some.other.library
    <class files>
    - magic.gz
    - LICENSE.txt
  / com.some.third.library
    <class files>
    - asm-license.txt
    / META-INF
      - LICENSE.TXT

I have many dependencies, so I don't consider writing dependency-specific filters/relocators/... with hard-coded filenames a good solution to this problem.

How do you organize all files from dependencies in your shaded jar automatically? Ideally by relocating all files from a dependency inside a subfolder named after the dependency.

Zero3
  • 594
  • 2
  • 11
  • 18

1 Answers1

0

This doesn't sound like a Maven best practice...but that being said one possible way to do this would be to create your own Maven plugin based on the maven-shade-plugin. The code base is quite straightforward. You could implement org.apache.maven.plugins.shade.resource.ResourceTransformer and call it in the org.apache.maven.plugins.shade.DefaultShader class when it enumerates each Jar & Jar entry (you could organize the resource by jar name, perhaps). If you want a basic implementation, I'll post one.

aoetalks
  • 1,741
  • 1
  • 13
  • 26
  • What is the Maven best practice then? Please feel free to provide an implementation if you feel like it could solve the problem. – Zero3 Mar 26 '15 at 20:56
  • Sorry for the delay - busy weekend. This would break jars that expect resources in the root directory. That being said, I forked the maven-shade-plugin and edited it to support root resource relocation. It's a very basic implementation, but I did unit test a simple case. Clone the repo here https://github.com/aoetalks/shadeplus-maven-plugin, and run mvn clean install on the shadeplus-maven-plugin project. There is an example project in shadeplus-maven-test-project, in which you can compare my plugin vs the original shade plugin. Let me know if it works for you. – aoetalks Mar 31 '15 at 18:51
  • Thanks! I will definitely check it out! Will report back afterwards. – Zero3 Apr 04 '15 at 11:04
  • I looked at your code. It appears like it only relocates files located in the root of the shaded .jar. What i was asking was "How do you organize *all* files from dependencies in your shaded jar automatically? Ideally by relocating all files from a dependency inside a subfolder named after the dependency." – Zero3 Apr 13 '15 at 17:26
  • My bad. I misread your request because of the description of root files behavior (I did find the relocation of only root resources rather odd :)) I'll make the chance when I get the chance and push it. – aoetalks Apr 13 '15 at 17:30
  • No problem. I added an example to my original post clarifying my issue. – Zero3 Apr 13 '15 at 17:49