maven: multi-module project assembly into single jar

后端 未结 3 2000
傲寒
傲寒 2021-01-02 02:06

I have a multi-module project and want to create a single jar containing the classes of all my modules. Inside my parent POM, I declared the following plugin:



        
相关标签:
3条回答
  • 2021-01-02 02:11

    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>
    
    0 讨论(0)
  • 2021-01-02 02:14

    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.

    0 讨论(0)
  • 2021-01-02 02:34

    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.

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