2

I have a multi-module project like this

foobar
 |
 +-- pom.xml
 |
 +-- common-lib/
 |       |
 |       +-- pom.xml
 |       +-- src/
 |
 +-- foo-app/
 |       |
 |       +-- pom.xml
 |       +-- src/
 |
 +-- bar-app/
 |       |
 |       +-- pom.xml
 |       +-- src/
 |
-+-

Both foo-app and bar-app depend on code in common-lib and also on dependencies in their own POMs.

Using mvn package I can build three light JARs.

What I want is two executable JARs, each with dependencies included, for:

  • foo-app
  • bar-app

How do I do this with maven?


In case anyone suggests it, due to clashes between the dependencies for foo-app and bar-app I cannot merge them into one single foobar-app.

Feenaboccles
  • 412
  • 3
  • 12
  • I'm confused why this is an issue. Are there "clashes between dependencies" within common-lib? – markspace Mar 07 '18 at 18:03
  • foo-app requires fdep. bar-app requires bdep. You cannot build a project that uses both fdep and bdep in the same JVM instance, they've both bundled different versions of netty into their JARs leading to AbstractMethodError exceptions at runtime. Hence I split foobar-app into foo-app and bar-app. Now I want a maven configuration for this three-module project that will generate two executable jars. – Feenaboccles Mar 07 '18 at 18:24

1 Answers1

1

Add maven assembly plugin to the pom.xml's which you want to create a executable jar with dependencies. Execute mvn package on aggregator pom.xml. This command will execute mvn package command on all the sub modules. The ones that have maven assembly plugin will generate executable jars with dependencies.

In your case add this to foo-app and bar-app projects pom.xml. And configure your <mainClass>your.package.mainclass</mainClass> according to each projects main class.

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>3.1.0</version>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <mainClass>your.package.mainclass</mainClass>
                    </manifest>
                </archive>

            </configuration>
            <executions>
                <execution>
                    <id>assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
miskender
  • 7,460
  • 1
  • 19
  • 23
  • What I get is `Failed to execute goal org.apache.maven.plugins:maven-assembly-plugin:2.4.1:assembly (default-cli) on project foobar: Error reading assemblies: No assembly descriptors found. -> [Help 1]` where`foobar` refers to the parent POM – Feenaboccles Mar 07 '18 at 18:34
  • @Feenaboccles did you add the above configuration to foobar project. also which command did you execute. your error mentions 2.4.1 version of assembly. – miskender Mar 07 '18 at 18:35
  • It’s not in the `foobar` project, just `foo-app` and `bar-app`. I executed `mvn assembly:assembly` – Feenaboccles Mar 07 '18 at 19:00
  • @Feenaboccles I suggest you to read the answer. you should execute mvn package command. – miskender Mar 07 '18 at 19:23