【问题标题】:How to turn directories to .jar files with Ant如何使用 Ant 将目录转换为 .jar 文件
【发布时间】:2009-10-12 12:27:24
【问题描述】:

如何将目录列表转换为 .jar 文件列表? 似乎使用 Copy Task 和 Mappers 是正确的方法,但我没有找到任何从目录创建 jar 文件的嵌套元素。

当然,目录列表将被计算出来,而不是硬编码在 ant 脚本中。

例如,一个 foo 目录包含 bar1、bar2 和 bar3 目录。该脚本将从 bar1 目录创建 bar1.jar,从 bar2 目录创建 bar2.jar,从 bar3 目录创建 bar3.jar 该脚本将从新添加的 bar4 目录中创建一个 bar4.jar,而无需对脚本进行任何更改(无硬编码列表)。

感谢您的帮助

【问题讨论】:

    标签: ant jar


    【解决方案1】:

    您可以使用subant 任务来执行此操作。例如:

    <project name="jars" default="jar" basedir=".">
        <property name="dir.build" value="${basedir}/build"/>
        <property name="dir.root" value="${basedir}/foo"/>
    
        <target name="init">
            <tstamp/>
            <mkdir dir="${dir.build}"/>
        </target>
    
        <target name="jar" depends="init">
            <!-- ${ant.file} is the name of the current build file -->
            <subant genericantfile="${ant.file}" target="do-jar">
    
                <!-- Pass the needed properties to the subant call. You could also use
                     the inheritall attribute on the subant element above to pass all
                     properties. -->
                <propertyset>
                    <propertyref name="dir.build"/>
                </propertyset>
    
                <!-- subant will call the "do-jar" target for every directory in the
                     ${dir.root} directory, making the subdirectory the basedir. -->
                <dirset dir="${dir.root}" includes="*"/>
            </subant>
        </target>
    
        <target name="do-jar">
            <!-- Get the basename of the basedir (bar1, bar2, etc.) -->
            <basename file="${basedir}" property="jarname"/>
    
            <jar jarfile="${dir.build}/${jarname}.jar" basedir="${basedir}"/>
        </target>
    </project>
    

    【讨论】:

      【解决方案2】:

      您可以使用适当的Jar task,以及foreach ant-contrib 扩展任务。

      一个样本:

      <foreach list="${hmProjectsList}" delimiter="/," 
               target="cvsCheckOut" param="hmProjectDir" inheritall="true" />
      

      【讨论】:

      • Jar 输入多个文件,单个输出文件。我需要多个输出文件。如果你知道怎么做,你能举个例子吗?
      • AFAIK jar 任务可以将 basedir 作为输入并将其转换为目标文件 - 这似乎是您所需要的。
      • 实际上,我想要类似: 无论 'mydir' 中包含什么目录,它们都是 jared。 n 个目录产生 n 个 .jar 文件。
      猜你喜欢
      • 1970-01-01
      • 2020-03-22
      • 1970-01-01
      • 2011-02-19
      • 2013-09-24
      • 2012-10-08
      • 1970-01-01
      • 2012-02-15
      • 1970-01-01
      相关资源
      最近更新 更多