Delete files specified via a zipfileset? - ant

I have the following Ant target which extracts contents from a specific .ZIP archive:
<!-- UNPACK-MATH -->
<target name="unpack-math" depends="init-contrib">
<!-- NOTE: the 'unzip' task doesn't fail when it cannot extract over read-only files; however, 'copy' with a 'zipfileset' does. -->
<first id="math.archive">
<fileset dir="${builddir}" includes="MATH_MF*.zip" />
</first>
<if>
<length string="${toString:math.archive}" when="greater" length="0" />
<then>
<copy todir="${basedir}">
<zipfileset src="${toString:math.archive}" />
</copy>
</then>
<else>
<echo message="No math to unpack." />
</else>
</if>
</target>
What I'd like to do now is "clean up" the files that were extracted. However, the following does not work:
<!-- CLEAN-MATH -->
<target name="clean-math" depends="init-contrib">
<first id="math.archive">
<fileset dir="${builddir}" includes="MATH_MF*.zip" />
</first>
<if>
<length string="${toString:math.archive}" when="greater" length="0" />
<then>
<delete>
<zipfileset src="${toString:math.archive}" />
</delete>
</then>
<else>
<echo message="No math to clean." />
</else>
</if>
</target>
I get the following stack trace:
BUILD FAILED
D:\Development\MForce\Games\gamebuild.xml:214: java.lang.ClassCastException: class org.apache.tools.ant.types.resources.ZipResource doesn't provide files
at org.apache.tools.ant.types.resources.comparators.FileSystem.resourceCompare(FileSystem.java:43)
...
Any ideas?

This solution appears to work, but requires unpacking the .ZIP archive (which lists the files you'd like to delete as some other root) first, which I'd prefer to avoid:
<!-- CLEAN-MATH -->
<target name="clean-math" depends="init-contrib">
<first id="math.archive">
<fileset dir="${builddir}" includes="MATH_MF*.zip" />
</first>
<if>
<length string="${toString:math.archive}" when="greater" length="0" />
<then>
<unzip dest="${builddir}/tmp" src="${toString:math.archive}"/>
<delete>
<fileset dir="${basedir}" includes="**/*">
<present present="both" targetdir="${builddir}/tmp"/>
</fileset>
</delete>
<delete dir="${builddir}/tmp"/>
</then>
<else>
<echo message="No math to clean." />
</else>
</if>
</target>

Related

using if with resourcecontains in and is not returning true value

I'm reading a file that contains a list of files path.
For each file, I would like to know if it contains a substring.
the answer is always false, although part of it should be true.
Here is my target:
<target name="chek-file">
<loadfile property="file" srcfile="c:\tmp\testing.txt"/>
<for param="line" list="${file}" delimiter="${line.separator}">
<sequential>
<echo>#{line}</echo>
<loadfile property="inner_file" srcfile="#{line}"/>
<if>
<resourcecontains resource="${inner_file}" substring="parent" />
<then>
<echo message="this is a jpa jar"/>
</then>
<else>
<echo message="this is NOT a jpa jar"/>
</else>
</if>
</sequential>
</for>
</target>
the echo is typing "this is NOT a jpa jar" for all jars.
Is 'if' not working with 'resourcecontains'?
OK, found two problems:
1) The second load file is actually not needed here because resourcecontains needs to get the file name and not its value.
2) resourcecontains is inherited from <condition>
so the solution should be:
<target name="chek-file">
<loadfile property="file" srcfile="c:\tmp\testing.txt"/>
<for param="line" list="${file}" delimiter="${line.separator}">
<sequential>
<echo>#{line}</echo>
<condition property="substring_found">
<resourcecontains resource="#{line}" substring="JPA>true" />
</condition>
<echo message="substring_found value: ${substring_found}"/>
<if>
<equals arg1="${substring_found}" arg2="true" />
<then>
<echo message="this is a jpa jar"/>
<get_jar_name_no_version property.to.process="#{line}" output.property="linetobeadd" />
</then>
<else>
<echo message="this is NOT a jpa jar"/>
</else>
</if>
<var name="substring_found" unset="true"/>
</sequential>
</for>
</target>

ant: condition command does not find pattern when pattern is a file path

