【问题标题】:How to do custom packaging in Maven?如何在 Maven 中进行自定义打包?
【发布时间】:2016-03-18 05:47:40
【问题描述】:

我想在运行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 文件?如果是这样,它的目录结构如何以及可以被操纵?


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


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

【问题讨论】:

    标签: java maven zip packaging


    【解决方案1】:

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

    • 压缩包
    • 焦油
    • tar.gz(或 tgz)
    • tar.bz2(或 tbz2)
    • 罐子
    • 目录
    • 战争
    • 自定义(参见 ArchiveManager)

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

    根据文档,您需要:

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

    【讨论】:

    • 将描述符文件保存在 src/assembly 中是否是一个好习惯,或者我可以将它保存在任何地方,因为我将它保存在项目文件夹中。
    • src/assembly 文件夹默认不是源文件夹,因此它不会成为最终打包项目的一部分(未交付)。必须在 POM 中的插件配置期间指定文件的位置和名称。 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <configuration> <descriptor>src/assembly/bin.xml</descriptor> <finalName>my_super_project -${pom.version}</finalName> </configuration> ...
    【解决方案2】:

    找到了关于如何创建 .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>
    

    【讨论】:

      猜你喜欢
      • 2017-01-28
      • 2020-06-16
      • 2018-10-29
      • 1970-01-01
      • 2019-03-20
      • 2016-08-12
      • 1970-01-01
      • 1970-01-01
      • 2016-11-12
      相关资源
      最近更新 更多