【问题标题】:Creating a zip archive of the maven "target" directory创建 Maven“目标”目录的 zip 存档
【发布时间】:2011-01-31 15:33:16
【问题描述】:

我希望为我的“目标”目录 (${project.build.directory) 创建一个 zip 存档。在我看来,对于这样一个简单的任务,使用 maven-assembly-plugin 有点矫枉过正(而且有点复杂——为什么我必须为这样的任务使用另一个文件,即程序集描述符) 我在http://repo1.maven.org/maven2 存储库中找不到看似更简单的maven-zip-plugin

有什么意见吗?

【问题讨论】:

  • jeejava.com/zip-file-using-maven-assembly-plugin/

标签: maven-2 plugins zip


【解决方案1】:

如果您对生成的 .zip 文件没有任何“特殊”需求,您可以使用预定义的 Maven 程序集描述符之一。预定义的程序集描述符可以轻松快速轻松地创建特定程序集,而无需提供您自己的程序集描述符。假设您想使用 bin 预定义描述符。然后在 POM 的 build 部分的 plugins 部分中,您可以添加以下内容。

<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <version>2.6</version>
  <configuration>
    <descriptorRefs>
      <descriptorRef>bin</descriptorRef>
    </descriptorRefs>
  </configuration>
</plugin>

当然,与 Maven 一样,如果你想做一些超出默认配置的事情,你必须创建自己的配置,在这种情况下,这意味着你自己的程序集描述符。

预定义描述符列表记录在here

【讨论】:

  • 不错的选择。然而,在我的过程结束时,目标目录只包含一个 exe 文件和一个包含其他 exe 文件的目录 - 所有 linstallers。我只是想把它们全部拉起来
  • 所以你的麻烦是默认的描述符会比你需要的更多,比如工作文件夹的内容等等?是否存在您要打包的目标下的特定文件夹(例如${project.build.directory}/installers)但您希望省略其他目录的情况?还是您有其他目标?
  • 我已经从 ${project.build.directory} 目录中删除了所有不需要的文件和目录,只是希望完全压缩其内容。如果您没有比 Pascal 更好的建议,我想我只需要创建他展示的“冗余”程序集文件并选择他的解决方案...
【解决方案2】:

如果bin 预定义的程序集描述符不适合您的需要,那么您有三个选择:

  1. 使用 maven-assembly-plugin - maven-zip-plugin 从未出现,因为程序集插件可以完成 zip 插件所做的一切,以及更多,请参阅 MNG-2243
  2. 使用 maven-antrun-plugin(可能还有 build-helper-plugin 来附加 zip) - 有一个示例 here(这看起来比最后的程序集插件更冗长)。
  3. 编写自己的插件 - 拥有程序集插件时为什么要这样做。

就个人而言,我只会使用带有以下 zip.xml 描述符的 maven-assembly-plugin:

<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>bin</id>
  <baseDirectory>/</baseDirectory>
  <formats>
    <format>zip</format>
  </formats>
  <fileSets>
    <fileSet>
      <directory>${project.build.directory}</directory>
    </fileSet>
  </fileSets>
</assembly>

在你的 POM 中:

<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <version>2.6</version>
  <configuration>
    <descriptors>
      <descriptor>src/main/assembly/zip.xml</descriptor>
    </descriptors>
  </configuration>
  <executions>
    <execution>
      <id>make-assembly</id> <!-- this is used for inheritance merges -->
      <phase>package</phase> <!-- append to the packaging phase. -->
      <goals>
        <goal>single</goal> <!-- goals == mojos -->
      </goals>
    </execution>
  </executions>
</plugin>

【讨论】:

  • 不起作用 - 使用了内置的“bin”。而且我不知道如何指向自定义非内置。
  • 与汇编插件相比,使用 antrun 插件有一个优势,因为它在 zip 中以不同的方式存储文件大小。如果您使用 'ZipInputStream' 读取 zip 并使用 'getSize()' 您将获得 antrun 创建的 zip 的条目大小,对于使用程序集创建的 zip,它返回 -1。
猜你喜欢
  • 2010-12-23
  • 2016-08-20
  • 1970-01-01
  • 2012-07-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多