使用 Maven 修改多个 zip 文件内的文件

发布于 2024-12-29 10:17:02 字数 330 浏览 1 评论 0原文

今天我想做这个任务,途中遇到了一些问题。所以我在这里提出我的问题和我找到的解决方案。也许有人知道一个更简单的解决方案!

问题是这样的:我试图构建一个使用 Maven2 构建的 Java 项目的分发包。在上一步中,生成了几个 zip 文件,它们的根目录中都包含一个名为 manifest.xml 的文件,我想在所有这些 ZIP 文件中修改此 XML 文件。这是一个方案:

package-file-1.zip
|- contents(DIR)
\- manifest.xml

package-file-2.zip
|- contents(DIR)
\- manifest.xml

Today I wanted to perform this task, and I run across some issues in the way. So I present here my problem and the solution I found. Perhaps somebody knows a simpler solution!

The problem was this: I was trying to build a distribution package of a Java project which is built with Maven2. In a previous step, several zip files all containing a file named manifest.xml in the root were generated, and I wanted to modify this XML file in all this ZIP files. This is a scheme:

package-file-1.zip
|- contents(DIR)
\- manifest.xml

package-file-2.zip
|- contents(DIR)
\- manifest.xml

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

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

发布评论

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

评论(1

ま昔日黯然 2025-01-05 10:17:02

此示例修改 ${zip.sourcedir} 中的 zip 文件,将清单.xml 文件中的字符串 & 替换为 &所有这些 zip 文件,并将修改后的 zip 文件放入目录 target 中。

为此,它使用 maven-antrun-plugin ,包括 antcontrib 任务中的 forvar 任务( http://ant-contrib.sourceforge.net)。这允许将每个 zip 文件的内容解压缩到单独的目录中。另请注意使用 basename 任务从 zip 文件的路径中提取名称。

<build>
<plugins>
<plugin>
  <artifactId>maven-antrun-plugin</artifactId>
  <version>1.7</version>

  <executions>
    <execution>
      <id>copy-and-repair-zips</id>
      <phase>initialize</phase>
      <goals>
        <goal>run</goal>
      </goals>                          
      <configuration>                           
        <tasks>                               
          <taskdef resource="net/sf/antcontrib/antlib.xml" classpathref="maven.plugin.classpath"/>
          <for param="filepath">
            <path>
                 <fileset dir="${zip.sourcedir}" includes="**/*.zip"/>
            </path>
            <sequential>
                    <var name="for.filename" unset="true" />
                    <basename property="for.filename" file="@{filepath}" />

                    <unzip src="@{filepath}" dest="target/repair-temp/${for.filename}" encoding="UTF8" />

                    <replace file="target/repair-temp/${for.filename}/manifest.xml" token="&" value="&amp;" encoding="UTF8" />

                    <zip basedir="target/repair-temp/${for.filename}" destfile="target/${for.filename}" encoding="UTF8" />
            </sequential>
          </for>
        </tasks>
      </configuration>
    </execution>
  </executions>
  <dependencies>
    <dependency>
      <groupId>ant-contrib</groupId>
      <artifactId>ant-contrib</artifactId>
      <version>1.0b3</version>
      <exclusions>
        <exclusion>
          <groupId>ant</groupId>
          <artifactId>ant</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
  </dependencies>
</plugin>
</plugins>
</build>

为了编写这个解决方案,我从以下 URL 中获得了所需的知识:

编辑

发布问题后,我找到了几个相关的问题,如果有人有问题,这可能会有所帮助实现类似的问题时遇到问题:

This example modifies the zip files in ${zip.sourcedir} replacing the string & with & in the file manifest.xml of all this zip files and places the modified zip files in the directory target.

For that, it uses the maven-antrun-plugin including the for and var tasks from the antcontrib tasks(http://ant-contrib.sourceforge.net). This permits to unzip the contents of every zip file into a separate directory. Note also the use of the task basename to extract the name of the zip files out of their path.

<build>
<plugins>
<plugin>
  <artifactId>maven-antrun-plugin</artifactId>
  <version>1.7</version>

  <executions>
    <execution>
      <id>copy-and-repair-zips</id>
      <phase>initialize</phase>
      <goals>
        <goal>run</goal>
      </goals>                          
      <configuration>                           
        <tasks>                               
          <taskdef resource="net/sf/antcontrib/antlib.xml" classpathref="maven.plugin.classpath"/>
          <for param="filepath">
            <path>
                 <fileset dir="${zip.sourcedir}" includes="**/*.zip"/>
            </path>
            <sequential>
                    <var name="for.filename" unset="true" />
                    <basename property="for.filename" file="@{filepath}" />

                    <unzip src="@{filepath}" dest="target/repair-temp/${for.filename}" encoding="UTF8" />

                    <replace file="target/repair-temp/${for.filename}/manifest.xml" token="&" value="&amp;" encoding="UTF8" />

                    <zip basedir="target/repair-temp/${for.filename}" destfile="target/${for.filename}" encoding="UTF8" />
            </sequential>
          </for>
        </tasks>
      </configuration>
    </execution>
  </executions>
  <dependencies>
    <dependency>
      <groupId>ant-contrib</groupId>
      <artifactId>ant-contrib</artifactId>
      <version>1.0b3</version>
      <exclusions>
        <exclusion>
          <groupId>ant</groupId>
          <artifactId>ant</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
  </dependencies>
</plugin>
</plugins>
</build>

In order to write this solution, I got the needed knowledge from this URLs:

Edit

After posting the question, I was able to find a couple of questions related, that could help if one is having problems implementing a similar thing:

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