4

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!

A_Di-Matteo
  • 26,902
  • 7
  • 94
  • 128
Wisco crew
  • 1,337
  • 1
  • 17
  • 25

1 Answers1

7

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).

A_Di-Matteo
  • 26,902
  • 7
  • 94
  • 128
  • 1
    Fantastic! One note for anyone else running into this: I wanted to copy the jars into the main-level `target/` dir, using `jar-with-dependencies`. To do so, I had to put the maven-assembly-plugin ahead of maven-dependency plugin in the parent pom, otherwise the jar-with-dependencies wasn't built before maven tried to copy it over. Didn't know plugin order mattered! – Wisco crew Dec 09 '15 at 22:23
  • This didn't work for me. I'm using assembly plugin v3.1. Any idea why this maybe the case? – Ahmed Moawad Mar 03 '18 at 13:35
  • 1
    It worked when I used: `mvn clean compile assembly:single` instead of `mvn package`. Maybe the assembly step was not hooked up with the package step, I'm not sure. – Ahmed Moawad Mar 03 '18 at 16:29
  • @AhmedMoawad building my project with `mvn clean compile assembly:single` gives me `Error reading assemblies: No assembly descriptors found.`. Building with `mvn clean package` gives me JARs with dependencies but without my sources... It's a Kotlin project, but when there was no parent it worked fine. – Jezor Jul 28 '19 at 10:23