Maven

Summary

Notes on the Maven build system.

Artifacts

Über JARs

There are many ways to build über JARs, but we will talk about two, maven-assembly-plugin and maven-shade-plugin.

Assembly

The maven-assembly-plugini 1 adds all dependencies inside the final fat JAR and can be used by adding the following to your pom.xml:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-assembly-plugin</artifactId>
  <configuration>
    <descriptorRefs>
      <descriptorRef>jar-with-dependencies</descriptorRef>
    </descriptorRefs>
    <archive>
      <manifest>
        <mainClass>{your.package.main.class}</mainClass>
      </manifest>
    </archive>
  </configuration>
  <executions>
    <execution>
      <phase>package</phase>
      <goals>
        <goal>single</goal>
      </goals>
    </execution>
  </executions>
</plugin>

Shade

The maven-shade-plugin 2 also adds all dependencies inside the final fat JAR, but additionally executes shading. Add the following to your pom.xml:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-shade-plugin</artifactId>
  <configuration>
    <transformers>
      <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
        <mainClass>{your.package.main.class}</mainClass>
      </transformer>
    </transformers>
  </configuration>
  <executions>
    <execution>
      <phase>package</phase>
      <goals>
        <goal>shade</goal>
      </goals>
    </execution>
  </executions>
</plugin>

  1. https://maven.apache.org/plugins/maven-assembly-plugin/ ↩︎

  2. https://maven.apache.org/plugins/maven-shade-plugin/ ↩︎