maven:将多模块项目组装成单个jar

Jer*_*oen 11 maven-2 archive maven maven-assembly-plugin multi-module

我有一个多模块项目,想要创建一个包含所有模块类的jar.在我的父POM中,我声明了以下插件:

<plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-assembly-plugin</artifactId>
 <configuration>
  <descriptorRefs>
   <descriptorRef>bin</descriptorRef>
  </descriptorRefs>
 </configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)

但是,在运行mvn assembly:assembly时,只包含父文件夹中的源(空).如何将模块中的源包含到存档中?

Lin*_*oln 7

我想你正在寻找Maven Shade插件:

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

将任意数量的依赖项打包成uber包的依赖性.然后可以将其部署到存储库.


zor*_*ran 6

要将所有模块中的类打包到单个 jar 中,我执行了以下操作:

  1. 创建了仅用于将所有其他模块的内容打包到单个 jar 的附加模块。这通常被称为组装模块。尝试调用与目标 jar 文件相同的模块。

  2. 在这个新模块的 pom.xml 中,我添加了 maven-assemby-plugin。该插件打包所有类并将它们放在单个文件中。它使用额外的配置文件(第 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>
Run Code Online (Sandbox Code Playgroud)

3.在这个新模块的 pom.xml 中,我还添加了对所有其他模块的依赖,包括父 pom。只有包含在依赖项中的模块才会打包到目标 jar 文件中。

<dependencies>
    <dependency>
        <groupId>${project.groupId}</groupId>
        <artifactId>fwk-bam</artifactId>
        <version>${project.version}</version>
    </dependency>...
Run Code Online (Sandbox Code Playgroud)

4.最后我在程序集模块中创建了程序集描述符(文件: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>
Run Code Online (Sandbox Code Playgroud)


Pas*_*ent 0

预定义bin在这里不起作用。您必须使用类似于预定义描述符的自定义描述符bin,但声明moduleSet包含您的项目模块。