简体   繁体   中英

Copy file in Ant to dynamic directory name

I need to copy a file using ant from one folder to the other. I know the exact name of the src file but for the destination file it's unknown since it's a version based folder I used the following script but it didn't work (please notice the * in the todir attribute:

<copy todir="${env.WORKSPACE}/my_dist_folder*" >
    <fileset dir="${env.WORKSPACE}/my_src_folder">
         <exclude name="**/*.svn"/>
    </fileset>
</copy>

Please Advice! Thanks

assuming there may be multiple my_dist_folder-xx.. folders:

<!-- goups all directories of the form "my_dist_folder-xx" -->
<dirset id="dir_list" dir="${env.WORKSPACE}">
  <include name="my_dist_folder*"/>
</dirset>
<!-- generate path names of the above dirs and sort them -->
<pathconvert property="dir_names_list" refid="dir_list" pathsep=","/>
<sortlist property="sorted_names_list" value="${dir_names_list}" delimiter="," />
<!-- pick the last path-name (as it corresponds with the latest version-number directory ) -->
<propertyregex property="dist_folder" input="${sorted_names_list}" regexp=",?([^,]+)$" select="\1"/>
<!-- use this "dist_folder" as "todir" in the copy task -->
<copy todir="${dist_folder}" >
    <fileset dir="${env.WORKSPACE}/my_src_folder">
         <exclude name="**/*.svn"/>
    </fileset>
</copy>  

although, from your question, it appears that all such my_dist_folder-xx.. folders would be directly under the basedir ${env.WORKSPACE} ... in case they may be under sub-directories within the basedir, then replace the line <include name="my_dist_folder*"/> with <include name="**/my_dist_folder*"/> in the <dirset> task.

UPDATE : also, if you're sure that there'll always be only one such folder my_dist_folder-xx.. on the system, then you may remove the following two lines- <sortlist> and <propertyregexp> , because the <dirset> will generate only one path-name and therefore you won't need to sort/pick-the-last-one. so you may use the <pathconvert> property to directly set the destination folder . eg:

<dirset id="dir_list" dir="${env.WORKSPACE}">
  <include name="my_dist_folder*"/>
</dirset>
<!-- generate path name of the above dir -->
<pathconvert property="dir_names_list" refid="dir_list" pathsep=","/>   
<!-- use this "dir_names_list" as "todir" in the copy task -->
<copy todir="${dir_names_list}" >
    <fileset dir="${env.WORKSPACE}/my_src_folder">
         <exclude name="**/*.svn"/>
    </fileset>
</copy> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM