Search code examples
javamavenmaven-3maven-shade-plugin

Maven Shade plugin configuration to create uber jar containing just the submodules based in the same groupId


Having the following situation:

  • STS
  • Java
  • Maven

Machine One

workspace-01

the Java app with Maven is based for a single module. It is installed (mvn install) in the repository and the master-project.jar file in other Machine is installed and used for other project how

<dependency>
  <groupId>com.manuel.jordan</groupId>
  <artifactId>master-project</artifactId>
  <version>0.0.1.SNAPSHOT</version>
</dependency>

Until here all work fine

workspace-02

The app grew up and in other workspace the app was migrated to work with multi-modules

Lets assume the following structure for simplicity

master-project (<packaging>pom</packaging>)
alpha (<packaging>jar</packaging>)
beta (<packaging>jar</packaging>)
numbers (<packaging>pom</packaging>)
   one (<packaging>jar</packaging>)
   two (<packaging>jar</packaging>)
countries (<packaging>pom</packaging>)
   Europe (<packaging>pom</packaging>)
      France (<packaging>jar</packaging>)
      Italy (<packaging>jar</packaging>)
   AmericaLatina (<packaging>pom</packaging>)
      Peru (<packaging>jar</packaging>)
      Argentina (<packaging>jar</packaging>)

I am able to compile all these modules. Therefore build success

Goal

Now the goal is generate the same master-project.jar file including the sub modules how internal jars.

After to do a research the solution is: maven-shade-plugin

I have the following configuration (part of it) using how reference the following links and other sources:

Therefore:

  <modelVersion>4.0.0</modelVersion>
  <packaging>pom</packaging>  <--- it is the main or root pom.xml file

  <groupId>com.manuel.jordan</groupId>
  <artifactId>master-project</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <properties>
    ....
  </properties>

  <dependencies>
    <dependency>
        <groupId>com.hazelcast</groupId>
        <artifactId>hazelcast-all</artifactId>
        <version>${hazelcast.version}</version>
        <scope>provided</scope> <!-- Otherwise each uber jar within each submodule is 11.6MB -->
    </dependency>
  </dependencies>

  <modules>
    <module>...</module>
    ...
  </modules>

  <build>
    <plugins>
      ....
      <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.2.4</version>
            <executions>
                <execution>
                    <id>create-fat-jar</id>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <artifactSet>
                            <includes>
                                <include>com.manuel.jordan</include>
                            </includes>
                        </artifactSet>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
  </build>

With the current configuration happens the following:

For each module within the target directory:

  • With <packaging>pom</packaging> generates a .pom file
  • With <packaging>jar</packaging> generates two .jar files, one of the sub module itself and other of the uber jar (yes for each module)

Goal: what is the extra or missing configuration to generate something like this:

master-project.jar containing
 module1.jar
 module2.jar
 ...
 module3.jar

Where each module# have the same <groupId>com.manuel.jordan</groupId> declaration, that's why I declared

<configuration>
   <artifactSet>
      <includes>
         <include>com.manuel.jordan</include>
      </includes>
   </artifactSet>
</configuration>

And observe that the root/main pom.xml file has <packaging>pom</packaging>

Goal

Therefore the goal is that the other project located in the other machine keeps in peace

<dependency>
  <groupId>com.manuel.jordan</groupId>
  <artifactId>master-project</artifactId>
  <version>0.0.1.SNAPSHOT</version>
</dependency>

Thus it does not matter if the master-project project is either single or multi-module


Solution

  • You could make a packaging jar module below your master-project with a dependency to the other modules, and include only there the shade plugin configuration