【问题标题】:How to copy file to folder if I know incomplete name of that folder using ANT如果我知道使用 ANT 的文件夹名称不完整,如何将文件复制到文件夹
【发布时间】:2018-06-14 10:02:27
【问题描述】:

下面是代码sn-p

<property name="apache.dst.dir" value="../../apache-tomcat-7.0.79/webapps" />

<copy todir="${apache.dst.dir}">
    <fileset dir="${dstdir}">
        <include name="api.war" />
    </fileset>
</copy>

我正在尝试将 war 文件复制到 apache-tomcat 下的 webapps 目录。但是不同的用户可能有不同版本的tomcat,因此文件夹名称可能会有所不同。它将是apache-tomcat-something。我该如何指定?我希望我的 ant 文件查找以 apache-tomcat-*/webapps 开头的文件夹,并将该文件复制到该文件夹​​下的 webapps 中。

我添加了 * 但是它会创建一个新文件夹,而不是查找具有相似名称的文件夹。

感谢任何帮助!

【问题讨论】:

    标签: tomcat build ant terminal


    【解决方案1】:

    Ant 的property 任务不适用于通配符,因此您必须使用资源集合来查找所需的目录。以下是我推荐的做法:

    <dirset id="tomcat.dir" dir="../.." includes="apache-tomcat-*" />
    
    <fail message="Multiple Tomcat directories found in ${tomcat.parent.dir}.${line.separator}${toString:tomcat.dir}">
        <condition>
            <resourcecount refid="tomcat.dir" when="greater" count="1" />
        </condition>
    </fail>
    
    <fail message="No Tomcat directory found in ${tomcat.parent.dir}.">
        <condition>
            <resourcecount refid="tomcat.dir" when="less" count="1" />
        </condition>
    </fail>
    
    <pathconvert refid="tomcat.dir" property="tomcat.dir" />
    
    <property name="tomcat.webapps.dir" location="${tomcat.dir}/webapps" />
    
    <copy todir="${tomcat.webapps.dir}" file="${dstdir}/api.war" flatten="true" />
    

    解释:

    1. 使用dirset 类型来收集位于../.. 中遵循“apache-tomcat-*”模式的目录。这将存储为 ID 为“tomcat.dir”的 Ant path。 (随意将这些值重命名为“apache”或其他名称;这只是我的偏好,因为 Apache 生产了许多不同的产品。)
    2. 由于dirset 可能收集多个目录,如果发生这种情况,您可能希望构建失败。否则你会在后面的脚本中遇到令人困惑的错误。
    3. 同样,如果找不到目录,您可能希望构建失败。 dirset 类型如果找不到任何内容,它本身不会使构建失败。
    4. 使用pathconvert 任务从tomcat.dir 路径创建属性。我给他们取了相同的名字,但不必如此。
    5. 使用property 任务专门为您的目标目录创建一个属性。请注意使用location 属性代替value 属性。这将导致属性值被解析为带有适合用户操作系统的文件分隔符的规范路径(即,如果用户在 Windows 上,正斜杠将被转换为反斜杠)。
    6. 复制到上面定义的目录。我假设您想从您的 war 文件中删除任何父目录,因此我包含了 flatten="true" 属性,但如果不是这种情况,请继续删除该部分。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-04
      • 2019-03-28
      • 2018-01-19
      • 1970-01-01
      • 1970-01-01
      • 2020-02-14
      • 2020-12-19
      相关资源
      最近更新 更多