【问题标题】:Creating one jar file from maven modules从 maven 模块创建一个 jar 文件
【发布时间】:2019-09-12 12:07:40
【问题描述】:

我在主项目中有两个 maven 模块的 Spring Boot 项目,每个模块和主项目都有自己的 pom 文件。使用 mvn install 时,我在目标文件夹中获得了两个 jar 文件。那么如何更改 pom 以从应用程序中仅创建一个 jar?

<modules>
       <module>module_1</module>
       <module>module_2</module>
</modules>

【问题讨论】:

  • 对于每个模块,你都会得到一个 jar。如果您想要更少的 jar,请使用更少的模块。

标签: maven


【解决方案1】:

尝试使用Apache Maven Assembly Plugin

配置项目的插件(文档:here)。

配置其描述符(文档:here)例如:

<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
  <id>src</id>
  <formats>
    <format>dir</format>
  </formats>
  <includeBaseDirectory>false</includeBaseDirectory>
  <moduleSets>
    <moduleSet>
      <useAllReactorProjects>true</useAllReactorProjects>
      <includes>
        <include>${groupId}module_1</include>
      </includes>
      <sources>
        <includeModuleDirectory>false</includeModuleDirectory>
        <fileSets>
          <fileSet>
            <outputDirectory>sources/${module.artifactId}</outputDirectory>
          </fileSet>
        </fileSets>
      </sources>
    </moduleSet>
  </moduleSets>
</assembly>

并运行mvn clean assembly:single 目标来构建程序集。 它将所有类打包在一个目标 jar 文件中。

【讨论】:

    【解决方案2】:

    如果是多模块 Spring Boot 应用程序,您将在每个模块的目标文件夹中获得一个 jar 文件。如果是父级,您将获得一个胖 jar 文件,其中包含来自每个模块的所有所有相应的 jar 文件。查看以下有关结构的 pom.xml 文件。

    https://github.com/debjava/restful-basic-security/blob/master/pom.xml

    如果你没有使用多模块spring boot应用,你可以使用Maven shade插件来创建一个fat jar。您可以查看以下链接。

    https://maven.apache.org/plugins/maven-shade-plugin/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-22
      • 1970-01-01
      • 1970-01-01
      • 2022-01-16
      • 2016-11-03
      • 1970-01-01
      相关资源
      最近更新 更多