用 NAnt build .NET 项目

NAnt 是一个.NET 的Build工具, 官方网站: http://nant.sourceforge.net/. NAnt提供了丰富的API, 可以方便地集成、部署.NET 项目.

这是一个部署ASP.NET项目的例子, 要实现:

  • copy文件到特定目录, 过滤文件
  • copy所有文件到特定目录, 过滤文件/文件夹
  • 设置文件夹权限, 自动处理"ARE YOU SURE? Y/N"提示 (参见:http://support.microsoft.com/kb/135268

操作: 在project中设置post-build event command: $(SolutionDir)build.bat. 具体见代码:

build.bat:

C:\nant\bin\NAnt -buildfile:~FilePath/nant.build
echo y| cacls C:\temp\webroot\gen_files /G <MY_USER_NAME>:F

nant.build:

<?xml version="1.0"?>
<project name="FrankWeb" default="build" basedir="FrankWebApp">
    <target name="build">
        <echo message="Hello, this is NAnt build." />
        
        <property name="dir.base" value="${project::get-base-directory()}" />
        <property name="dir.bin" value="${dir.base}\bin" />
        <echo message="the project dir is: ${dir.base}" />
        
        <property name="dir.to" value="C:\temp\webroot" />
        <delete dir="${dir.to}" />
        
        <property name="dir.gen_files" value="${dir.to}\gen_files" />
        <foreach item="File" in="${dir.bin}" property="file.name">
            <echo message="the file in bin: ${file.name}" />
            <if test="${not string::ends-with(file.name, '.pdb')}">
                <copy file="${file.name}" todir="${dir.to}\to_files" />
            </if>
            <if test="${string::ends-with(file.name, '.pdb')}">
                <copy file="${file.name}" todir="${dir.gen_files}" />
            </if>
        </foreach>
        
        <copy todir="${dir.to}">
            <fileset>
                <include name="**/*.aspx" />
                <include name="**/*.cs" />
                <include name="**/*.js" />
                <include name="**/*.css" />
                <include name="**/*.config" />
                <include name="**/*.csproj" />
                <include name="**/*.dll" />
                <include name="**/*.pdb" />
                
                <exclude name="**/obj/**" />
            </fileset>
        </copy>
    </target>
</project>
posted @ 2013-02-25 16:39  ☆大森林☆  阅读(601)  评论(0编辑  收藏  举报