maven: multi-module project assembly into single j

2019-02-16 14:26发布

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:

<plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-assembly-plugin</artifactId>
 <configuration>
  <descriptorRefs>
   <descriptorRef>bin</descriptorRef>
  </descriptorRefs>
 </configuration>
</plugin>

However, when running mvn assembly:assembly, only the source from the parent folder (empty) are included. How do I include the sources from my modules into the archive?

3条回答
Juvenile、少年°
2楼-- · 2019-02-16 15:12

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.

查看更多
Anthone
3楼-- · 2019-02-16 15:22

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.

查看更多
叼着烟拽天下
4楼-- · 2019-02-16 15:28

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>
查看更多
登录 后发表回答