Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

maven multimodule project: can I jar-with-dependencies?

I have a maven project, with a main project A and modules B and C. The children inherit from A's pom.

A
|
|----B
|    |----pom.xml
|
|----C
|    |----pom.xml
| 
|----pom.xml

It already builds jars for all the modules. Is there a way to include the dependencies in those jars? E.g. so I get B-1.0-with-dependencies.jar and C-1.0-with-dependencies.jar? I have tried setting

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.6</version>
    <configuration>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
    </configuration>
</plugin>

In the parent pom, but it doesn't seem to do anything: the build succeeds, but I get regular, no-dependency jars.

I'd like to avoid putting something in each child pom, as in reality I have more than 2 modules. I'm sure there is some way to do this, but can't seem to work it out from the maven docs. Thanks!

like image 896
Wisco crew Avatar asked Sep 03 '25 14:09

Wisco crew


1 Answers

This is how I made it work.
In the aggregator/parent pom I configured:

<properties>
    <skip.assembly>true</skip.assembly>
</properties>

<build>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.6</version>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <skipAssembly>${skip.assembly}</skipAssembly>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Note the skip.assembly property, which is set by default to true. That means the assembly would not be executed on the parent, which makes sense since the parent doesn't provide any code (having packaging pom).

Then, in each module I configured simply the following:

<properties>
    <skip.assembly>false</skip.assembly>
</properties>

Which means in each submodule the skip is disabled and the assembly is executed as configured in the parent. Moreover, with such a configuration, you can also easily skip the assembly for a certain module (if required).

Please also note the assembly configuration on the parent, I added an execution on top of the configuration you provided so that the assembly plugin is triggered automatically when invoking mvn clean package (or mvn clean install).

like image 61
A_Di-Matteo Avatar answered Sep 05 '25 02:09

A_Di-Matteo