使用MSBuild自動編譯發布你的ASP.NET應用程式

來源:互聯網
上載者:User

標籤:style   blog   http   io   color   ar   os   使用   sp   

此文介紹如何使用MSBuild自動編譯並發布你的ASP.NET應用程式. 比較適合每日編譯.通常我們都使用IDE(Visual Studio)編譯並發布ASP.NET程式, 需要手動操作, 如果步驟繁瑣會很容易出錯. 同樣的工作可由MSBuild自動完成, MSBuild是一個Visual Studio編譯工具, 隨.NET Framework提供, 一般可在 %SystemDrive% \WINDOWS\Microsoft.NET\Framework\v3.5 找到.有關MSBuild的詳細介紹請看MSDN:  MSBuild Overview下面我們用一個例子來介紹一下MSBuild是如何做到這些的. 請從這裡下載樣本: MSBuildDemo解壓樣本檔案到一個新的目錄, 其中MSBuildDemo.sln是一個樣板項目, 這個樣板項目包括一個Class Library工程Ref, 一個Web Application工程WebApp和一個網站WebSite.而DailyBuild\DailyBuild.sln是我們為MSBuild建立的, 它包含了MSBuild編譯和發布的配置資訊, 包括編譯方案(Release), 網站發布設定等.現在試一下, 雙擊DailyBuild\DailyBuild.bat, 看能否自動編譯並發布WebApp到WebAppPublished, 編譯並發布WebSite到WebSitePublished目錄中? DailyBuild.bat中, 主要命令是: C:\WINDOWS\Microsoft.NET\Framework\v3.5\MSBuild.exe DailyBuild.xml /t:Main @DailyBuild.rsp其中DailyBuild.xml儲存了一系列MSBuild的任務(有關MSBuild任務的相關資訊, 請參考MSDN:  MSBuild Task Reference), MSBuild就是通過執行一系列的任務來編譯和發布你的程式的./t:Main 指定了MSBuild的進入點, 它說明了MSBuild應該從哪裡開始(後面還會提到).@DailyBuild.rsp為MSBuild指定了回應檔(為了避免重複輸入相同的命令開關, 將這些開關每個一行放入回應檔中代替).主要的部分就是DailyBuild.xml:  1<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> 2    <PropertyGroup> 3        <SolutionRoot>..</SolutionRoot> 4        <WebAppOutput>..\WebAppPublished</WebAppOutput> 5        <WebSiteOutput>..\WebSitePublished</WebSiteOutput> 6    </PropertyGroup> 7 8    <Target Name="GetLatest"> 9        <PropertyGroup>10            <TFServer>$/PO</TFServer>11            <WorkspacePath>D:\WorkCopies\PO\src\trunk</WorkspacePath>12            <TFCommand>D:\Programs\VS9\Common7\IDE\tf.exe</TFCommand>13        </PropertyGroup>1415        <Exec16            Command="$(TFCommand) history $(TFServer) /version:T /noprompt /recursive /stopafter:1 /format:detailed 1>DailyBuild\MSBuild\LatestChangeset.txt"17            WorkingDirectory="$(SolutionRoot)"18            ContinueOnError="true" />1920        <Exec21            Command="$(TFCommand) get"22            WorkingDirectory="$(WorkspacePath)"23            ContinueOnError="true" />24        25    </Target>2627    <Target Name="Build">28        <RemoveDir Directories="$(WebAppOutput)" ContinueOnError="false" />29        <MakeDir Directories="$(WebAppOutput)" ContinueOnError="false" />30        <RemoveDir Directories="$(WebSiteOutput)" ContinueOnError="false" />31        <MakeDir Directories="$(WebSiteOutput)" ContinueOnError="false" />3233        <MSBuild34            Projects="$(SolutionRoot)\DailyBuild\DailyBuild.sln"35            ContinueOnError="false"36            Targets="Rebuild"37            Properties="Configuration=Release">38        </MSBuild>39    </Target>4041    <Target Name="CleanupAfterBuild" DependsOnTargets="Build">42        <Delete Files="$(WebAppOutput)\bin\*.pdb" ContinueOnError="true" />43        <Delete44            Condition="Exists(‘$(WebAppOutput)\BFL.MSBuildDemo.WebApp.csproj‘)"45            Files="$(WebAppOutput)\BFL.MSBuildDemo.WebApp.csproj"46            ContinueOnError ="true" />47        <Delete48            Condition="Exists(‘$(WebAppOutput)\BFL.MSBuildDemo.WebApp.csproj.user‘)"49            Files="$(WebAppOutput)\BFL.MSBuildDemo.WebApp.csproj.user"50            ContinueOnError ="true" />51        <Delete52            Condition="Exists(‘$(WebAppOutput)\BFL.MSBuildDemo.WebApp.Publish.xml‘)"53            Files="$(WebAppOutput)\BFL.MSBuildDemo.WebApp.Publish.xml"54            ContinueOnError ="true" />55        <RemoveDir56            Condition="Exists(‘$(WebAppOutput)\obj‘)"57            Directories="$(WebAppOutput)\obj"58            ContinueOnError ="true" />5960        <Delete Files="$(WebSiteOutput)\bin\*.pdb" ContinueOnError="true" />61    </Target>6263    <Target Name="Main">64        <CallTarget Targets="GetLatest" ContinueOnError="false" />65        <CallTarget Targets="CleanupAfterBuild" />66    </Target>67</Project>其中 PropertyGroup 定義了一些全域變數, 你可以在這個檔案中以$(變數名)的方式使用它. Target 相當於函數或方法, 我們在上面的命令中為MSBuild定義的進入點/t:Main就是一個Target.Target由一組CallTarget或MSBuild工作群組成, 可以為Target元素定義DependsOnTargets屬性, 以確保在執行當前Target 前先調用DependsOnTargets指定的Target.由 DailyBuild.xml的Target名稱可以看出, 樣本中我們首先執行的是GetLatest, 它確保在編譯之前先擷取最新版本的代碼. 我們通過在GetLatest中執行兩個Exec任務做到這點.這裡我使用了TFS(Team Foundation Server), 你完全可以根據你的實際情況替換成等價的VSS, CVS或SVN命令.接下來執行的是Build, 我們調用MSBuild任務來編譯DailyBuild.sln解決方案, 並根據解決方案中的設定, 發布WebApp和WebSite. 最後, 根據多次自動編譯的經驗, 我添加了CleanupAfterBuild, 以刪除不必要的檔案.另外, 你可以參考這個連結MSBuild Community Tasks, 給MSBuild添加更多種任務.

 

使用MSBuild自動編譯發布你的ASP.NET應用程式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.