Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven Shade Plugin to produce two Jars

Till now I was using maven assembly plugin to generate two JARs for each artifact - compiled sources and dependencies - the reason for this was simple - deploying only the compiled sources over network is significantly faster than deploying all-in-one-JAR with 40 MB of data.

Because of overwriting of inner files I had to switch for maven shade plugin to be able to use the <transformers> feature. However I am unable to manage to run both of the two executions:

<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <executions>
      <execution>
        <id>shade-libs</id>
        <phase>package</phase>
        <goals>
          <goal>shade</goal>
        </goals>
        <configuration>
          <outputFile>target/assembly/${project.artifactId}-libs.jar</outputFile>
          <artifactSet>
            <excludes>
              <exclude>...</exclude>
            </excludes>
          </artifactSet>
          <transformers>
            <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
              <resource>META-INF/spring.handlers</resource>
            </transformer>
            <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
              <resource>META-INF/spring.schemas</resource>
            </transformer>
          </transformers>
        </configuration>
      </execution>
    </executions>
  </plugin>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <executions>
      <execution>
        <id>shade-main</id>
        <phase>package</phase>
        <goals>
          <goal>shade</goal>
        </goals>
        <configuration>
          <outputFile>target/assembly/${project.artifactId}.jar</outputFile>
          <artifactSet>
            <includes>
              <include>...</include>
            </includes>
          </artifactSet>
        </configuration>
      </execution>
    </executions>
  </plugin>
</plugins>

When I run mvn package, only the second execution is run. The first one is always ignored. With maven assembly plugin it worked perfectly.

Of course the solution could be to use both assembly and shade plugin at the same time, but I would like to find more consistent solution.

like image 506
Vojtěch Avatar asked Jul 24 '13 21:07

Vojtěch


People also ask

How does maven shade plugin work?

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.


1 Answers

Instead of defining the plugin twice, simply define it once but with two execution sections. So in your situation it would be:

<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <executions>
      <execution>
        <id>shade-libs</id>
        <phase>package</phase>
        <goals>
          <goal>shade</goal>
        </goals>
        <configuration>
          <outputFile>target/assembly/${project.artifactId}-libs.jar</outputFile>
          <artifactSet>
            <excludes>
              <exclude>...</exclude>
            </excludes>
          </artifactSet>
          <transformers>
            <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
              <resource>META-INF/spring.handlers</resource>
            </transformer>
            <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
              <resource>META-INF/spring.schemas</resource>
            </transformer>
          </transformers>
        </configuration>
      </execution>
      <execution>
        <id>shade-main</id>
        <phase>package</phase>
        <goals>
          <goal>shade</goal>
        </goals>
        <configuration>
          <outputFile>target/assembly/${project.artifactId}.jar</outputFile>
          <artifactSet>
            <includes>
              <include>...</include>
            </includes>
          </artifactSet>
        </configuration>
      </execution>
    </executions>
  </plugin>
</plugins>
like image 126
DB5 Avatar answered Oct 17 '22 09:10

DB5