I am using the following code with the idea of finding a file in a directory that is also part of a list in a file:
<loadfile property="ReportFileContent" srcFile="${ReportFile}"/>
<for param="file">
<path>
<fileset dir="${MainDir}" includes="**/**"/>
</path>
<sequential>
<basename file="#{file}" property="#{file}" />
<condition property="found-file${index2}">
<matches pattern="#{file}" string="${ReportFileContent}"/>
</condition>
<if>
<isset property="found-file${index2}"/>
<then>
<echo message=" Found file #{file}" level="warning" />
</then>
<else>
<echo message="Not Found file #{file}" level="warning" />
</else>
</if>
<math result="index2" operand1="${index2}" operation="+" operand2="1" datatype="int" />
</sequential>
</for>
The command is not working though as it is not finding the file that is available in ${ReportFileContent}.
The content of the ReportFileContent property is the following:
c:\___tools\test\file1.txt;2
c:\___tools\test\file2.txt;2
c:\___tools\test\file3.txt;2
Any idea why the condition is not working correctly?
Thanks
Tony

ant, copy file with it's directory structure

I am trying to write a ant script to automate the update process of our web application.
When some files is going to be updated, I need to backup that file. my question is how to copy that file to backup directory and also create the directory structure relative to the root directory of my web application?
for example:
${WEB_APP_ROOT}/dir1/file1
${WEB_APP_ROOT}/dir2/subdir1/file2
copied to backup folder should be:
${BACK_UP_DIR}/dir1/file1
${BACK_UP_DIR}/dir2/subdir1/file2
currently, I can only copy all files to backup folder, but if two file with same name but located in different folder will cause problem.
my ant code:
<?xml version="1.0" encoding="UTF-8" ?>
<!-- 对公信贷自动更新Ant任务脚本 created by ggfan#bgzchina.com at 2013.11.14 -->
<project default="patch" basedir=".">
<!-- 引入Weblogic安装目录下的antcontrib包,才能使用if,foreach,propertyregex-->
<taskdef resource="net/sf/antcontrib/antlib.xml">
<classpath>
<pathelement location="/home/weblogic/Oracle/Middleware/modules/net.sf.antcontrib_1.1.0.0_1-0b2/lib/ant-contrib.jar" />
</classpath>
</taskdef>
<!-- 发布目标路径 -->
<property name="target_dir" value="/home/weblogic/amarsoft/ccms/war/CCMS" />
<!-- 数据库连接配置属性 -->
<property name="db_driver" value="oracle.jdbc.OracleDriver" />
<property name="db_url" value="jdbc:oracle:thin:#10.53.1.116:1521:credit" />
<property name="db_user" value="als_sit" />
<property name="db_pswd" value="als_sit" />
<!-- WLST配置 -->
<property name="wl_target_server" value="AdminServer" />
<property name="wl_admin_url" value="t3://10.53.1.117:7001" />
<property name="wl_user" value="weblogic" />
<property name="wl_pswd" value="weblogic123" />
<property name="wl_app_name" value="CCMS" />
<target name="patch">
<!-- 检查是否存在WEB-INF目录,如果有则说明更新了配置文件或者JAVA类需要重新加载应用 -->
<available file="${patch_dir}/WebRoot/WEB-INF" type="dir" property="WEB-INF.present"/>
<!-- 检查是否存在数据库更新脚本 -->
<available file="${patch_dir}/update.sql" type="file" property="sql.present"/>
<!-- 创建备份目录 -->
<mkdir dir="${patch_dir}/backup" />
<!-- 针对单个文件,检查是否更新还是新增,如果是更新则要先备份 -->
<foreach target="move-to-backup" param="theFile">
<path>
<fileset dir="${patch_dir}/WebRoot" />
</path>
</foreach>
<!-- 提醒用户检查更新列表预览 -->
<input message="Is patching preview above correct?" validargs="y,n" addproperty="patch.continue" />
<!-- 用户确认无误则更新 -->
<if>
<equals arg1="${patch.continue}" arg2="y" />
<then>
<!-- 存在WEB-INF目录,则先停止应用 -->
<if>
<equals arg1="${WEB-INF.present}" arg2="true" />
<then>
<echo message="Directory [WEB-INF] found in patching dir, application will be stoped" />
<wldeploy action="stop" graceful="true" name="${wl_app_name}" user="${wl_user}" password="${wl_pswd}"
verbose="true" adminurl="${wl_admin_url}" targets="${wl_target_server}" />
</then>
</if>
<copy todir="${target_dir}" verbose="true">
<fileset dir="${patch_dir}/WebRoot/" />
</copy>
<!-- 存在数据库更新脚本则执行 -->
<if>
<equals arg1="${sql.present}" arg2="true" />
<then>
<sql driver="${db_driver}" url="${db_url}" userid="${db_user}" password="${db_pswd}">
<classpath>
<pathelement location="/home/weblogic/Oracle/Middleware/wlserver_10.3/server/lib/ojdbc6.jar" />
</classpath>
<transaction src="${patch_dir}/update.sql"/>
</sql>
</then>
</if>
<!-- 更新完成后,重启应用 -->
<if>
<equals arg1="${WEB-INF.present}" arg2="true" />
<then>
<echo message="Application will be started again." />
<wldeploy action="start" name="${wl_app_name}" user="${wl_user}" password="${wl_pswd}"
verbose="true" adminurl="${wl_admin_url}" targets="${wl_target_server}" />
</then>
</if>
<echo message="Patching done! " />
</then>
</if>
</target>
<target name="move-to-backup">
<propertyregex property="target.file" input="${theFile}" regexp=".+/${patch_dir}/WebRoot/(.+)" replace="${target_dir}/\1" casesensitive="true" />
<available file="${target.file}" type="file" property="target.file.exist" />
<if>
<equals arg1="${target.file.exist}" arg2="true" />
<then>
<echo message="[UPDATE] ${target.file}" />
<copy todir="${patch_dir}/backup" verbose="false">
<fileset file="${target.file}" />
</copy>
</then>
<else>
<echo message="[ADD ] ${target.file}" />
</else>
</if>
</target>
</project>
In your target move-to-backup you have this copy task:
<copy todir="${patch_dir}/backup" verbose="false">
<fileset file="${target.file}" />
</copy>
When you define a fileset as a single file, it uses the directory containing the file as the base directory for the fileset, and the path to the file is relative to that: simply the name of the file.
You can do something like this instead, so that the path to the file to be copied is relative to the root of your application:
<copy todir="${patch_dir}/backup" verbose="false">
<fileset dir="${WEB_APP_ROOT}">
<include name="${target.file}" />
</fileset>
</copy>
If your file was
${WEB_APP_ROOT}/x/y/z/file.txt
It would then be copied to
${patch_dir}/backup/x/y/z/file.txt

