53

I'm using the "shade" Maven2 plugin to build a monolithic JAR with all Java dependencies bundled together. The relevant section in pom.xml is pretty straightforward:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>1.4</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <finalName>${project.artifactId}-${project.version}-SHADED</finalName>
                <transformers>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>com.mypackage.MyClass</mainClass>
                    </transformer>
                </transformers>
            </configuration>
        </execution>
    </executions>
</plugin>

However, the build results are odd. It seems that TWO files are actually created by this Maven plugin:

myartifact-1.0.0-SHADED.jar  (zero bytes)
original-myartifact-1.0.0-SHADED.jar  (10 MB)

The JAR file with prefix "original" is properly built, and functions just fine. I suppose I could just rename it to strip off that prefix, and go on my merry way.

However, I very curious as to what may be going on here with the "shade" plugin. It looks like the "original" file is temporary working space type thing, intended to be renamed at the end of the process, and that final renaming simply doesn't complete. There's no obvious explanation for that, though (i.e. filesystem permissions, etc). Has anyone ever seen this before?

2
  • 5
    You might consider switching your check mark to Stewart's answer.
    – Gray
    Feb 27, 2013 at 23:24
  • 1
    A work around is to set <outputDirectory>/tmp</outputDirectory>. The original-*.jar file is still written, but at least it isn't in the development path. Sep 22, 2016 at 5:09

3 Answers 3

66

I know this question is old, but thought it was worth adding the following information.

I think the output originally desired by Steve is that given on this page of the maven-shade-plugin documentation.

<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>jar-with-dependencies</shadedClassifierName>
1
  • 3
    Maven is about reasonable defaults; but the Shale default here is hardly reasonable: It changes the content of the jar that has the original default name built without Shale. Because of that, some IDEs that depend on the default-named jar to have only the original default content now freak out as they see extra/Shaled content in that jar. In that sense, the assembly-plugin has the more reasonable default - keeping both original name and content untouched for the default jar, and give a different name to the fat jar.
    – q3769
    Dec 2, 2018 at 23:53
31

Maven's build steps will create the jar target/artifact-version.jar.

Then the shade plugin runs. It normally renames that jar to target/original-artifact-version.jar, and gives the shaded JAR the name target/artifact-version.jar.

However, you're configuring the Shade plugin to use a different name. Unless there's a good reason for that, I'd remove <finalName> from your configuration, and live with what the Shade plugin wants to give you.

6
  • 19
    That was it. Sigh... I'm enjoying my experiments with Maven, and I like the power it gives me. However, I could do without much of the "don't fight Maven" and "do what Maven wants" culture in that community. I can understand throwing an error or warning message if you ask Maven to perform a bad practice... but when Maven simply fails in a buggy manner instead, those phrases sound like hollow excuses. If the plugin "doesn't want" me to declare a custom final name in the <finalName> element... then WHY OFFER THE <finalName> ELEMENT AS PART OF THE SCHEMA AT ALL? That's simply a bug. Nov 5, 2010 at 12:46
  • 2
    Oh, not intending to gripe at you, Anon... just griping at Maven and its regular community in general! :) I appreciate your help. Nov 5, 2010 at 12:48
  • @Steve - no offense taken. Believe me, I gripe at Maven constantly. Right now I'm griping because I can't easily override the deployment URL in my settings.xml :-) And occasionally I've had to look at the source code to figure out what went wrong.
    – Anon
    Nov 5, 2010 at 13:19
  • 1
    @swordfish the shaded jar (unless otherwise configured) simply includes all your project's dependencies - thus if your project includes a 'main', you needn't specify a long classpath. With the shade plugin, you can also specify a mainClass so you can make an executable jar: java -jar myproj-1.0.jar.
    – Chadwick
    Dec 19, 2011 at 7:24
  • 2
    Hi. u said that first of all Maven build original jar without dependencies, then rename it with prefix "original-" and then with shade plugin build final jar with all dependencies. Why is it building original jar without dependencies, then renaming it? is it possible to set to ignore build of original jar without dependencies and build just one jar with all dependencies? without all this dancing around with building useless jars and then renaming them?
    – Bryksin
    Sep 17, 2015 at 23:54
12

Following @Stewart on providing a slightly better answer (no offense to either :D):

The reason you get the original-* mess can be twofold:

  1. Specifying <finalName> means you want a name different than what Maven gives you by default (ie: same as artifact's name: artifactId-version.jar or artifactId-version-shaded.jar). If you specify a final name that is the same as one of the two, it will try to back the old one up as original-*.jar and then overwrite it with the new shaded one. In your case, you're telling it to make the final JAR *-shaded.jar, which is already the case when it comes out of Maven (before they by default make it into artifactId-version.jar), so it first backs up the old *-shaded.jar as original-*-shaded.jar and then bugs out when writing the bytes to the new *-shaded.jar given the old one disappeared (they seem to rename it).

  2. (Which was my case) Using <shadedClassifierName>, which changes only the suffix Maven uses to generate the *-shaded.jar, in combination with <finalName> can also yield the same results. If you want you can just use <shadedClassifierName> and specify a different suffix and be done with it without having to specify <finalName> with the whole thing. In my case I had both set up to name the output the same thing: ie: artifactId-version-all.jar but using 'all' as Classifier brought me back to the scenario described in #1.

1
  • 1
    You might want to use <shadedArtifactAttached> as well but that just means you want the shaded artifact in the Maven repo after installing/deploying it alongside the original one. Doesn't really change anything when it comes to naming it. Jan 23, 2013 at 16:40

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.