0

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

3 Answers3

4

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.

Lothar
  • 860
  • 6
  • 21
1

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>

Community
  • 1
  • 1
aked
  • 5,625
  • 2
  • 28
  • 33
1

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.

Morgan
  • 486
  • 2
  • 3