copy doesn't support the nested "if" element in Ant-contrib Nested Loop

I need to copy all dml.sql files to inside DB2_List.txt file if DML.sql file is present. But after executing this file i'm getting error like this:
copy doesn't support the nested "if" element.
Please let me know if you have any better idea about the nested loop in Ant.
<available file="DB/DML.sql" property="db.check.present"/>
<copy file="DB/DDL.sql" tofile="DB2/DB2_List.txt" >
<if>
<equals arg1="${db.check.present}" arg2="true"/>
<then>
<filterchain>
<concatfilter append="DB/DML.sql" />
<tokenfilter delimoutput="${line.separator}" />
</filterchain>
</then>
</if>
</copy>
It is possible to accomplish what you are after, you just have to approach it quite differently in Ant. Just note that you will need to utilize separate targets.
<target name="db.check">
<available file="DB/DML.sql" property="db.check.present"/>
</target>
<target name="db.copy" depends="db.check" if="db.check.present">
<copy file="DB/DDL.sql" tofile="DB2/DB2_List.txt" >
<filterchain>
<concatfilter append="DB/DML.sql" />
<tokenfilter delimoutput="${line.separator}" />
</filterchain>
</copy>
</target>
Take a look at Ant 1.9.1 which supports special if/unless attributes on tags. This might be possible:
<project name="mysterious.moe" basedir="." default="package"
xmlns:if="ant:if"
xmlns:unless="ant:unless"/>
<target name="db.copy">
<available file="DB/DML.sql" property="db.check.present"/>
<copy file="DB/DDL.sql"
tofile="DB2/DB2_List.txt">
<filterchain if:true="db.ceck.present">
<concatfilter append="DB/DML.sql" />
<tokenfilter delimoutput="${line.separator}" />
</filterchain>
</copy>
<target>
...
</project>
Otherwise, you'll have to use two separate copies. You can't put <if> antcontrib inside tasks. Only around tasks:
<available file="DB/DML.sql" property="db.check.present"/>
<if>
<equals arg1="${db.check.present}" arg2="true"/>
<then>
<copy file="DB/DDL.sql" tofile="DB2/DB2_List.txt" >
<filterchain>
<concatfilter append="DB/DML.sql" />
<tokenfilter delimoutput="${line.separator}" />
</filterchain>
</copy>
</then>
<else>
<copy file="DB/DDL.sql" tofile="DB2/DB2_List.txt" >
</else>
</if>
</copy>

ant-contrib - if/then/else task

