【问题标题】:Ant - Zip Multiple FoldersAnt - 压缩多个文件夹
【发布时间】:2023-03-19 11:36:01
【问题描述】:

我正在尝试在 Maven 中运行 Ant 作业以查看文件夹,并根据该文件夹中有多少个文件夹,创建 x 个具有文件夹名称的 zip 文件。我让它手动工作,但如果我不必在每次向这个结构中添加一个新文件夹时都编辑 pom,那就太好了。

<configuration>
    <target name="zip">
        <zip destfile="root/sub1/sub1.jar"> 
        <zipfileset dir="root/sub1/unpacked/" includes="**" ></zipfileset>
        </zip>
    </target>
</configuration>

如果我要将 sub2 添加到根目录,我希望它自动被拾取,并创建一个 sub2.jar 文件(是的,我知道我正在使用 .jar,但程序是获取这些文件需要 .jar 文件,但它们不是 jar 文件,因为它们包含任何 Java 代码,它们只是带有 jar 扩展名的 zip 文件)

我试过了

谢谢我看了第一个链接,但也许我做错了

<target name="checkDir">
    <foreach target="zip" param="theFile">
        <dirset dir="root" casesensitive="yes">
            <include name="**"/>
        </dirset>
    </foreach>
    </target>
<target name="zip">
<!-- <zip destfile="root/${theFile}/${theFile}.jar"> 
                            <zipfileset dir="root/${theFile}/unpacked/" includes="**" ></zipfileset>
</zip> -->
<echo message="${theFile}"/>
</target>

我刚刚得到

[INFO] --- maven-antrun-plugin:1.7:run (process-javascript-plugin) @war--- [INFO] 执行任务

邮编: [回声] ${theFile} [INFO] 已执行的任务


好像还是不行。

pom.xml

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.7</version>
                <executions>
                    <execution>
                        <id>process-cartridges</id>
                        <phase>compile</phase>
                        <configuration>
                        <target>
                           <ant antfile="root/build-main.xml"/>
                        </target>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

build-main.xml

<?xml version="1.0" encoding="UTF-8" ?>
<project>
    <target name="checkDir">
        <foreach target="zip" param="theFile">
            <dirset dir="root" casesensitive="yes">
                <include name="**"/>
            </dirset>
        </foreach>
    </target>
    <target name="zip">
        <zip destfile="root/${theFile}/${theFile}.jar"> 
            <zipfileset dir="root/${theFile}/unpacked/" includes="**" />
        </zip>
        <echo message="TESTZIP ${theFile}"/>
    </target>
</project>

似乎没有工作。我错过了什么吗?

【问题讨论】:

  • 我想到的问题是这样包装的是什么?
  • 它只是一个 XML 文件的集合。为什么这样做不是我关心的问题,因为它是我必须使用的内部产品,并且没有改变的发言权,所以我只能顺其自然!使用 ant-jar 会在 META-INF 文件夹中抛出,这就是我不这样做的原因。
  • 我可以想象的最简单的解决方案是使用带有适当描述符的 maven-assembly-plugin....

标签: maven ant zip ant-contrib


【解决方案1】:

您可以使用 Ant 的 foreach 任务来执行此操作,结合这两个帖子的答案:Ant: How to execute a command for each file in directory?Calling foreach in maven-antrun-plugin

但是,您也可以使用 Maven 解决方案,使用 iterator-maven-plugin 遍历所有子文件夹,然后使用 maven-jar-plugin 制作 jar:

<plugin>
  <groupId>com.soebes.maven.plugins</groupId>
  <artifactId>iterator-maven-plugin</artifactId>
  <version>0.3</version>
  <executions>
    <execution>
      <phase>package</phase>
      <goals>
        <goal>iterator</goal>
      </goals>
      <configuration>
        <folder>root/</folder>
        <pluginExecutors>
          <pluginExecutor>
            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-jar-plugin</artifactId>
              <version>2.6</version>
            </plugin>
            <goal>jar</goal>
            <configuration>
              <classesDirectory>root/@item@/unpacked</classesDirectory>
              <outputDirectory>root/@item@</outputDirectory>
              <finalName>@item@.jar</finalName>
            </configuration>
          </pluginExecutor>
        </pluginExecutors>
      </configuration>
    </execution>
  </executions>
</plugin>

这绑定到package 阶段。它将遍历 root 文件夹的所有直接子文件夹,并使用给定的配置调用 jar 插件。 @item@ 用于引用当前目录名。

【讨论】:

  • 谢谢我看了第一个链接,但也许我做错了更新主帖
  • @ChrisO'Brien 我知道,这就是我添加第二个链接的原因:你不能有超过 2 个目标。但我建议将其他解决方案与 iterator-maven-plugin 一起使用。
  • 酷。我会深入研究的。 maven 插件可能不是一个选项,因为它是一个内部仓库,但如果外部 ant 文件不起作用,我会记住。谢谢!
  • 您能再快速浏览一下吗?它仍然没有捡起它。
  • @ChrisO'Brien 我认为应该是&lt;ant antfile="root/build-main.xml" target="checkDir"/&gt;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-05
相关资源
最近更新 更多