【问题标题】:Ant unzip/unwar with the same directory name as the file nameAnt unzip/unwar 与文件名同目录名
【发布时间】:2012-08-28 21:20:39
【问题描述】:

我需要使用 ANT 构建脚本在 tomcat/webapps 目录中解压缩一个 war 文件。战争文件名不固定。如何将其解压缩到名称与war文件名相同的目录中。我知道如何解压缩文件,但问题是它解压缩了指定目标目录中的内容。如果我不知道目录名称怎么办?

构建之前:

tomcat/webapps/
   myApp-0.1.war

构建之后:

tomcat/webapps
   myApp-0.1/
   myApp-0.1.war

【问题讨论】:

  • 目录名不应该是myApp-0.1还是应该全部小写?

标签: ant


【解决方案1】:

所以在了解了一些 Ant 任务之后,我想到了:

<!-- Get the path of the war file. I know the file name pattern in this case -->
<path id="warFilePath">
    <fileset dir="./tomcat/webapps/">
        <include name="myApp-*.war"/>
    </fileset>
</path>

<property name="warFile" refid="warFilePath" />

<!-- Get file name without extension -->
<basename property="warFilename" file="${warFile}" suffix=".war" />

<!-- Create directory with the same name as the war file name -->
<mkdir dir="./tomcat/webapps/${warFilename}" />

<!-- unzip war file -->
<unwar dest="./tomcat/webapps/${warFilename}">
    <fileset dir="./tomcat/webapps/">
        <include name="${warFilename}.war"/>    
    </fileset>
</unwar>

如果有更好的方法,请告诉我。我还使用 ant-contrib 在 stackoverflow 上找到了a solution,但这不是我想要的。

【讨论】:

    【解决方案2】:

    干得好bluetech。您的解决方案也可以表示如下:

    <target name="unwar-test">
      <property name="webapps.dir" value="tomcat/webapps" />
    
      <fileset id="war.file.id" dir="${basedir}"
          includes="${webapps.dir}/myApp-*.war" />
      <property name="war.file" refid="war.file.id" />
    
      <basename property="war.basename" file="${war.file}" suffix=".war" />
      <property name="unwar.dir" location="${webapps.dir}/${war.basename}" />
      <mkdir dir="${unwar.dir}" />
      <unwar dest="${unwar.dir}" src="${war.file}" />
    </target>
    

    【讨论】:

    • 如果有多个文件与包含模式匹配,则此方法不起作用
    猜你喜欢
    • 2012-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-02
    • 1970-01-01
    相关资源
    最近更新 更多