I am using ant, and I have a problem with if/then/else task, (ant-contrib-1.0b3.jar).
I am running something that can be simplified with build.xml below.
I am expecting to obtain from 'ant -Dgiv=Luke' the message
input name: Luke
should be overwritten with John except for Mark: John
but it seems property "giv" is not overwritten inside if/then/else..
input name: Luke
should be overwritten with John except for Mark: Luke
Is it depending from the fact I am using equals task with ${giv} ?
Otherwise what is wrong in my code?
build.xml CODE:
<project name="Friend" default="ifthen" basedir=".">
<property name="runningLocation" location="" />
<taskdef resource="net/sf/antcontrib/antcontrib.properties">
<classpath>
<pathelement location="${runningLocation}/antlib/ant-contrib-1.0b3.jar" />
</classpath>
</taskdef>
<target name="ifthen">
<echo message="input name: ${giv}" />
<if>
<equals arg1="${giv}" arg2="Mark" />
<then>
</then>
<else>
<property name="giv" value="John" />
</else>
</if>
<echo message="should be overwritten with John except for Mark: ${giv}" />
</target>
</project>
In Ant a property is always set once, after that variable is not alterable anymore.
Here follows a solution using standard Ant (without ant-contrib) which could be useful for the people who does not want an extra dependency.
<target name="test" >
<echo message="input name: ${param}" />
<condition property="cond" >
<equals arg1="${param}" arg2="Mark" />
</condition>
</target>
<target name="init" depends="test" if="cond">
<property name="param2" value="Mark" />
</target>
<target name="finalize" depends="init">
<property name="param2" value="John" />
<echo message="should be overwritten with John except for Mark: ${param2}" />
</target>
Ant Properties are very hard to overwrite (if not impossible). What you need is a Variable. These are also defined in the Ant Contrib JAR.
Editing your example:
<target name="ifthen">
<var name="Evangelist" value="${giv}" />
<echo message="input name: ${Evangelist}" />
<if>
<equals arg1="${Evangelist}" arg2="Mark" />
<then>
</then>
<else>
<var name="Evangelist" value="John" />
</else>
</if>
<echo message="should be overwritten with John except for Mark: ${Evangelist}" />
</target>
<project name="Friend" default="ifthen" basedir=".">
<property name="runningLocation" location="" />
<taskdef resource="net/sf/antcontrib/antcontrib.properties">
<classpath>
<pathelement location="${runningLocation}/antlib/ant-contrib-1.0b3.jar" />
</classpath>
</taskdef>
<target name="ifthen">
<echo message="input name: ${giv}" />
<if>
<equals arg1="${giv}" arg2="Mark" />
<then>
</then>
<else>
<var name="giv" unset="true"/>
<property name="giv" value="John" />
</else>
</if>
<echo message="should be overwritten with John except for Mark: ${giv}" />
</target>
</project>
We can use var task to unset the property also.
I know this is old, but should prove handy to others searching for a solution.
to re-assign a property without using ant-contrib, use macrodef with a script.
<macrodef name="property-change">
<attribute name="name"/>
<attribute name="value"/>
<sequential>
<script language="javascript"><![CDATA[
project.setProperty("#{name}", "#{value}");
]]></script>
</sequential>
</macrodef>
then in anywhere in ant, just call this like the property tag
<property-change name="giv" value="John"/>
to Implement this in your original version of xml, it would look like this:
<project name="Friend" default="ifthen" basedir=".">
<property name="runningLocation" location="" />
<taskdef resource="net/sf/antcontrib/antcontrib.properties">
<classpath>
<pathelement location="${runningLocation}/antlib/ant-contrib-1.0b3.jar" />
</classpath>
</taskdef>
<target name="ifthen">
<echo message="input name: ${giv}" />
<if>
<equals arg1="${giv}" arg2="Mark" />
<then>
</then>
<else>
<property-change name="giv" value="John" />
</else>
</if>
<echo message="should be overwritten with John except for Mark: ${giv}" />
</target>
</project>
This sample is given purely as an example on writing a macro to replace the <var> command in ant-contrib. In a situation like this one, where the <if> command is being used, it makes more sense to use <var> sinnce ant-contrib is already loaded, and <var> might be faster in processing.
Hope this helps.
It is possible to re-assign the value of a property using the ant-contrib 'propertycopy'. This is an alternative to using ant-contrib Variables.
This way the property "giv" can be overwritten.
<target name="ifthen">
<echo message="input name: ${giv}" />
<if>
<equals arg1="${giv}" arg2="Mark" />
<then>
</then>
<else>
<property name="tempName" value="John" />
<propertycopy name="giv" from="tempName" override="true" />
</else>
</if>
<echo message="should be overwritten with John except for Mark: ${giv}" />
</target>
Be aware this assumes the property tempName is not already set to a value other than 'John'.

Resources