如何在 Maven 中进行自定义包装?

How to do custom packaging in Maven?

我想在 运行 mvn clean package 时创建 .zip 文件而不是 .jar 文件。 .zip 文件应具有如下目录结构 -

/
 |- bin
 |- conf
 |   |- All Property Files
 |- lib
     |- All JAR Files (packaged jar and its dependency jar OR a single fat jar)

我搜索了很多,但我最终得到的唯一结果是更改了主项目文件夹的目录结构,而不是打包的内容。我知道各种包装类型,如 JAR、WAR、EAR 等,但不确定是否可以制作 .zip 文件。

是否可以制作 .zip 而不是 JAR 文件?如果是这样,如何以及可以操纵其目录结构?


更新: 找到了关于如何创建 .zip 文件的 answer。但它不包含有关更改目录结构的信息。


更新:自己解决了。请参阅下面的答案。

已找到关于如何创建 .zip 文件的 answer。似乎调整 descriptor.xml 文件会给我想要的结果。以下是我的 descriptor.xml 文件。

<assembly>
    <formats>
        <format>zip</format>
    </formats>
    <fileSets>
        <fileSet>
          <directory>${project.build.directory}</directory>
          <outputDirectory>lib</outputDirectory>
          <includes>
            <include>*.jar</include>
          </includes>
        </fileSet>
        <fileSet>
          <directory>${project.build.directory}/lib</directory>
          <outputDirectory>lib</outputDirectory>
        </fileSet>
        <fileSet>
          <directory>${project.basedir}/src/main/resources/config</directory>
          <outputDirectory>config</outputDirectory>
        </fileSet>
        <fileSet>
          <directory>${project.basedir}/src/main/resources/bin</directory>
          <outputDirectory>bin</outputDirectory>
        </fileSet>
    </fileSets>
</assembly>

自定义打包的专用 Maven 插件是 Maven Assembly plugin。 可以使用此插件将包装结构和类型更改为:

  • zip
  • tar
  • tar.gz(或 tgz)
  • tar.bz2(或 tbz2)
  • 罐子
  • 目录
  • war
  • 自定义(参见 ArchiveManager)

There is the documentation 包含插件配置示例。

根据文档,您需要:

  • 设置格式(zip、tgz、...)
  • 为您当前的个人资料激活插件
  • 在默认情况下在 src/assembly 文件夹中找到的专用程序集 bin.xml 文件中定义特定的包结构
  • 显然,调用 mvn 包阶段(在安装过程中自动...)以便奇迹发生:)