How to print hidden files/folders list using NAnt script? - nant

Current I try as below but that will skip all hidden files/folders. I want to see them.
Please help if you know the trick! Thank you.
<foreach item="Folder" property="folderName">
<in>
<items>
<include name=".\**" />
</items>
</in>
<do>
<echo message="${folderName}" />
</do>
</foreach>

Are you sure they are skipped because they're hidden? I'm more inclined that the files not listed are in default excludes. You should try disabling these excludes and see if that helps:
<foreach item="Folder" property="folderName">
<in>
<items defaultexcludes="false">
<include name=".\**" />
</items>
</in>
<do>
<echo message="${folderName}" />
</do>
</foreach>

Related

how can I copy one file to multiple sub directories

I'm trying to use nant because I thought it would be the easiest, but i'm open to any solution that works on windows xp.
I have the following folder structure
basefolder
folder1
folder2
subfolder1
code
solutionname1
projectname.interface
projectname.simulation
projectname.testcase
bin
release
folder3
...
folderN
folder1 - folderN all have the same directory structure as folder2. I want to copy a file to the release folder in each folderN.
I currently have the following nant script
<?xml version="1.0" encoding="utf-8"?>
<project xmlns="http://nant.sf.net/release/0.90/nant.xsd" name="CopyDll" default="FileCopy">
<property name="fileToCopy"
value="C:\file.dll"
overwrite="false"/>
<property name="baseDirectory" value="${directory::get-current-directory()}" overwrite="false"/>
<target name="FileCopy"
description="Copies file to multiple directories">
<foreach item="Folder"
in="${baseDirectory}"
property="foldername">
<in>
<items>
<include name="**\**\**\*.TestCase\bin\Release"/>
</items>
</in>
<do>
<copy file="${fileToCopy}"
todir="${foldername}"/>
<echo message="Copied file to ${foldername}"/>
</do>
</foreach>
</target>
</project>
This copies file.dll to each folderN directory.
What am I doing wrong?
Is there a better way to do this?
I figured it out. I had to change my foreach to look like this
<foreach item="Folder"
property="foldername">
<in>
<items>
<include name="${baseDirectory}\**\*.TestCase\bin\Release"/>
</items>
</in>
<do>
<copy file="${fileToCopy}"
todir="${foldername}"/>
<echo message="Copied file to ${foldername}"/>
</do>
</foreach>

How to echo the contents of a fileset in Nant?

Was trying to do something like this:
<copy>
<fileset id="mySet">
<include name="*.sql" />
</fileset>
</copy>
<echo message="Copied files: ${mySet} to directory: ${Folder}." />
But i get the following error:
'id' is an invalid attribute for a
tag. Datatypes can only be
declared at Project or Target level.
Thanks
You can do this by looping over the files in the set.
<fileset id="mySet">
<include name="*.sql" />
</fileset>
<copy>
<fileset refid="mySet" />
</copy>
<foreach item="File" property="filename">
<in>
<items refid="mySet" />
</in>
<do>
<echo message="Copied files: ${filename} to directory: ${Folder}." />
</do>
</foreach>
But depending on verbosity level the result of the copy action is echoed anyway.

Nant "nant.onsuccess" property

