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

发布于 2024-08-29 19:29:07 字数 455 浏览 11 评论 0原文

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

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

但是,当运行 mvn assembly: assembly 时,仅包含父文件夹(空)中的源代码。如何将模块中的源代码包含到存档中?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甜嗑 2024-09-05 19:29:07

我认为您正在寻找 Maven Shade 插件:

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

将任意数量的依赖项打包成超级包依赖项。然后可以将其部署到存储库。

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.

明天过后 2024-09-05 19:29:07

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

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

  2. 在这个新模块的 pom.xml 中,我添加了 maven-assemby-plugin。该插件打包所有类并将它们放入单个文件中。它使用额外的配置文件(步骤 4)。

<预置><代码><构建>
<插件>
<插件>
maven- assembly-plugin;
<版本>2.4
<处决>
<执行>
go-framework-assembypackage
<目标>
<目标>单个

<配置>
<描述符>
<描述符>src/main/assemble/framework_bin.xml

框架
false;





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

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

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>

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>
川水往事 2024-09-05 19:29:07

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文