Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure Maven shade plugin in a multi-module project?

I have been trying to get jar using Maven Shade Plugin, but I still don't get a success.

This is my project structure:

MainModule
  -Module1
    -src
    -pom.xml
  -Module2
    -src
    -pom.xml
  -pom.xml

Module1 (pom.xml):

<parent>
    <artifactId>MainModule</artifactId>
    <groupId>com.plugintest</groupId>
    <version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>Module1</artifactId>

Module2 (pom.xml):

<parent>
    <artifactId>MainModule</artifactId>
    <groupId>com.plugintest</groupId>
    <version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>Module1</artifactId>

MainModule (pom.xml):

<groupId>com.plugintest</groupId>
<artifactId>MainModule</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
    <module>Module1</module>
    <module>Module2</module>
</modules>
<build>
    <plugins>
        <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>2.2</version>
        <executions>
            <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            </execution>
        </executions>
        </plugin>
    </plugins>
</build>

According this code I get 2 jar-files (Module1-version.jar and Module2-version.jar). But it is not what I want. I wish to get 1 jar file (MainModule-version.jar), which would contain the other (Module1 and Module2).

Why doesn't this Shade Plugin work?

like image 254
nightin_gale Avatar asked Jan 09 '14 13:01

nightin_gale


People also ask

What is use of Maven Shade plugin?

This plugin provides the capability to package the artifact in an uber-jar, including its dependencies and to shade - i.e. rename - the packages of some of the dependencies.

How can I add an existing Maven project to another Maven project as a module?

Just do a regular "Import existing maven project into workspace" to get this done. Show activity on this post. If you use M2e with Eclipse you do not need to do that, because Eclipse resolves dependencies across the workspace. You just need to have the two projects open and your dependencies declared correctly.


1 Answers

You MainModule is not supposed to produce a jar file. It can produce only... pom files. It contains configuration shared across all it child modules. This is why the shade plugin is called against each modules.

Instead, create a third module. Let's call it FinalModule. This module is a child of MainModule. Move the whole <build> node from MainModule pom.xml to FinalModule pom.xml.

File structure:

   MainModule
      -FinalModule
        -src
        -pom.xml
      -Module1
        -src
        -pom.xml
      -Module2
        -src
        -pom.xml
      -pom.xml

The FinalModule pom.xml looks like this:

FinalModule (pom.xml)

<parent>
    <groupId>com.plugintest</groupId>
    <artifactId>MainModule</artifactId>
    <version>1.0-SNAPSHOT</version>
</parent>
<artifactId>FinalModule</artifactId>

<dependencies>
    <dependency>
        <groupId>com.plugintest</groupId>
        <artifactId>Module1</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>com.plugintest</groupId>
        <artifactId>Module2</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.2</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

In the end, you should get something like this:

[INFO] 
[INFO] --- maven-jar-plugin:2.3.2:jar (default-jar) @ FinalModule ---
[INFO] Building jar: D:\workspaces\java\Parent\FinalModule\target\FinalModule-1.0-SNAPSHOT.jar
[INFO] 
[INFO] --- maven-shade-plugin:2.2:shade (default) @ FinalModule ---
[INFO] Including my:Module1:jar:1.0-SNAPSHOT in the shaded jar.
[INFO] Including my:Module2:jar:1.0-SNAPSHOT in the shaded jar.
[INFO] Replacing original artifact with shaded artifact.
[INFO] Replacing D:\workspaces\java\Parent\FinalModule\target\FinalModule-1.0-SNAPSHOT.jar with D:\workspaces\java\Parent\FinalModule\target\FinalModule-1.0-SNAPSHOT-shaded.jar
[INFO] Dependency-reduced POM written at: D:\workspaces\java\Parent\FinalModule\dependency-reduced-pom.xml
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO] 
[INFO] Parent ............................................ SUCCESS [0.016s]
[INFO] Module1 ........................................... SUCCESS [1.654s]
[INFO] Module2 ........................................... SUCCESS [0.343s]
[INFO] FinalModule ....................................... SUCCESS [0.953s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
like image 113
Stephan Avatar answered Sep 18 '22 15:09

Stephan