Visual Studio 2005網站發布

轉:http://blog.csdn.net/ttzhang/archive/2008/11/18/3331283.aspx

项目管理实践【五】自动编译和发布网站【Using Visual Studio with Source Control System to build and publish website automatically】 收藏

<script type="text/javascript"> </script> <script class="blogstory"> </script>

在上一篇教程项目管理实践【三】每日构建【Daily Build Using CruiseControl.NET and MSBuild】 中,我们讲解了如何使用CCNET+MSBuild来自动编译项目,今天我们讲解一下怎么使用MSBuild+WebDeployment+Robocopy自动编译过和部署ASP.NET网站。
首先安装下面的三个软件:

1.MSBuild.Community.Tasks下载:
http://msbuildtasks.tigris.org/files/documents/3383/28296/MSBuild.Community.Tasks.msi

源代码:
http://msbuildtasks.tigris.org/files/documents/3383/36642/MSBuild.Community.Tasks.v1.2.0.306.zip

 2.WebDeployment 下载:
http://download.microsoft.com/download/c/c/b/ccb4877f-55f7-4478-8f16-e41886607a0e/WebDeploymentSetup.msi

 3.Utility Spotlight Robocopy GUI 下载:【下载后,解压后安装,Vista不用安装】
http://download.microsoft.com/download/f/d/0/fd05def7-68a1-4f71-8546-25c359cc0842/UtilitySpotlight2006_11.exe

安装完成后,就开始今天的教程了。

我们以前面教程中创建的StartKit解决方案为例子,结构如下:

在上图所示的Web项目StartKit上右键点击,然后点击Add Web Deployment Project…,如下图:

 弹出下面的窗体,分别输入部署项目名称和项目要放置的位置,如下图:

 点击OK按钮后,解决方案的结构如下图:

今天会讲到下面二个方法,上面的步骤一样,从这里开始,下面的步骤有区别。

方法一:使用WebDeployment创建虚拟目录

优点:使用简单

缺点:功能不够强大,只能部署到虚拟目录

右键点击部署项目,点击菜单中的Property Pages,如下图:

在下面的窗体中,点击左侧的Complication,在右侧的Output Folder下的文本框中输入编译后网站文件的输出路径:

 然后,点击左侧的Deploment,在右侧选中Create an IIS virtual directory for the output folder前面的CheckBox,在下面的Virtual directory name下的文本框中输入虚拟目录的名字,Replace the existing virtual directory前面的CheckBox根据实际情况确定是否选中,如下图:

 点击确定按钮,编译部署项目StartKit.csproj_deploy,编译成功后,我们打开IIS,在默认网站下可以看到虚拟目录StartKit。OK,成功了!

方法二:使用WebDeployment+MSBuild+Robocopy

优点:功能强大

缺点:配置有点麻烦

这个方法不用配置Property Pages,直接右键点击StartKit.csproj_deploy项目文件,在菜单中点击Open Project File打开部署项目文件:


