maven: multi-module project assembly into single jar

◇◆丶佛笑我妖孽 提交于 2019-11-30 12:45:26

I think you are looking for the Maven Shade Plugin:

http://maven.apache.org/plugins/maven-shade-plugin/index.html

Packages up any number of dependencies into an uber package depenency. This can then be deployed to a repository.

To package classes from all modules to a single jar I did the following:

  1. Created additional module that is used only for packing contents of all other modules to a single jar. This is usually reffered to as a assembly module. Try calling this module same as target jar file.

  2. In pom.xml of this new module i added maven-assemby-plugin. This plugin packages all classes and puts them in single file. It uses additional configuration file (step 4.)

<build>
    <plugins>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.4</version>
        <executions>
          <execution>
            <id>go-framework-assemby</id>
            <phase>package</phase><!-- create assembly in package phase (invoke 'single' goal on assemby plugin)-->
            <goals>
              <goal>single</goal>
            </goals>
            <configuration>
              <descriptors>
                <descriptor>src/main/assemble/framework_bin.xml</descriptor>
              </descriptors>
                  <finalName>framework</finalName>
                  <appendAssemblyId>false</appendAssemblyId>
          </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

3.In pom.xml of this new module I also added dependencies to all other modules including parent pom. Only modules included in dependencies will be packed in target jar file.

<dependencies>
    <dependency>
        <groupId>${project.groupId}</groupId>
        <artifactId>fwk-bam</artifactId>
        <version>${project.version}</version>
    </dependency>...

4.Finally i created assembly descriptor in assembly module (file: src/main/assemble/framework_bin.xml)

<assembly
    xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
    <id>all-jar</id>
    <formats>
        <format>jar</format> <!-- the result is a jar file -->
    </formats>

    <includeBaseDirectory>false</includeBaseDirectory> <!-- strip the module prefixes -->

    <dependencySets>
        <dependencySet>
            <unpack>true</unpack> <!-- unpack , then repack the jars -->
            <useTransitiveDependencies>false</useTransitiveDependencies> <!-- do not pull in any transitive dependencies -->
        </dependencySet>
    </dependencySets>
</assembly>

The predefined bin won't do the trick here. You'll have to use a custom descriptor similar to the predefined bin descriptor but that declares moduleSet to include your project modules.

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