天天看點

項目管理實踐【五】自動編譯和釋出網站【Using Visual Studio with Source Control System to build and publish website automatically】

在上一篇教程項目管理實踐【三】每日建構【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解決方案為例子,結構如下:

項目管理實踐【五】自動編譯和釋出網站【Using Visual Studio with Source Control System to build and publish website automatically】

在上圖所示的Web項目StartKit上右鍵點選,然後點選Add Web Deployment Project…,如下圖:

項目管理實踐【五】自動編譯和釋出網站【Using Visual Studio with Source Control System to build and publish website automatically】

 彈出下面的窗體,分别輸入部署項目名稱和項目要放置的位置,如下圖:

項目管理實踐【五】自動編譯和釋出網站【Using Visual Studio with Source Control System to build and publish website automatically】

 點選OK按鈕後,解決方案的結構如下圖:

項目管理實踐【五】自動編譯和釋出網站【Using Visual Studio with Source Control System to build and publish website automatically】

今天會講到下面二個方法,上面的步驟一樣,從這裡開始,下面的步驟有差別。

方法一:使用WebDeployment建立虛拟目錄

優點:使用簡單

缺點:功能不夠強大,隻能部署到虛拟目錄

右鍵點選部署項目,點選菜單中的Property Pages,如下圖:

項目管理實踐【五】自動編譯和釋出網站【Using Visual Studio with Source Control System to build and publish website automatically】

在下面的窗體中,點選左側的Complication,在右側的Output Folder下的文本框中輸入編譯後網站檔案的輸出路徑:

項目管理實踐【五】自動編譯和釋出網站【Using Visual Studio with Source Control System to build and publish website automatically】

 然後,點選左側的Deploment,在右側選中Create an IIS virtual directory for the output folder前面的CheckBox,在下面的Virtual directory name下的文本框中輸入虛拟目錄的名字,Replace the existing virtual directory前面的CheckBox根據實際情況确定是否選中,如下圖:

項目管理實踐【五】自動編譯和釋出網站【Using Visual Studio with Source Control System to build and publish website automatically】

 點選确定按鈕,編譯部署項目StartKit.csproj_deploy,編譯成功後,我們打開IIS,在預設網站下可以看到虛拟目錄StartKit。OK,成功了!

方法二:使用WebDeployment+MSBuild+Robocopy

優點:功能強大

缺點:配置有點麻煩

這個方法不用配置Property Pages,直接右鍵點選StartKit.csproj_deploy項目檔案,在菜單中點選Open Project File打開部署項目檔案:

項目管理實踐【五】自動編譯和釋出網站【Using Visual Studio with Source Control System to build and publish website automatically】

修改部署項目檔案為下面的内容:

  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>[email protected]</FromAddress>
  23. <ToAddress>[email protected]</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 。

繼續閱讀