3

In my Maven project, I have tried the maven-shade-plugin to produce an uber jar when I run mvn package. As a result, I get three jars in my target directory:

original-hello-world-0.1.0-SNAPSHOT.jar
hello-world-0.1.0-SNAPSHOT.jar
hello-world-0.1.0-SNAPSHOT-shaded.jar

I understand that first jar is the output of packaging without including dependencies. But the both the second and the third jars include dependencies and seem to be completely identical (they both have the same file size).

Is there supposed to be a difference between the second and third jars? If so, what is it? Otherwise, why are two identical jars being produced?

Here is my pom file:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>mygroup</groupId>
  <artifactId>myproject</artifactId>
  <packaging>jar</packaging>
  <version>0.1.0-SNAPSHOT</version>
  <name>hello-world</name>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.7</java.version>
    <main.class>org.example.HelloWorld</main.class>

    <maven.compiler.target>${java.version}</maven.compiler.target>
    <maven.compiler.source>${java.version}</maven.compiler.source>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.jgrapht</groupId>
      <artifactId>jgrapht-core</artifactId>
      <version>0.9.0</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <!-- To build executable jar -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>2.3</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <transformers>
                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                  <manifestEntries>
                    <Main-Class>${main.class}</Main-Class>
                  </manifestEntries>
                </transformer>
              </transformers>
            </configuration>
          </execution>
        </executions>
      </plugin>

      <!-- To use Maven to run main class -->
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.3.2</version>
        <executions>
          <execution>
            <goals>
              <goal>java</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <mainClass>${main.class}</mainClass>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
anishpatel
  • 1,472
  • 1
  • 16
  • 23
  • Shaded jars rename the libraries/jars to avoid namespace issues, afaik. For example, suppose you are working on a codebase with many packages. Some packages may use the same libraries, but different versions. Using shaded jars helps to avoid mishaps with loading the wrong version of a library. – mauzel Jan 25 '15 at 02:12

1 Answers1

0

I copied your pom into a project of mine and only got two jars created when I ran mvn package.

Are you sure the "hello-world-0.1.0-SNAPSHOT-shaded.jar-shaded" jar is not the output of a previous maven run? Please try a mvn clean package and check what lands in the target folder.

If you would like an additional jar with the "-shaded" tag you can add the following to your pom in the shade plugin configuration:

<shadedClassifiedName>shaded</shadedClassifiedName>
JHowIX
  • 1,683
  • 1
  • 20
  • 38