I have a nant file which does the building of my project. Once the build succeeded, I will use the nant.onsuccess property to send mail. In this nant.onsuccess I will call next batch of targets to build. But I need to send the mail depending on the success or failure of these set of targets that are called from the nant.onsuccess target.
eg:
<?xml version="1.0" encoding="utf-8" ?>
<project name="Build.build" default="default">
<property name="mail.mailhost" value="x"/>
<property name="mail.from" value="y"/>
<property name="mail.to" value="z"/>
<target name="default" description="Just like that">
<echo message="Succeeded"/>
<echo message="Succeeded"/>
<property name="nant.onsuccess" value="suc"/>
</target>
<target name="suc" description="Just like that">
<echo message="I am called"/>
<echo message="in success part"/>
<property name="nant.onsuccess" value="here"/>
<call target="testing"/>
</target>
<target name="testing">
<echo message="I ammmmmmmmmm"/>
<property name="nant.onsuccess" value="here"/>
</target>
<target name="here">
<echo message="I should not be called"/>
</target>
<target name="nant.onfailure">
<if test="${string::get-length(mail.to) > 0}">
<mail mailhost="${mail.mailhost}" from="${mail.from}" tolist="${mail.to}"
subject="Test mail on ${environment::get-variable('COMPUTERNAME')}.">
Note: this is ignored.
</mail>
</if>
</target>
</project>
The target "here" should be called depending on whether the target "testing" is succeeded or not.
Please let me know how do i achieve it.
Thanks,
Priya
Once nant finished its build it will execute a target specified by nant.onsuccess or nant.onfailure. This happens only once, so if you change the nant.onsucces / nant.onfailure properties it will have no effect.
As other posters stated for implementing conditional logic target dependencies, <if>, <trycatch>,<choose> and the <nant> and <call> tasks together with the if / unless attributes are better suited.
First thing to understand is how target dependencies can control execution. You can probably do a lot of what you need just by using dependencies. Read through the NAnt fundamentals page on targets.
Next, if you have to do a lot of logic depending on whether a particular task fails, you might check out the <trycatch> task in the NAntContrib project, which adds many useful tasks to NAnt. The trycatch task allows for a lot more flexibility than the default nant.onfailure.
I am not sure , if this will help. please give a look at below link.
I have put together a sample build file.
https://stackoverflow.com/a/11365488/1060656
<description>Sample Build Scripts</description>
<property name="nant.onsuccess" value="success" />
<property name="nant.onfailure" value="failure" />
<property name="tolist" value="youremail#email.com" />
<property name="cclist" value="youremail#email.com" />
<property name="emailsubject" value="" />
<target name="build" depends="init">
YOUR ACTUAL BUILD CODE GOES HERE
</target>
<target name="init">
<echo>
-----------------------------------------------------------------------------------------------------------------
TASK : INITIALIZE
-----------------------------------------------------------------------------------------------------------------
</echo>
<loadtasks assembly="nantcontrib-0.85/bin/NAnt.Contrib.Tasks.dll" />
<!-- http://www.basilv.com/psd/blog/2007/how-to-add-logging-to-ant-builds -->
<tstamp>
<formatter property="timestamp" pattern="yyMMdd_HHmm"/>
</tstamp>
<property name="build.log.filename" value="build_${timestamp}.log"/>
<echo message="build.log.filename: ${build.log.filename}" />
<record name="${build.log.dir}/${build.log.filename}" action="Start" level="Verbose"/>
<echo message="Build logged to ${build.log.filename}"/>
<echo message="Build Start at: ${datetime::now()}" />
</target>
<!--http://www.mail-archive.com/nant-users#lists.sourceforge.net/msg02485.html-->
<target name="success" depends="successresult,sendemail">
<echo>${emailsubject}</echo>
</target>
<!--http://www.mail-archive.com/nant-users#lists.sourceforge.net/msg02485.html-->
<target name="failure" depends="failureresult,sendemail">
<echo>${emailsubject}</echo>
</target>
<target name="successresult" >
<echo>
BUILD FAILED . CHANGE SUBJECT
</echo>
<property name="emailsubject" value="Web Integration DEV Build : SUCCESS !!!" />
</target>
<target name="failureresult" >
<echo>
BUILD FAILED . CHANGE SUBJECT
</echo>
<echo message="Task Start at: ${datetime::now()}" />
<property name="emailsubject" value="Web Integration DEV Build : FAILED !!! :)" />
</target>
<target name="sendemail" >
<echo>
-----------------------------------------------------------------------------------------------------------------
SENDING EMAIL
-----------------------------------------------------------------------------------------------------------------
</echo>
<echo message="Task Start at: ${datetime::now()}" />
<echo>${emailsubject}</echo>
<echo>Sending Email</echo>
<echo>Attaching File : ${build.log.dir}/email_${build.log.filename}</echo>
<echo>Attaching File : ${path.vsshelper.log}/logs/email_${build.log.getlistoffiles}</echo>
<!-- Flush is very important before you copy -->
<record name="${build.log.dir}/${build.log.filename}" action="Flush" level="Verbose"/>
<sleep milliseconds="1000" />
<!-- make a copy -->
<copy file= "${build.log.dir}/${build.log.filename}" tofile="${build.log.dir}/email_${build.log.filename}" />
<mail
from="${email.from}"
tolist="${email.to}"
mailhost="${email.host}"
message="${emailsubject}"
subject="${emailsubject}"
>
<attachments>
<include name="${build.log.dir}/email_${build.log.filename}" />
<include name="${path.vsshelper.log}/logs/email_${build.log.getlistoffiles}" />
</attachments>
</mail>
</target>

nant exclude folder

