Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

maven-shade-plugin doesn't replace the original jar

It is weird that my maven-shade-plugin doesn't replace the original jar with the shaded jar. Does anyone know what could be the reason?

Here's my plugin in pom.xml

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>${plugin.shade.version}</version>
    <configuration>
      <artifactSet>
        <excludes>
          <!-- Leave slf4j unshaded so downstream users can configure logging. -->
          <exclude>org.slf4j:slf4j-api</exclude>
          <exclude>org.slf4j:slf4j-log4j12</exclude>
          <!-- Leave commons-logging unshaded so downstream users can configure logging. -->
          <exclude>commons-logging:commons-logging</exclude>
          <!-- Leave commons-exec unshaded so downstream users can use ProcessLauncher. -->
          <exclude>org.apache.commons:commons-exec</exclude>
          <!-- Leave log4j unshaded so downstream users can configure logging. -->
          <exclude>log4j:log4j</exclude>
        </excludes>
      </artifactSet>
      <filters>
        <filter>
          <artifact>*:*</artifact>
          <excludes>
            <exclude>META-INF/*.SF</exclude>
            <exclude>META-INF/*.DSA</exclude>
            <exclude>META-INF/*.RSA</exclude>
          </excludes>
        </filter>
      </filters>
      <transformers>
        <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
        <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
          <resource>reference.conf</resource>
        </transformer>
        <transformer implementation="org.apache.maven.plugins.shade.resource.ApacheLicenseResourceTransformer"/>
        <transformer implementation="org.apache.maven.plugins.shade.resource.DontIncludeResourceTransformer">
          <resource>NOTICE.txt</resource>
        </transformer>
        <transformer implementation="org.apache.maven.plugins.shade.resource.IncludeResourceTransformer">
          <resource>META-INF/LICENSE.txt</resource>
          <file>${basedir}/../../LICENSE.txt</file>
        </transformer>
        <transformer implementation="org.apache.maven.plugins.shade.resource.IncludeResourceTransformer">
          <resource>META-INF/NOTICE.txt</resource>
          <file>${basedir}/../../NOTICE.txt</file>
        </transformer>
      </transformers>
      <relocations>
        <relocation>
          <pattern>org</pattern>
          <shadedPattern>${shaded.dependency.prefix}.org</shadedPattern>
          <excludes>
            <exclude>org/apache/zeppelin/*</exclude>
            <exclude>org/apache/zeppelin/**/*</exclude>
            <exclude>org/apache/thrift/*</exclude>
            <exclude>org/apache/thrift/**/*</exclude>
            <exclude>org/slf4j/*</exclude>
            <exclude>org/slf4j/**/*</exclude>
            <exclude>org/apache/commons/logging/*</exclude>
            <exclude>org/apache/commons/logging/**/*</exclude>
            <exclude>org/apache/commons/exec/*</exclude>
            <exclude>org/apache/commons/exec/**/*</exclude>
            <exclude>org/apache/log4j/*</exclude>
            <exclude>org/apache/log4j/**/*</exclude>
            <exclude>org/sonatype/*</exclude>
            <exclude>org/sonatype/**/*</exclude>
            <exclude>**/pom.xml</exclude>

            <!-- Not the org/ packages that are a part of the jdk -->
            <exclude>org/ietf/jgss/*</exclude>
            <exclude>org/omg/**/*</exclude>
            <exclude>org/w3c/dom/*</exclude>
            <exclude>org/w3c/dom/**/*</exclude>
            <exclude>org/xml/sax/*</exclude>
            <exclude>org/xml/sax/**/*</exclude>
          </excludes>
        </relocation>
        <relocation>
          <pattern>com.google</pattern>
          <shadedPattern>${shaded.dependency.prefix}.com.google</shadedPattern>
        </relocation>
        <relocation>
          <pattern>io</pattern>
          <shadedPattern>${shaded.dependency.prefix}.io</shadedPattern>
        </relocation>
        <relocation>
          <pattern>com.esotericsoftware</pattern>
          <shadedPattern>${shaded.dependency.prefix}.com.esotericsoftware</shadedPattern>
        </relocation>
      </relocations>
    </configuration>
    <executions>
      <execution>
        <phase>package</phase>
        <goals>
          <goal>shade</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
like image 350
zjffdu Avatar asked Aug 29 '19 02:08

zjffdu


People also ask

How to get single Jar file in Maven using shade plugin?

We can use the shade plugin in maven to obtain the single jar file also called as uber/fat jar file that is self-sufficient and can be run independently as it contains all the dependencies and project code inside it. This can be done by simply adding the plugin tag with shade plugin related information as shown in the above example.

What is shade Apache Maven?

Apache Maven Shade Plugin. This plugin provides the capability to package the artifact in an uber-jar, including its dependencies and to shade - i.e. rename - the packages of some of the dependencies. The Shade Plugin has a single goal: shade:shade is bound to the package phase and is used to create a shaded jar.

What is the difference between shaded jar and shade plugin?

But shade plugin is the most preferred one as it provides relocating facility that helps in avoiding the class name conflicts having the same name in the classpath. Shade plugin has only one goal that is it can be only used in the package phase and the main purpose lies in creating the shaded jar that can be independently executed.

What are the plugins available for Maven?

There are many plugins available for maven projects. The main aim of the plugins is to provide the capability to form the package of the artifacts in the maven project and its dependencies into an uber or fat jar and rename certain packages. Uber is a german word that stands for above which means it is one level up, unlike the other normal jars.


Video Answer


1 Answers

Shaded plugin by default save original file as -original.jar, if you want to replace original file with the new generated (shaded), put this line in your configuration plugin section:

<configuration>
    ...

    <outputFile>${output.directory}\${project.artifactId}-${project.version}.jar</outputFile>
    ...
</configuration>

Replace output.directory with your shade plugin outputDirectory.

Check this post with more details: post

like image 124
Ariel Carrera Avatar answered Oct 14 '22 06:10

Ariel Carrera