Nant“nant.onsuccess”属性

时间:2009-10-21 06:47:32

标签: nant

我有一个nant文件来构建我的项目。一旦构建成功,我将使用nant.onsuccess属性发送邮件。在这个nant.onsuccess中,我将调用下一批目标进行构建。但我需要发送邮件,具体取决于从nant.onsuccess目标调用的这些目标集的成功或失败。

例如:

     <?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.

请让我知道如何实现它。

谢谢, 普里亚

3 个答案:

答案 0 :(得分:4)

一旦nant完成构建,它将执行 nant.onsuccess nant.onfailure 指定的目标。这只发生一次,因此如果您更改 nant.onsucces / nant.onfailure 属性,它将无效。

正如其他海报所述,用于实现条件逻辑目标依赖关系,&lt; if&gt;,&lt; trycatch&gt;,&lt; choose&gt; &lt; nant&gt;和&lt; call&gt; 任务以及 if / unless 属性更适合。

答案 1 :(得分:1)

首先要了解的是目标依赖项如何控制执行。您可以通过使用依赖项来完成所需的大量工作。阅读NAnt基础page on targets

接下来,如果您必须根据特定任务是否失败而执行大量逻辑操作,则可以查看&lt; trycatch&gt; NAntContrib项目中的任务,它为NAnt添加了许多有用的任务。 trycatch任务比默认的nant.onfailure具有更大的灵活性。

答案 2 :(得分:1)

我不确定,如果这会有所帮助。请看下面的链接。

我已经整理了一个示例构建文件。

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>