在上一篇教程項目管理實踐三、每日構建【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/28296/MSBuild.Community.Tasks.msi
源代碼:
http://msbuildtasks.tigris.org/files/documents/3383/36642/MSBuild.Community.Tasks.v1.2.0.306.zip
http://msbuildtasks.tigris.org/files/documents/3383/36642/MSBuild.Community.Tasks.v1.2.0.306.zip
2.WebDeployment下載:
For VS2008 [RTW]
http://download.microsoft.com/download/9/4/9/9496adc4-574e-4043-bb70-bc841e27f13c/WebDeploymentSetup.msi
http://download.microsoft.com/download/9/4/9/9496adc4-574e-4043-bb70-bc841e27f13c/WebDeploymentSetup.msi
3.Utility Spotlight Robocopy GUI 下載:【下載後,解壓後安裝,Vista不用安裝】
http://download.microsoft.com/download/f/d/0/fd05def7-68a1-4f71-8546-25c359cc0842/UtilitySpotlight2006_11.exe
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
3 <Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
4 <PropertyGroup>
5 <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6 <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7 <ProductVersion>9.0.21022</ProductVersion>
8 <SchemaVersion>2.0</SchemaVersion>
9 <ProjectGuid>{00000000-0000-0000-0000-000000000000}</ProjectGuid>
10 <SourceWebPhysicalPath>..\StartKit</SourceWebPhysicalPath>
11 <SourceWebProject>{96E1A089-3FBB-4909-94F6-172665994449}|StartKit\StartKit.csproj</SourceWebProject>
12 <SourceWebVirtualPath>/StartKit.csproj</SourceWebVirtualPath>
13 <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
14 <ProjectName>StartKit</ProjectName>
15 <Major>1</Major>
16 <Minor>0</Minor>
17 <Revision>0</Revision>
18 <VSSName>ttzhang</VSSName>
19 <VSSPassword>123456</VSSPassword>
20 <FtpName>anonymous</FtpName>
21 <FtpPassword>anonymous</FtpPassword>
22 <SmtpServerName>smtp.163.com</SmtpServerName>
23 <FromAddress>[email protected]</FromAddress>
24 <ToAddress>[email protected]</ToAddress>
25 <MailPassword>testmail</MailPassword>
26 </PropertyGroup>
27 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
28 <DebugSymbols>true</DebugSymbols>
29 <OutputPath>.\Debug</OutputPath>
30 <EnableUpdateable>true</EnableUpdateable>
31 <UseMerge>true</UseMerge>
32 <SingleAssemblyName>StartKit_deploy</SingleAssemblyName>
33 </PropertyGroup>
34 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
35 <DebugSymbols>false</DebugSymbols>
36 <OutputPath>.\Release</OutputPath>
37 <EnableUpdateable>true</EnableUpdateable>
38 <UseMerge>true</UseMerge>
39 <SingleAssemblyName>StartKit_deploy</SingleAssemblyName>
40 </PropertyGroup>
41 <ItemGroup>
42 </ItemGroup>
43 <!--下面的ItemGroup節點可選,這個和項目文件StartKit.csproj中的內容相同-->
44 <ItemGroup>
45 <ProjectReference Include="..\BLL\BLL.csproj">
46 <Project>{73A293A1-CDCC-4919-9B05-BA2531ADDB56}</Project>
47 <Name>BLL</Name>
48 </ProjectReference>
49 <ProjectReference Include="..\DAL\DAL.csproj">
50 <Project>{AFF6077D-DD2D-48A0-BFAD-051BD67A6953}</Project>
51 <Name>DAL</Name>
52 </ProjectReference>
53 <ProjectReference Include="..\IBLL\IBLL.csproj">
54 <Project>{620770BB-7A27-4585-9B97-44EEE349121D}</Project>
55 <Name>IBLL</Name>
56 </ProjectReference>
57 <ProjectReference Include="..\Model\Model.csproj">
58 <Project>{EA43EC2E-5890-4431-BD3E-5F6C090DEA3A}</Project>
59 <Name>Model</Name>
60 </ProjectReference>
61 </ItemGroup>
62 <!--引入MSBuildCommunityTasks-->
63 <Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets" />
64 <!--郵件發送-->
65 <!--<Target Name="EmailTest" >
66 <Message Text = " Mail sending……"></Message>
67 <Mail SmtpServer="$(SmtpServerName)"
68 Subject="Test"
69 Password="$(MailPassword)"
70 From ="$(FromAddress)"
71 To ="$(ToAddress)"
72 Body="This is a test of the mail task." />
73 </Target>-->
74 <!--備份文件到FTP-->
75 <!--<Target Name="Backup" DependsOnTargets="Zip" >
76 <FtpUpload UserName="$(FtpName)"
77 Password="$(FtpPassword)"
78 LocalFile="$(ZipFileName)"
79 RemoteUri="ftp://192.168.1.2/SourceBackup/$(ZipFileName)" />
80 <OnError ExecuteTargets="HandleErrorBackup" />
81 </Target>-->
82 <!--備份文件到FTP失敗則發送郵件-->
83 <!--<Target Name="HandleErrorBackup">
84 <Message Text="Backup failed……" />
85 <Mail SmtpServer="$(SmtpServerName)"
86 To="$(ToAddress)"
87 From="$(FromAddress)"
88 Subject="$(ProjectName) Build failed"
89 Body="Backup Failure: Could not finish Backup ." />
90 </Target>-->
91 <!--編譯項目-->
92 <Target Name="BuildProjectReferences">
93 <MSBuild Projects="@(ProjectReference)" Targets="Build" />
94 </Target>
95 <!--生成壓縮文件-->
96 <Target Name="Zip">
97 <!--時間格式-->
98 <Time Format="yyyyMMddHHmmss">
99 <Output TaskParameter="FormattedTime" PropertyName="buildDate"/>
100 </Time>
101 <Zip Files="@(ZipFiles)" ZipFileName="StartKit V$(Major)-$(Minor)-$(Revision)-$(buildDate).zip"/>
102 </Target>
103 <!--複製文件-->
104 <Target Name="Copy">
105 <!--停止IIS服務-->
106 <ServiceController ServiceName="w3svc" Action="Stop" />
107 <!--使用Robocopy複製編譯後的文件到指定位置 /XD是要忽略的文件夾,/XF要忽略的文件類型-->
108 <Exec Command="Robocopy Debug c:\inetpub\StartKit /MIR /XD Fckeditor attachments .svn obj doc Test /XF *.zip *.wdproj *.user *.cs *.csproj" IgnoreExitCode="true" />
109 <!--啓動IIS服務-->
110 <ServiceController ServiceName="w3svc" Action="Start" />
111 </Target>
112 <!--引入WebDeployment-->
113 <Import Project="$(MSBuildExtensionsPath)\Microsoft\WebDeployment\v9.0\Microsoft.WebDeployment.targets" />
114 <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
115 Other similar extension points exist, see Microsoft.WebDeployment.targets.-->
116 <Target Name="BeforeBuild"></Target>
117 <Target Name="BeforeMerge"></Target>
118 <Target Name="AfterMerge"></Target>
119 <Target Name="AfterBuild">
120 <!--編譯成功後,執行下面的Targets-->
121 <!—不想生成ZIP文件,可以註釋下面ZIP的target-->
122 <CallTarget Targets="Zip"/>
123 <CallTarget Targets="Copy" />
124 <!--<CallTarget Targets="EmailTest"/>
125 <CallTarget Targets="Backup" />-->
126 </Target>
127 </Project>
2
3 <Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
4 <PropertyGroup>
5 <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6 <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7 <ProductVersion>9.0.21022</ProductVersion>
8 <SchemaVersion>2.0</SchemaVersion>
9 <ProjectGuid>{00000000-0000-0000-0000-000000000000}</ProjectGuid>
10 <SourceWebPhysicalPath>..\StartKit</SourceWebPhysicalPath>
11 <SourceWebProject>{96E1A089-3FBB-4909-94F6-172665994449}|StartKit\StartKit.csproj</SourceWebProject>
12 <SourceWebVirtualPath>/StartKit.csproj</SourceWebVirtualPath>
13 <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
14 <ProjectName>StartKit</ProjectName>
15 <Major>1</Major>
16 <Minor>0</Minor>
17 <Revision>0</Revision>
18 <VSSName>ttzhang</VSSName>
19 <VSSPassword>123456</VSSPassword>
20 <FtpName>anonymous</FtpName>
21 <FtpPassword>anonymous</FtpPassword>
22 <SmtpServerName>smtp.163.com</SmtpServerName>
23 <FromAddress>[email protected]</FromAddress>
24 <ToAddress>[email protected]</ToAddress>
25 <MailPassword>testmail</MailPassword>
26 </PropertyGroup>
27 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
28 <DebugSymbols>true</DebugSymbols>
29 <OutputPath>.\Debug</OutputPath>
30 <EnableUpdateable>true</EnableUpdateable>
31 <UseMerge>true</UseMerge>
32 <SingleAssemblyName>StartKit_deploy</SingleAssemblyName>
33 </PropertyGroup>
34 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
35 <DebugSymbols>false</DebugSymbols>
36 <OutputPath>.\Release</OutputPath>
37 <EnableUpdateable>true</EnableUpdateable>
38 <UseMerge>true</UseMerge>
39 <SingleAssemblyName>StartKit_deploy</SingleAssemblyName>
40 </PropertyGroup>
41 <ItemGroup>
42 </ItemGroup>
43 <!--下面的ItemGroup節點可選,這個和項目文件StartKit.csproj中的內容相同-->
44 <ItemGroup>
45 <ProjectReference Include="..\BLL\BLL.csproj">
46 <Project>{73A293A1-CDCC-4919-9B05-BA2531ADDB56}</Project>
47 <Name>BLL</Name>
48 </ProjectReference>
49 <ProjectReference Include="..\DAL\DAL.csproj">
50 <Project>{AFF6077D-DD2D-48A0-BFAD-051BD67A6953}</Project>
51 <Name>DAL</Name>
52 </ProjectReference>
53 <ProjectReference Include="..\IBLL\IBLL.csproj">
54 <Project>{620770BB-7A27-4585-9B97-44EEE349121D}</Project>
55 <Name>IBLL</Name>
56 </ProjectReference>
57 <ProjectReference Include="..\Model\Model.csproj">
58 <Project>{EA43EC2E-5890-4431-BD3E-5F6C090DEA3A}</Project>
59 <Name>Model</Name>
60 </ProjectReference>
61 </ItemGroup>
62 <!--引入MSBuildCommunityTasks-->
63 <Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets" />
64 <!--郵件發送-->
65 <!--<Target Name="EmailTest" >
66 <Message Text = " Mail sending……"></Message>
67 <Mail SmtpServer="$(SmtpServerName)"
68 Subject="Test"
69 Password="$(MailPassword)"
70 From ="$(FromAddress)"
71 To ="$(ToAddress)"
72 Body="This is a test of the mail task." />
73 </Target>-->
74 <!--備份文件到FTP-->
75 <!--<Target Name="Backup" DependsOnTargets="Zip" >
76 <FtpUpload UserName="$(FtpName)"
77 Password="$(FtpPassword)"
78 LocalFile="$(ZipFileName)"
79 RemoteUri="ftp://192.168.1.2/SourceBackup/$(ZipFileName)" />
80 <OnError ExecuteTargets="HandleErrorBackup" />
81 </Target>-->
82 <!--備份文件到FTP失敗則發送郵件-->
83 <!--<Target Name="HandleErrorBackup">
84 <Message Text="Backup failed……" />
85 <Mail SmtpServer="$(SmtpServerName)"
86 To="$(ToAddress)"
87 From="$(FromAddress)"
88 Subject="$(ProjectName) Build failed"
89 Body="Backup Failure: Could not finish Backup ." />
90 </Target>-->
91 <!--編譯項目-->
92 <Target Name="BuildProjectReferences">
93 <MSBuild Projects="@(ProjectReference)" Targets="Build" />
94 </Target>
95 <!--生成壓縮文件-->
96 <Target Name="Zip">
97 <!--時間格式-->
98 <Time Format="yyyyMMddHHmmss">
99 <Output TaskParameter="FormattedTime" PropertyName="buildDate"/>
100 </Time>
101 <Zip Files="@(ZipFiles)" ZipFileName="StartKit V$(Major)-$(Minor)-$(Revision)-$(buildDate).zip"/>
102 </Target>
103 <!--複製文件-->
104 <Target Name="Copy">
105 <!--停止IIS服務-->
106 <ServiceController ServiceName="w3svc" Action="Stop" />
107 <!--使用Robocopy複製編譯後的文件到指定位置 /XD是要忽略的文件夾,/XF要忽略的文件類型-->
108 <Exec Command="Robocopy Debug c:\inetpub\StartKit /MIR /XD Fckeditor attachments .svn obj doc Test /XF *.zip *.wdproj *.user *.cs *.csproj" IgnoreExitCode="true" />
109 <!--啓動IIS服務-->
110 <ServiceController ServiceName="w3svc" Action="Start" />
111 </Target>
112 <!--引入WebDeployment-->
113 <Import Project="$(MSBuildExtensionsPath)\Microsoft\WebDeployment\v9.0\Microsoft.WebDeployment.targets" />
114 <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
115 Other similar extension points exist, see Microsoft.WebDeployment.targets.-->
116 <Target Name="BeforeBuild"></Target>
117 <Target Name="BeforeMerge"></Target>
118 <Target Name="AfterMerge"></Target>
119 <Target Name="AfterBuild">
120 <!--編譯成功後,執行下面的Targets-->
121 <!—不想生成ZIP文件,可以註釋下面ZIP的target-->
122 <CallTarget Targets="Zip"/>
123 <CallTarget Targets="Copy" />
124 <!--<CallTarget Targets="EmailTest"/>
125 <CallTarget Targets="Backup" />-->
126 </Target>
127 </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 。
補充:
Microsoft Build Engine (MSBuild) 是 Microsoft 和 Visual Studio 的新的生成平臺。MSBuild 在如何處理和生成軟件方面是完全透明的,使開發人員能夠在未安裝 Visual Studio 的生成實驗室環境中組織和生成產品。通過這幾篇教程,我們可以看出,MSBuild的強大功能,如果希望理解更多關於MSBuild的信息,請查看這裏http://msdn.microsoft.com/zh-cn/library/ms171451.aspx 。