Nant <script>任务似乎无法正常工作</script>

时间:2012-08-17 14:54:12

标签: c# nant

我有一个简单的nant构建文件,我想删除某个目录中的文件和另一个目录中的目录:

    <?xml version="1.0" ?>
    <project name="maintenance">
      <include buildfile="common.properties" />
    <target name="clean">  
       <script language="C#">              
              <imports>
                 <import namespace="System.Diagnostics" />
              </imports>
    <code>
         <![CDATA[               

   public static void DeleteUnusedFilesAndFolders(Project project) {

    var screensDirectory = new DirectoryInfo(@"${failed.screens.dir}");
    var testsResultsDirectory = new DirectoryInfo(@"${results.dir}");
    var _7daysAgoDate = DateTime.Now.AddDays(-7);
    foreach (var file in screensDirectory.GetFiles())
     {                
         if (file.CreationTime.CompareTo(_7daysAgoDate) == -1)
         {
            project.Log(Level.Info, "Delete file: " + file.Name);
            file.IsReadOnly = false;
            file.Delete();
          }
      }

     foreach (var dir in testsResultsDirectory.GetDirectories())
       {
             if (dir.CreationTime.CompareTo(_7daysAgoDate) == -1)
             { 
                  project.Log(Level.Info, "Delete directory: " + dir.Name);
                  try
                  {
                      dir.Delete(true);
                  }
                  catch (Exception exception)
                  {
                      Console.WriteLine("Caught exception: "+exception.Message);
                  }

             }
    }                   
}
        ]]>
      </code>
    </script> 
  </target>    
</project>

当我运行这个nant文件时,它会写出“构建成功”但没有任何反应。我错过了什么?

1 个答案:

答案 0 :(得分:0)

我找到了一个简单的解决方案 - 使用echo:

<echo message="Calling function: ${Cleaning::DeleteUnusedFilesAndFolders()}"/>

在此之前你应该定义前缀:

<project name="maintenance" prefix="Cleaning">