Difference between maven plugins ( assembly-plugins , jar-plugins , shaded-plugins)

23,250

Solution 1

  1. maven-jar-plugin: This plugin provides the capability to build and sign JARs. But it just compiles the java files under src/main/java and src/main/resources/. It doesn't include the dependencies JAR files.
  2. maven-assembly-plugin: This plugin extracts all dependency JARs into raw classes and groups them together. It can also be used to build an executable JAR by specifying the main class. It works in project with less dependencies only; for large project with many dependencies, it will cause Java class names to conflict.
  3. maven-shade-plugin: It packages all dependencies into one uber-JAR. It can also be used to build an executable JAR by specifying the main class. This plugin is particularly useful as it merges content of specific files instead of overwriting them by relocating classes. This is needed when there are resource files that have the same name across the JARs and the plugin tries to package all the resource files together.

Refer: comparison:maven plugin jar,assembly,shade

Solution 2

Jar plugin

Let's see what the following command can tell.

mvn help:describe -Dplugin=org.apache.maven.plugins:maven-jar-plugin

It has 3 goals, help, jar and test-jar. I believe you are mostly interested in the jar goal, which according to the description does the following:

Build a JAR from the current project.

As a side note, executing mvn help:effective-pom on a project with packaging set to jar, shows that this plugin is automatically configured and gets executed during the package phase.

  <plugin>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.4</version>
    <executions>
      <execution>
        <id>default-jar</id>
        <phase>package</phase>
        <goals>
          <goal>jar</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

Assembly plugin

This one serves a different purpose. It has 8 goals, but 6 of them are deprecated. So apart from the help goal, this leaves us with the single goal.

mvn help:describe -Dplugin=org.apache.maven.plugins:maven-assembly-plugin

Assemble an application bundle or distribution from an assembly descriptor. This goal is suitable either for binding to the lifecycle or calling directly from the command line (provided all required files are available before the build starts, or are produced by another goal specified before this one on the command line).

You may use the assembly plugin when you want to deliver more than your project's artifact (JAR, WAR, etc.), but the configuration goes in another file.

Shade plugin

The description of the main goal is a bit disappointing.

mvn help:describe -Dplugin=org.apache.maven.plugins:maven-shade-plugin

Mojo that performs shading delegating to the Shader component.

You mostly want to use this plugin if you want to produce an uber-jar, which is your artifact in a JAR with all its transitive dependencies in it.

Basicly, if you're building a library, you'll stick with the default JAR plugin. If you're building an application, you could consider using the shade plugin, though to me, it's kind of quick and dirty. If uber-jar is not your taste or the distribution cannot fit inside a single JAR (external configuration, native dependencies, etc.) then you should go for the assembly plugin.

Share:
23,250

Related videos on Youtube

ilovejavaAJ
Author by

ilovejavaAJ

Updated on July 05, 2022

Comments

  • ilovejavaAJ
    ilovejavaAJ almost 2 years

    I am a beginner in maven and now I'm confused with the difference between these maven plugins. Is these all create jar files? now my questions are

    1. what's the difference between the jar created in each plugins.( assembly plugin, jar-plugin, shaded plugin)

    2. The purpose of each plugin. ( assembly, jar plugin, shaded plugin )

    3. I know even without specifying any of these plugins once type mvn package there will be a jar output. What is the difference of the output jar without these plugins and the output jar with these plugins?. TIA

    • Tunaki
      Tunaki almost 8 years
      This is really too broad to answer. Take a look at the documentation of each plugin to see what it does. There are examples also in the docs.
    • Raúl
      Raúl over 7 years
      IMHO, This is a good question. For a newbie, (or many experts, for that matter) this choice is confusing. And yes, have docs been suffice for each/every thing - there was no need for SO at first place.
  • Michel Feinstein
    Michel Feinstein over 5 years
    large project with many dependencies, it will cause Java class name conflict issue. so how to deal with these cases then?
  • spats
    spats over 5 years
    @mFeinstein in that case you have to use maven-shade-plugin , you can find more details in already pasted link for "Relocating Classes" above
  • Alain Désilets
    Alain Désilets almost 2 years
    I still have trouble seeing the difference between assembly and shade as they both seem to allow producting of a uber jar containing all dependencies. Am I correct in assuming that assembly blindly assembles everything in a single jar, which can lead conflicts between different version of the same file, and that shade gives you more control to resolves that sort of conflict? Thx.