In nant, how to delete contents of a directory, but not the directory itself? - nant

Suppose I were scripting a deployment using nant on a Windows server to a file share: \\server\share. I want a nant script to delete all files from the share then copy in new files.
I have this code to delete the files, but I'm getting an error that it can't delete "\server\share". But I didn't want to delete the share, just the contents in it.
<delete>
<fileset basedir="\\server\share">
<include name="**/**" />
</fileset>
</delete>
Output:
BUILD FAILED
D:\code\xxx\xxx.deploy(177,8):
Cannot delete directory '\\server\share'.
Access to the path '\\server\share' is denied.
If I modified it to instead delete contents of a directory in the share, say \\server\share\somedir, it'll delete "somedir" without error. But still, I didn't want to delete the dir, just the contents. Is there a way?

This works for me - no workarounds required:
<delete>
<fileset basedir="\\server\share">
<include name="**\*" />
</fileset>
</delete>

You could introduce an "exclude" tag and exclude a dummy file. That'll leave the root folder intact.
I'm using the following:
<target name="clean">
<delete>
<fileset basedir="${DeployTo}">
<include name="**/*" />
<exclude name="**/aspnet_client/**" />
</fileset>
</delete>
</target>

Taking cue from nsr81, I was able to come up with this workaround that works for me:
<touch file="${DeployTo}/deleteme" />
<delete>
<fileset basedir="${DeployTo}">
<include name="**/**" />
<exclude name="deleteme" />
</fileset>
</delete>
<delete file="${DeployTo}/deleteme" />

Related

Add Xom to managed URI In res

I am Working on ILOG.Currently I am using an Ant script for deploying ruleapp to res.My Problem is when I am deploying it is working fine but I want to add xom automatically to " ADD Managed URI" tab present in res. xom is getting attached to ruleapp but i want to get it attached to managed uri section.
Thanks in advance.
Have you tried this way - Got it from IBM Help.I have not tried it, but looks like, it should work. ( IBM ODM.8.0.1)
<target name="deployruleappwithxom">
<res-deploy hostname="${hostname}" portnumber="${portnumber}" webapp="${webapp}" userid="${userid}" password="${password}" file="my-ruleapp.jar">
<xompath rulesetpath="/MyRuleApp/MyRuleset">
<fileset dir="${lib}">
<include name="**/myxom.jar" />
</fileset>
</xompath>
<xompath rulesetpath="/AnotherRuleApp/AnotherRuleset">
<fileset dir="${otherlib}">
<include name="**/otherxom.jar" />
</fileset>
</xompath>
</res-deploy>
</target>
if you just looking for managing xomuri property, you can try this.
<target name="runloanvalidation">
<res-deploy-xom
hostname="localhost"
portnumber="9080"
webapp="res"
userid="resAdmin"
password="resAdmin"
jarMajorVersion="false"
libName="person"
outputRulesetProperty="ruleset.managedxom.uris">
<xompath>
<fileset dir="hello">
<include name="*.jar"/>
</fileset>
</xompath>
</res-deploy-xom>
<echo message="Resulting property: ${ruleset.managedxom.uris}"/>
</target>
And for more information, you can visit this.
http://www-01.ibm.com/support/knowledgecenter/#!/SSQP76_8.6.0/com.ibm.odm.dserver.rules.ref.res/ant_tasks/con_ant_res_deploy_xom.html

Ant search in text file some text expression

Ant search in text file some text expression, if it NOT found I try to delete file that txt was. So my code in Ant is down, but I don't know how put NOT correctly
<target name="ifDelete">
<if>
<length when="greater" length="0">
<fileset file="MyText.txt">
<!--containsregexp negate="true"-->
<containsregexp expression="findME" />
<!--/containsregexp-->
</fileset>
</length>
<then>
<delete includeemptydirs="true">
<fileset dir="FileIgonnaDelete">
</fileset>
</delete>
</then>
</if>
</target>
To search and delete - means seek and destroy - files in a fileset that do not contain a specific text, use something like :
<delete verbose="true">
<fileset dir="rootdir/to/search" includes="**/*.txt">
<not>
<contains text="findMe"/>
</not>
</fileset>
</delete>
you may use one or more nested resource collections (fileset, dirset ...) within delete.
see Ant manual delete, fileset and selectors for details.

Moving from NAnt to PowerShell. Copy/Delete replacement

Please help me to translate this to PowerShell:
<delete>
<fileset defaultexcludes="false" basedir="${svnRepository}">
<include name="**/*" />
<exclude name="**/.svn" />
<exclude name="**/.svn/**" />
</fileset>
</delete>
<!-- Copy entire output of successful build into the svn working folder -->
<copy todir="${svnRepository}">
<fileset defaultexcludes="false" basedir="${binariesFolder}">
<include name="**/*" />
<exclude name="**/Bin/*.pdb"/>
<exclude name="**/Bin/*.xml"/>
</fileset>
</copy>
PowerShell
Get-ChildItem -recurse "$svnRepository" | ? {$_.FullName -notmatch '.svn'} | Remove-Item -Recurse
I've tried to write something like above, but it doesn't exclude all SVN directories in nested foldes, e.g if I have a directory C:\blabla\test with nested .svn it will ignore C:\blabla\test\.svn\* but at the same time it will try to remove C:\blabla\test itself
Are you sure you have to do anything here? Get-ChildItem already doesn't include hidden folders like .svn, .git etc. and only includes them when you specify the -Force parameter.

How to copy modified files to several servers using ANT

This script works good for the first server (server1), but it doesn't work for the second pass (server2) as at that point all 'modified' files are already flagged by the first pass.
<macrodef name="copythings">
<attribute name="todir"/>
<sequential>
<scp todir="#{todir}" trust="true">
<fileset dir=".">
<modified/>
<include name="cgi-bin/Application/" />
<exclude name="**/*.log" />
</fileset>
</scp>
</sequential>
</macrodef>
<target name="deploy">
<copythings todir="server1"/>
<copythings todir="server2"/>
</target>
I'd suggest copying the files from the fileset to a temporary folder first.
And copy to the server from that directory.
copy required files to a tempory folder
run the targets and use the temporay folder as base/filset for scp
delete the temporary folder

nAnt Deleting files older than 7 days old

I would like to create a target that cleans log files older than 7 days old in a specific folder. I get an error when I try to put in a "date" element inside a fileset. How can I go about this?
<delete>
fileset basedir="${StageIISRoot}/MySite/App_Data/ErrorLog">
<date datetime="${datetime::now() - timespan::from-days(7)}" when="before"/>
<include name="*.xml" />
</fileset>
</delete>
I don't see any documentation for using the "date" element. You might consider something like this:
<fileset id="thelogs" basedir="${StageIISRoot}/MySite/App_Data/ErrorLog">
<include name="*.xml" />
</fileset>
And then reference that fileset later in a loop that checks the file date and deletes:
<foreach item="File" property="filename">
<in>
<items refid="thelogs" />
</in>
<do>
<if test="${timespan::get-days(datetime::now() - file::get-last-write-time(filename)) >= 7}">
<delete file="${filename}" />
</if>
</do>
</foreach>
What about something like:
<tstamp>
<format property="last.week" pattern="MM/dd/yyyy hh:mm" locale="en,UK" offset="-7" unit="day"/>
</tstamp>
<echo>Delete backups before ${last.week}</echo>
<delete>
<fileset dir="${dst.dir}">
<date datetime="${last.week}" when="before"/>
</fileset>
</delete>
It seems to work for me :-)