如果我知道使用 ANT 的文件夹名称不完整,如何将文件复制到文件夹
How to copy file to folder if I know incomplete name of that folder using ANT
下面是代码片段
<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,因此文件夹名称可能 vary.It 将是 apache-tomcat-something。我该如何指定?我希望我的 ant 文件查找以 apache-tomcat-*/webapps 开头的文件夹,并将该文件复制到该文件夹下的 webapps 中。
我添加了 * 但是它创建了一个新文件夹而不是找到具有相似名称的文件夹。
感谢任何帮助!
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" />
解释:
- 使用
dirset
类型收集位于 ../..
中遵循模式 "apache-tomcat-*" 的目录。这将存储为 ID 为 "tomcat.dir" 的 Ant path
。 (随意将这些值重命名为 "apache" 或其他名称;这只是我的偏好,因为 Apache 生产许多不同的产品。)
- 由于
dirset
可能会收集多个目录,如果发生这种情况,您可能希望构建失败。否则,您稍后会在脚本中遇到令人困惑的错误。
- 同样,如果找不到目录,您可能希望构建失败。如果
dirset
类型什么也没找到,它自己的构建不会失败。
- 使用
pathconvert
任务从 tomcat.dir
路径创建一个 属性。我给了他们相同的名字,但这并不需要如此。
- 使用
property
任务专门为您的目标目录创建一个 属性。请注意使用 location
属性代替 value
属性。这将导致 属性 值被解析为具有适合用户 OS 文件分隔符的规范路径(即,如果用户在 Windows 上,对于 war d 斜杠将转换为反斜杠)。
- 复制到上面定义的目录。我假设您想从 war 文件中删除任何父目录,因此我包含了
flatten="true"
属性,但如果不是这种情况,请继续删除该部分。
下面是代码片段
<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,因此文件夹名称可能 vary.It 将是 apache-tomcat-something。我该如何指定?我希望我的 ant 文件查找以 apache-tomcat-*/webapps 开头的文件夹,并将该文件复制到该文件夹下的 webapps 中。
我添加了 * 但是它创建了一个新文件夹而不是找到具有相似名称的文件夹。
感谢任何帮助!
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" />
解释:
- 使用
dirset
类型收集位于../..
中遵循模式 "apache-tomcat-*" 的目录。这将存储为 ID 为 "tomcat.dir" 的 Antpath
。 (随意将这些值重命名为 "apache" 或其他名称;这只是我的偏好,因为 Apache 生产许多不同的产品。) - 由于
dirset
可能会收集多个目录,如果发生这种情况,您可能希望构建失败。否则,您稍后会在脚本中遇到令人困惑的错误。 - 同样,如果找不到目录,您可能希望构建失败。如果
dirset
类型什么也没找到,它自己的构建不会失败。 - 使用
pathconvert
任务从tomcat.dir
路径创建一个 属性。我给了他们相同的名字,但这并不需要如此。 - 使用
property
任务专门为您的目标目录创建一个 属性。请注意使用location
属性代替value
属性。这将导致 属性 值被解析为具有适合用户 OS 文件分隔符的规范路径(即,如果用户在 Windows 上,对于 war d 斜杠将转换为反斜杠)。 - 复制到上面定义的目录。我假设您想从 war 文件中删除任何父目录,因此我包含了
flatten="true"
属性,但如果不是这种情况,请继续删除该部分。