15

I am using maven-shade-plugin for a simple maven project, the plugin successfully includes all the dependencies into a final "shaded" jar. The process works well every time and produces exactly what I need.

When run the "first" time (after a clean), the plugin is quiet and produces very little output. However, when re-run (without a clean from the last build), there are lots of warning messages such as this;

[WARNING] We have a duplicate package/a/b/foo.class
[WARNING] We have a duplicate package/c/d/bar.class

This are warning messages only and the final artifact works fine.

My question is simple: how can I safely workaround or suppress these warning messages without having to run a clean first?


note: A possible solution would be to move to the maven-assembly-plugin, but I would prefer not to because the configuration for maven-shade-plugin is very nice and simple.

javamonkey79
  • 17,443
  • 36
  • 114
  • 172
xconspirisist
  • 1,451
  • 2
  • 13
  • 26
  • 1
    I assume the reported duplicates are all dependency classes, right? In that case they most likely are first extracted to some folder so you might want to add a clean step for those folders before or after the package phase (depending on whether you need those classes in between builds). – Thomas Jan 16 '12 at 13:10
  • 1
    I have the same question as above...how to suppress the warnings. In my case I'm including dependencies from numerous Spring package jars over which I have no control - i.e. I cannot "clean" before. – Mark Laff Apr 03 '12 at 18:07
  • 2
    This is exactly my issue! [Oh dear!](http://xkcd.com/979/) – plasma147 Jun 13 '12 at 11:43
  • Which version of `maven-shade-plugin` were you using? It appears there was a similar bug in versions < 1.2 ... http://maven.40175.n5.nabble.com/jira-Created-MSHADE-30-duplicate-entry-error-td263554.html – rogerdpack Mar 13 '19 at 00:22

1 Answers1

17

This is because it is shading the files into an already shaded jar.

The first time you run package after a clean then it will create the jar. The second time you run it then it doesn't bother as the jar already exists.

From the shade plugins perspective it doesn't know that this has already been shaded so it just tries to add the classes again.

We can force maven to create the jar everytime by configuring the jar plugin:

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-jar-plugin</artifactId>
   <version>2.4</version>
   <configuration>
     <forceCreation>true</forceCreation>
   </configuration>
</plugin>

And this works for me. Either that or just do a clean install

plasma147
  • 2,191
  • 21
  • 35
  • 1
    Thanks for this. Is there another way to get the plugin to use the `original` jar? I don't understand the dynamic between `maven-jar-plugin` and `maven-shade-plugin` – Dmitry Minkovsky May 25 '16 at 22:37
  • I want to add that if you also create shaded source JARs with Maven Shade, you also want to add the same `true` option to Maven Source Plugin for the corresponding module. – kriegaex Jul 08 '20 at 04:23