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

佐手、 提交于 2019-12-18 03:56:47

问题


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?


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/1895537/how-to-merge-module-jars-to-a-single-jar-in-maven2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!