Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to merge module jars to a single jar in Maven2?

Tags:

maven-2

I have a maven2 project with several jar modules, build the project will get .jar archives for each module in the directory modules/XYZ/target/XYZ-x.x.x.jar

Now, if my project P is of version P-p.q.r, and I want to generate a single jar P-p.q.r-all.jar with all sub-modules included in, how should I do?

like image 686
Xiè Jìléi Avatar asked Dec 13 '09 04:12

Xiè Jìléi


2 Answers

What you want to achive is called uber jar. This module has to have dependecies of all others submodules you want to package into one jar. If you create another submodule that will produce a desired artifact it can be built in reactor with all its dependencies but if it will be a separate project that you have to install all uber jar dependecies.

| parent
| -- submodule1
...
| -- submoduleN
| -- uberjarSubmodule

Uber jar can be done by using:

  1. maven-shade-plugin - in your case you have to remember to exclude transitive dependecies from your modules

    <project>
    ...
    <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>1.2.2</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <artifactSet>
                <excludes>
                  <exclude>classworlds:classworlds</exclude>
                  <exclude>junit:junit</exclude>
                  <exclude>jmock:jmock</exclude>
                  <exclude>xml-apis:xml-apis</exclude>
                </excludes>
              </artifactSet>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
    </build>
    ...
    </project>
    
  2. maven-assembly-plugin - in this question you'll find a detailed answer

like image 99
cetnar Avatar answered Nov 18 '22 17:11

cetnar


Depends on how you are going to ship this, if your jar is a library that you want other developers to download and use via maven. You should specify these as dependencies in the projects pom.

If you are trying to ship something to an end-user who just wants to grab the binary and use your project, you could try using the assembly plugin to package your project. With this plugin you can package a jar alongside its dependencies. It won't put it all in a single jar file, but assuming you configure the users classpath correctly it shouldn't matter.

like image 39
Clinton Avatar answered Nov 18 '22 15:11

Clinton