I am looping though my folders and need to exclude the svn folders. I thought I could simply add the exclude element, though that doesnt seem to work.
<foreach item="Folder" property="foldername">
<in>
<items>
<include name="YOUR_FOLDER\**" />
<exlcude name="YOUR_FOLDER\**/_svn" />
</items>
</in>
<do>
<foreach item="File" property="filename" in="${foldername}">
<do>
<echo message="${filename}" />
</do>
</foreach>
</do>
</foreach>
Can somebody help?
It works when adding simply:
<exclude name="YOUR_FOLDER\**_svn**" />
You'll want to include the .svn directories and everything in them as well, otherwise the exclude mask will only hit the .svn directories, but not .svn/prop-base, .svn/props, .svn/text-base and so on.
<foreach item="Folder" property="foldername">
<in>
<items>
<include name="YOUR_FOLDER/**" />
<exclude name="YOUR_FOLDER/**/.svn/**" />
<exclude name="YOUR_FOLDER/**/_svn/**" />
</items>
</in>
<do>
<foreach item="File" property="filename" in="${foldername}">
<do>
<echo message="${filename}" />
</do>
</foreach>
</do>
</foreach>
Also, are you really using the alternative _svn naming scheme for Subversion's data directories?
<copy failonerror="true" overwrite="true" todir="${BusinessServicesTarget}">
<fileset basedir="${BusinessServicesPath}">
<exclude name="*.*" />
<include name="/**/*.as?x" />
<exclude name="/**/**_SVN**" />
You need to use it in copy function not in foreach.

nant mail issues

Can anyone please suggest me how we could configure the sending of mails through nant.I had even gone through the link but i was unsucessfull.
thanks and regards
maddy
Something like this is what you would use.
<target name="sendmail">
<mail
from=“NAnt#myCompany.com“
tolist=“${distribution_list}“
subject="${mailsubject}"
mailhost="${smtpServer}"
message="${mailbody}"
verbose="true"
>
<attachments>
<include name="${buildroot}/build.log" />
</attachments>
</mail>
</target>
Here is a sample build file. The solution is not very elegant but it works. All it does is logs a file and before sending email flush it and makes a copy of it to attach it in email.
Note: there are some help links in code which i used while writing my own build file.
<description>Sample Build Scripts</description>
<property name="nant.onsuccess" value="success" />
<property name="nant.onfailure" value="failure" />
<property name="tolist" value="youremail#email.com" />
<property name="cclist" value="youremail#email.com" />
<property name="emailsubject" value="" />
<target name="build" depends="init">
YOUR ACTUAL BUILD CODE GOES HERE
</target>
<target name="init">
<echo>
-----------------------------------------------------------------------------------------------------------------
TASK : INITIALIZE
-----------------------------------------------------------------------------------------------------------------
</echo>
<loadtasks assembly="nantcontrib-0.85/bin/NAnt.Contrib.Tasks.dll" />
<!-- http://www.basilv.com/psd/blog/2007/how-to-add-logging-to-ant-builds -->
<tstamp>
<formatter property="timestamp" pattern="yyMMdd_HHmm"/>
</tstamp>
<property name="build.log.filename" value="build_${timestamp}.log"/>
<echo message="build.log.filename: ${build.log.filename}" />
<record name="${build.log.dir}/${build.log.filename}" action="Start" level="Verbose"/>
<echo message="Build logged to ${build.log.filename}"/>
<echo message="Build Start at: ${datetime::now()}" />
</target>
<!--http://www.mail-archive.com/nant-users#lists.sourceforge.net/msg02485.html-->
<target name="success" depends="successresult,sendemail">
<echo>${emailsubject}</echo>
</target>
<!--http://www.mail-archive.com/nant-users#lists.sourceforge.net/msg02485.html-->
<target name="failure" depends="failureresult,sendemail">
<echo>${emailsubject}</echo>
</target>
<target name="successresult" >
<echo>
BUILD FAILED . CHANGE SUBJECT
</echo>
<property name="emailsubject" value="Web Integration DEV Build : SUCCESS !!!" />
</target>
<target name="failureresult" >
<echo>
BUILD FAILED . CHANGE SUBJECT
</echo>
<echo message="Task Start at: ${datetime::now()}" />
<property name="emailsubject" value="Web Integration DEV Build : FAILED !!! :)" />
</target>
<target name="sendemail" >
<echo>
-----------------------------------------------------------------------------------------------------------------
SENDING EMAIL
-----------------------------------------------------------------------------------------------------------------
</echo>
<echo message="Task Start at: ${datetime::now()}" />
<echo>${emailsubject}</echo>
<echo>Sending Email</echo>
<echo>Attaching File : ${build.log.dir}/email_${build.log.filename}</echo>
<echo>Attaching File : ${path.vsshelper.log}/logs/email_${build.log.getlistoffiles}</echo>
<!-- Flush is very important before you copy -->
<record name="${build.log.dir}/${build.log.filename}" action="Flush" level="Verbose"/>
<sleep milliseconds="1000" />
<!-- make a copy -->
<copy file= "${build.log.dir}/${build.log.filename}" tofile="${build.log.dir}/email_${build.log.filename}" />
<mail
from="${email.from}"
tolist="${email.to}"
mailhost="${email.host}"
message="${emailsubject}"
subject="${emailsubject}"
>
<attachments>
<include name="${build.log.dir}/email_${build.log.filename}" />
<include name="${path.vsshelper.log}/logs/email_${build.log.getlistoffiles}" />
</attachments>
</mail>
</target>