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

前端 未结 2 713
陌清茗
陌清茗 2020-12-16 13:58

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

相关标签:
2条回答
  • 2020-12-16 14:36

    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.

    0 讨论(0)
  • 2020-12-16 14:41

    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

    0 讨论(0)
提交回复
热议问题