修改部署项目文件为下面的内容:

  1. <!--Microsoft Visual Studio 2008 Web Deployment Project http://go.microsoft.com/fwlink/?LinkID=104956-->  
  2. < Project  ToolsVersion ="3.5"  DefaultTargets ="Build"  xmlns ="http://schemas.microsoft.com/developer/msbuild/2003" >
  3. < PropertyGroup >
  4. < Configuration  Condition =" '$(Configuration)' == '' " > Debug</ Configuration >
  5. < Platform  Condition =" '$(Platform)' == '' " > AnyCPU</ Platform >
  6. < ProductVersion > 9.0.21022</ ProductVersion >
  7. < SchemaVersion > 2.0</ SchemaVersion >
  8. < ProjectGuid > {00000000-0000-0000-0000-000000000000}</ ProjectGuid >
  9. < SourceWebPhysicalPath > ../StartKit</ SourceWebPhysicalPath >
  10. < SourceWebProject > {96E1A089-3FBB-4909-94F6-172665994449}|StartKit/StartKit.csproj</ SourceWebProject >
  11. < SourceWebVirtualPath > /StartKit.csproj</ SourceWebVirtualPath >
  12. < TargetFrameworkVersion > v3.5</ TargetFrameworkVersion >
  13. < ProjectName > StartKit</ ProjectName >
  14. < Major > 1</ Major >
  15. < Minor > 0</ Minor >
  16. < Revision > 0</ Revision >
  17. < VSSName > ttzhang</ VSSName >
  18. < VSSPassword > 123456</ VSSPassword >
  19. < FtpName > anonymous</ FtpName >
  20. < FtpPassword > anonymous</ FtpPassword >
  21. < SmtpServerName > smtp.163.com</ SmtpServerName >
  22. < FromAddress > ttzhang@163.com</ FromAddress >
  23. < ToAddress > zttc@163.com</ ToAddress >
  24. < MailPassword > testmail</ MailPassword >
  25. </ PropertyGroup >
  26. < PropertyGroup  Condition =" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' " >
  27. < DebugSymbols > true</ DebugSymbols >
  28. < OutputPath > ./Debug</ OutputPath >
  29. < EnableUpdateable > true</ EnableUpdateable >
  30. < UseMerge > true</ UseMerge >
  31. < SingleAssemblyName > StartKit_deploy</ SingleAssemblyName >
  32. </ PropertyGroup >
  33. < PropertyGroup  Condition =" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' " >
  34. < DebugSymbols > false</ DebugSymbols >
  35. < OutputPath > ./Release</ OutputPath >
  36. < EnableUpdateable > true</ EnableUpdateable >
  37. < UseMerge > true</ UseMerge >
  38. < SingleAssemblyName > StartKit_deploy</ SingleAssemblyName >
  39. </ PropertyGroup >
  40. < ItemGroup >
  41. </ ItemGroup >
  42. <!--下面的ItemGroup节点可选,这个和项目文件StartKit.csproj中的内容相同-->
  43. < ItemGroup >
  44. < ProjectReference  Include ="../BLL/BLL.csproj" >
  45. < Project > {73A293A1-CDCC-4919-9B05-BA2531ADDB56}</ Project >
  46. < Name > BLL</ Name >
  47. </ ProjectReference >
  48. < ProjectReference  Include ="../DAL/DAL.csproj" >
  49. < Project > {AFF6077D-DD2D-48A0-BFAD-051BD67A6953}</ Project >
  50. < Name > DAL</ Name >
  51. </ ProjectReference >
  52. < ProjectReference  Include ="../IBLL/IBLL.csproj" >
  53. < Project > {620770BB-7A27-4585-9B97-44EEE349121D}</ Project >
  54. < Name > IBLL</ Name >
  55. </ ProjectReference >
  56. < ProjectReference  Include ="../Model/Model.csproj" >
  57. < Project > {EA43EC2E-5890-4431-BD3E-5F6C090DEA3A}</ Project >
  58. < Name > Model</ Name >
  59. </ ProjectReference >
  60. </ ItemGroup >
  61. <!--引入MSBuildCommunityTasks-->
  62. < Import  Project ="$(MSBuildExtensionsPath)/MSBuildCommunityTasks/MSBuild.Community.Tasks.Targets"  />
  63. <!--邮件发送-->
  64. <!--< Target  Name ="EmailTest"  >
  65. < Message  Text  = " Mail sending..." > </ Message >
  66. < Mail  SmtpServer ="$(SmtpServerName)"
  67. Subject ="Test"
  68. Password ="$(MailPassword)"
  69. From  ="$(FromAddress)"
  70. To  ="$(ToAddress)"
  71. Body ="This is a test of the mail task."  />
  72. </ Target > -->
  73. <!--备份文件到FTP-->
  74. <!--< Target  Name ="Backup"  DependsOnTargets ="Zip"  >
  75. < FtpUpload  UserName ="$(FtpName)"
  76. Password ="$(FtpPassword)"
  77. LocalFile ="$(ZipFileName)"
  78. RemoteUri ="ftp://192.168.1.2/SourceBackup/$(ZipFileName)"  />
  79. < OnError  ExecuteTargets ="HandleErrorBackup"  />
  80. </ Target > -->
  81. <!--备份文件到FTP失败则发送邮件-->
  82. <!--< Target  Name ="HandleErrorBackup" >
  83. < Message  Text ="Backup failed..............."  />
  84. < Mail  SmtpServer ="$(SmtpServerName)"
  85. To ="$(ToAddress)"
  86. From ="$(FromAddress)"
  87. Subject ="$(ProjectName) Build failed"
  88. Body ="Backup Failure: Could not finish Backup ."  />
  89. </ Target > -->
  90. <!--编译项目-->
  91. < Target  Name ="BuildProjectReferences" >
  92. < MSBuild  Projects ="@(ProjectReference)"  Targets ="Build"  />
  93. </ Target >
  94. <!--生成压缩文件-->
  95. < Target  Name ="Zip" >
  96. <!--时间格式-->
  97. < Time  Format ="yyyyMMddHHmmss" >
  98. < Output  TaskParameter ="FormattedTime"  PropertyName ="buildDate" />
  99. </ Time >
  100. < Zip  Files ="@(ZipFiles)"  ZipFileName ="StartKit V$(Major)-$(Minor)-$(Revision)-$(buildDate).zip" />
  101. </ Target >
  102. <!--复制文件-->
  103. < Target  Name ="Copy" >
  104. <!--停止IIS服务-->
  105. < ServiceController  ServiceName ="w3svc"  Action ="Stop"  />
  106. <!--使用Robocopy复制编译后的文件到指定位置 /XD是要忽略的文件夹,/XF要忽略的文件类型-->
  107. < Exec  Command ="Robocopy Debug c:/inetpub/StartKit /MIR /XD Fckeditor attachments .svn obj doc Test /XF *.zip *.wdproj *.user *.cs *.csproj"  IgnoreExitCode ="true"  />
  108. <!--启动IIS服务-->
  109. < ServiceController  ServiceName ="w3svc"  Action ="Start"  />
  110. </ Target >
  111. <!--引入WebDeployment-->
  112. < Import  Project ="$(MSBuildExtensionsPath)/Microsoft/WebDeployment/v9.0/Microsoft.WebDeployment.targets"  />
  113. <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
  114. Other similar extension points exist, see Microsoft.WebDeployment.targets.-->
  115. < Target  Name ="BeforeBuild" > </ Target >
  116. < Target  Name ="BeforeMerge" > </ Target >
  117. < Target  Name ="AfterMerge" > </ Target >
  118. < Target  Name ="AfterBuild" >
  119. <!--编译成功后,执行下面的Targets-->
  120. <!—不想生成ZIP文件,可以注释下面ZIP的target-->
  121. < CallTarget  Targets ="Zip" />
  122. < CallTarget  Targets ="Copy"  />
  123. <!--< CallTarget  Targets ="EmailTest" />
  124. < CallTarget  Targets ="Backup"  /> -->
  125. </ Target >
  126. </ Project >

编译部署项目成功后,打开 C /inetpub/StartKit 文件夹,看看是否成功复制过去了呢?好的,我去看看,哈哈,文件果然都在, OK, 成功啦!

这时候,在 IIS 上创建一个虚拟目录或者网站,指向我们部署项目中指定的目录。上一篇我们已经将该项目添加到了 CCNET 中,所以以后我们每次提交代码后, MSBuild 就会编译整个解决方案【当然也会编译部署项目】,如果编译成功,就会自动将最新的程序部署到我们网站上。这样就可以使网站和我们的开发实时保持同步,这只不是唯一的实现方法,其他还有很多可以实现这个功能的方法,大家可以在这里讨论和交流。

补充:
Microsoft Build Engine (MSBuild) Microsoft Visual Studio 的新的生成平台。 MSBuild 在如何处理和生成软件方面是完全透明的,使开发人员能够在未安装 Visual Studio 的生成实验室环境中组织和生成产品。通过这几篇教程,我们可以看出 ,MSBuild 的强大功能,如果希望理解更多关于 MSBuild 的信息,请查看这里http://msdn.microsoft.com/zh-cn/library/ms171451.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值