一个使用MSBuild Task自动编译部署的例子

 
用户可以根据本地文件或者SVN目录中的最新资料来进行本地编译后将Release文件自动拷贝,Web目录自动拷贝后压缩,生成Zip文件来部署。这个方法的优点是操作简单,容易和其他工具并用。
 
Build.bat 内容:
 

@ECHO   OFF
SET   / P DeployConfig = Specify build target  ( 'Production' or 'Development' )
 
IF   / NOT  '%DeployConfig%' == 'Production'  (
            
IF   / NOT  '%DeployConfig%' == 'Development'  (
                        
ECHO  Please specify Production or Development .
                        
GOTO  finish
            
)
)
 
SET   / P VersionTag = Enter SVN revision  ( e . g .   1.2 . 4 ),  or 'trunk': 
 
echo .
 
msbuild project
. msbuild  / p :VersionTag = %VersionTag% ; DeployConfig = %DeployConfig%  / nologo
 
 
rem  IF %ERRORLEVEL% GTR 0 (
rem
      ECHO.
rem
      ECHO Build failed for revision number '%VersionTag%'
rem
      ECHO.
rem
 ) ELSE (
rem
 
rem
      echo Build succeeded for revision number '%VersionTag%'
rem
 )
 
 
:finish
 
 
project.msbuild内容:
 

< Project  xmlns ="http://schemas.microsoft.com/developer/msbuild/2003"  DefaultTargets ="DeploymentZip" >
            
< PropertyGroup >
                        
< DefaultConfiguration > Release </ DefaultConfiguration >
                        
< DefaultSvnServer > 192.168.0.2 </ DefaultSvnServer >
            
</ PropertyGroup >
            
            
<!-- Choose>
                        <When Condition="'$(VersionTag)' == 'trunk'">
                                    <PropertyGroup>
                                                <RepositoryPath>svn://$(DefaultSvnServer)/MYProject/trunk</RepositoryPath>
                                    </PropertyGroup>
                        </When>
                        <Otherwise>
                                    <PropertyGroup>
                                                <RepositoryPath>svn://$(DefaultSvnServer)/MYProject/tags/$(VersionTag)</RepositoryPath>
                                    </PropertyGroup>
                        </Otherwise>
            </Choose
-->
            
            
            
<!--  Retrieve project files from source control, of the specified revision  -->
            
<!-- Target Name="SvnExport">
                        <RemoveDir Directories="buildExport" />
                        <MakeDir Directories="buildExport" />
                        
                        <SvnExport 
                                    RepositoryPath="$(RepositoryPath)/web"
                                    LocalPath="buildExport/Web"
                                    Username="deploy"
                                    Password="readonly" />
                        
                        <SvnExport 
                                    RepositoryPath="$(RepositoryPath)/api"
                                    LocalPath="buildExportAPI"
                                    Username="deploy"
                                    Password="readonly" />
            </Target
-->
            
            
< ItemGroup >
      
< WebFiles  Include ="..Web***.*"  Exclude ="..Web**.svn**;..Web***.pdb" />  
   
</ ItemGroup >
 
            
<!--  Compile API  -->
            
<!-- Target Name="API" DependsOnTargets="SvnExport" -->
            
< Target  Name ="CompileAPI" >
                        
< MSBuild 
                                    
Projects ="..APIapi.csproj"
                                    StopOnFirstFailure
="true"
                                    Properties
="Configuration=$(DefaultConfiguration)"   />               
            
</ Target >
 
 
< Target  Name ="CopyEverything"  DependsOnTargets ="CompileAPI" >
           
< RemoveDir  Directories ="buildExport"   />
           
< MakeDir  Directories ="buildExport"   />
           
< MakeDir  Directories ="buildExportWeb"   />
           
< MakeDir  Directories ="buildExportAPI"   />
           
< Copy 
                                    
SourceFiles ="..APIin$(DefaultConfiguration)MYProject.dll"  
                                    DestinationFolder
="buildExportWebin"   />
                        
< Copy 
                                    
SourceFiles ="@(WebFiles)"  
                                    DestinationFiles
="@(WebFiles->'buildExportWeb%(RecursiveDir)%(Filename)%(Extension)')"   />
 
</ Target >
 
            
< Target  Name ="CopyWebConfig"  DependsOnTargets ="CopyEverything" >
                        
< Copy 
                                    
SourceFiles ="buildExportWebConfig$(DeployConfig)Web.config"
                                    DestinationFiles
="buildExportWebWeb.config"   />
                        
< RemoveDir  Directories ="buildExportWebConfig"   />
            
</ Target >
 
<!--  Zip up all the files for transporting to deploy  -->
            
< Target 
                        
Name ="DeploymentZip"  
                        DependsOnTargets
="CopyEverything;CopyWebConfig"
                        Condition
="('$(DeployConfig)' == 'QA' OR '$(DeployConfig)' == 'Production' OR '$(DeployConfig)' == 'Development') AND '$(VersionTag)' != ''"
                        
>
 
                        
< CreateItem 
                                    
Include ="buildExportWeb**;" >
                                    
< Output  TaskParameter ="Include"  ItemName ="WebAppFiles"   />
                        
</ CreateItem >
                        
                        
< Zip 
                                    
Files ="@(WebAppFiles)"  
                                    ZipFileName
="MYProject_$(DeployConfig)_$(VersionTag).zip"
                                    WorkingDirectory
="buildExportWeb"   />
 
                        
< Message  Text ="Built deployment file with $(DefaultConfiguration) for $(DeployConfig)."   />
                        
< Message  Text ="Deployment package: MYPROJECT_$(DeployConfig)_$(VersionTag).zip"   />
                        
< Message  Text ="Done."   />
                        
            
</ Target >
 
            
< UsingTask  TaskName ="MSBuild.Community.Tasks.Subversion.SvnExport"  AssemblyFile ="$(MSBuildExtensionsPath)MSBuildCommunityTasksMSBuild.Community.Tasks.dll"   />
            
< UsingTask  TaskName ="MSBuild.Community.Tasks.Zip"  AssemblyFile ="$(MSBuildExtensionsPath)MSBuildCommunityTasksMSBuild.Community.Tasks.dll"   />
</ Project >
 
这2个文件没有进行优化,根据不同项目有很大改动余地。但基本功能都经过测试。在XML模式下编辑可以方便看到很多地方我都注释了,也都通过测试。相信对大多用户有所帮助。
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值