Search code examples
javamavenuberjarmaven-module

Trying to build uberjar from multiple maven modules


I am trying to build a single uber-jar from a set a modules that are mostly independent, but it's not working the way I'd thought.

I was initially directed here: https://maven.apache.org/plugins/maven-assembly-plugin/examples/multimodule/module-binary-inclusion-simple.html though now I'm not quite certain that this was what I should have started with...

The parent pom file looks like this:

<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/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mycompany</groupId>
    <artifactId>BigProject</artifactId>
    <version>0.0.1</version>
    <packaging>pom</packaging>
    <name>BigProject</name>

    <modules>
        <module>../mod1</module>
        <module>../mod2</module>
        <!-- ...a bunch more... -->
        <module>../distribution</module>
    </modules>
<build>
    <pluginManagement>
        <plugins>
            <!-- not even sure why this needs to be specified here, but the documentation seems to think it's important... -->
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.1.0</version>
                <configuration>
                    <descriptors>
                        <descriptor>src/assembly/assembly.xml</descriptor>
                    </descriptors>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>
</project>

The pom for the "distribution" module looks like this:

<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/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <artifactId>distribution</artifactId>
    <name>distribution</name>
    <packaging>pom</packaging>

    <parent>
        <groupId>com.mycompany</groupId>
        <artifactId>BigProject</artifactId>
        <version>0.0.1</version>
    </parent>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.1.0</version>
                <executions>
                    <execution>
                        <id>distro-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <descriptors>
                                <descriptor>src/assembly/assembly.xml</descriptor>
                            </descriptors>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

And finally, the assembly file:

<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
    <id>bin</id>
    <formats>
        <format>dir</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>

    <moduleSets>
        <moduleSet>
            <includeSubModules>true</includeSubModules>
            <useAllReactorProjects>true</useAllReactorProjects>
            <includes>
                <include>com.myCompany:mod1</include>
                <include>com.myCompany:mod2</include>
                <!-- and others... -->
            </includes>
            <binaries>
                <includeDependencies>true</includeDependencies>
                <outputDirectory>modules/maven-assembly-plugin</outputDirectory>
                <unpack>false</unpack>
            </binaries>
        </moduleSet>
    </moduleSets>
</assembly>

(I realize that at some point I will probably need to change <format>dir</format> to <format>jar</format> but for now, I'd just like to get something working)

When I run mvn clean package from the main parent module's directory, I get warnings like this:

[INFO] Reading assembly descriptor: src/assembly/assembly.xml
[WARNING] The following patterns were never triggered in this artifact inclusion filter:
o  'com.myCompany:mod1'
o  'com.myCompany:mod2'
...

Followed by the error:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-assembly-plugin:3.1.0:single (distro-assembly) on project distribution: Failed to create assembly: Error creating assembly archive bin: archive cannot be empty -> [Help 1]

What I actually want:

One jar file containing all of my modules, and all of their dependencies (that is, the *-with-dependencies.jars), as a single jar file with everything contained inside.

I'm really not sure how to achieve this in a multi-module context.


Solution

  • I think I may have resolved it: the child modules mod1, mod2, ... were missing the parent module dependency. I've added

    <parent>
        <groupId>com.mycompany</groupId>
        <artifactId>BigProject</artifactId>
        <version>0.0.1</version>
    </parent>
    

    to the relevant child modules and now the build doesn't fail, but populates the target directory under the distribution project.