Ms build的初體驗

324 views
218 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
324
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Ms build的初體驗

  1. 1. MsBuild的初體驗 2013.08.30 Bryan Lin
  2. 2. Agenda • Why MsBuild? • 認識MsBuild • 範例實作
  3. 3. Why MsBuild? • 當我們要發佈站台的時候: • 取得最新版本 • 所有程式都要Build過 • 更改設定 • …(其他步驟) • 產出檔案要publish至對應的目錄
  4. 4. Why MsBuild? • 有這麼多步驟要做,而且要分別執行,很麻煩。 • 有沒有一個方法只執行一次就做完所有動作? • 就是MsBuild了!
  5. 5. 認識MsBuild • MsBuild是什麼?能做到那些事情? • 是一個Xml格式的文件 • 裡面描述了專案所包含的檔案有哪些,建置的設定等等資訊(ex. *.csproj檔案) • 也可以使用MsBuild來撰寫建置站台所需要的流程
  6. 6. 認識MsBuild • MsBuild的基本框架 • MsBuild的內容其實是由一個Project包含著多個Target所組合而成, 再依照參數的不同來決定將會執行那些Target • 範例如下: <?xml version="1.0" encoding="utf-8"?> <Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/deve loper/msbuild/2003"> <Target Name="Build"> <Message Text="Hello Msbuild!" /> </Target> </Project>
  7. 7. 認識MsBuild • 在專案檔中我們指定了預設執行的Target為Build, 在Build之中執行了顯示訊息的任務 • 執行方式:在Visiual Stuiod Cmd中,執行MsBuild <你的Build檔案> • 在畫面上輸出Hello Msbuild!
  8. 8. 認識MsBuild • 想執行多個任務,範例如下:
  9. 9. 認識MsBuild <?xml version="1.0" encoding="utf-8"?> <Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Target Name="Build"> <CallTarget Targets="Step 1"/> <CallTarget Targets="Step 2"/> </Target> <Target Name="Step 1"> <Message Text="Step 1" /> </Target> <Target Name="Step 2"> <Message Text="Step 2" /> </Target> </Project>
  10. 10. 認識MsBuild • 想要了解更多有關MsBuild的資訊? • 請洽http://msdn.microsoft.com/zh-tw/library/dd393574.aspx
  11. 11. 範例實作 • 取得目前專案的ChangeSet,並將此ChangeSet寫入AssemblyVersion
  12. 12. 範例實作 <?xml version="1.0" encoding="utf-8"?> <Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup> <ProjectSourceControl>./</ProjectSourceControl> <VersionTextPath>version.txt</VersionTextPath> <MajorVersion Condition="$(MajorVersion) == ''">8</MajorVersion> <MinorVersion Condition="$(MinorVersion) == ''">1</MinorVersion> <ProgramFiles32 Condition="$(ProgramW6432) != ''">C:Program Files (x86)</ProgramFiles32> <ProgramFiles32 Condition="$(ProgramFiles32) == ''">C:Program Files</ProgramFiles32> <VS10Dir>$(ProgramFiles32)Microsoft Visual Studio 10.0</VS10Dir> <VS11Dir>$(ProgramFiles32)Microsoft Visual Studio 11.0</VS11Dir> <TF Condition="Exists('$(VS10Dir)')">&quot;$(VS10Dir)Common7IDETF.exe&quot;</TF> <TF Condition="Exists('$(VS11Dir)')">&quot;$(VS11Dir)Common7IDETF.exe&quot;</TF> </PropertyGroup>
  13. 13. 範例實作(續) <Target Name="GetVersion" BeforeTargets="Build" Condition="$(Configuration.Contains('Release'))"> <Message Text="***************** $(Configuration)" Importance="high" /> <Exec Command="$(TF) history /stopafter:1 /noprompt /recursive $(ProjectSourceControl) > $(VersionTextPath)"/> <PropertyGroup> <VersionContent>$([System.IO.File]::ReadAllText('$(VersionTextPath)'))</VersionContent> <CurrentVersion>$([System.Text.RegularExpressions.Regex]::Match($(VersionContent), '*?s*(?&lt;version&gt;d+)s*(S*)?s*((?&lt;year&gt;d{4})/(?&lt;month&gt;d{0,2})/(?&lt;day&gt;d{0,2})).*?$').Groups["version"].Value)</CurrentVersion> <CurrentVersionYear>$([System.Text.RegularExpressions.Regex]::Match($(VersionContent), '*?s*(?&lt;version&gt;d+)s*(S*)?s*((?&lt;year&gt;d{4})/(?&lt;month&gt;d{0,2})/(?&lt;day&gt;d{0,2})).*?$').Groups["year"].Value)</CurrentVersionYear> <CurrentVersionMonth>$([System.Text.RegularExpressions.Regex]::Match($(VersionContent), '*?s*(?&lt;version&gt;d+)s*(S*)?s*((?&lt;year&gt;d{4})/(?&lt;month&gt;d{0,2})/(?&lt;day&gt;d{0,2})).*?$').Groups["month"].Value)</CurrentVersionMonth> <CurrentVersionDay>$([System.Text.RegularExpressions.Regex]::Match($(VersionContent), '*?s*(?&lt;version&gt;d+)s*(S*)?s*((?&lt;year&gt;d{4})/(?&lt;month&gt;d{0,2})/(?&lt;day&gt;d{0,2})).*?$').Groups["day"].Value)</CurrentVersionDay> <CurrentDate>$([System.DateTime]::Today.ToString("MMdd"))</CurrentDate> <AssemblyVersion>$(MajorVersion).$(MinorVersion).$(CurrentVersion).$(CurrentVersionMonth)$(CurrentVersionDay)</AssemblyVersion> </PropertyGroup>
  14. 14. 範例實作(續) <Delete Files="$(VersionTextPath)" /> <Message Text="***************** $(AssemblyVersion)" Importance="high" /> <Exec Command="ATTRIB -R $(ProjectDir)PropertiesVersionInfo.cs" /> <WriteLinesToFile Condition=" '$(AssemblyVersion)' != '' " File="$(ProjectDir)PropertiesVersionInfo.cs" Overwrite="True" Lines="[assembly: System.Reflection.AssemblyVersion(&quot;$(AssemblyVersion)&quot;)] [assembly: System.Reflection.AssemblyFileVersion(&quot;$(AssemblyVersion)&quot;)]" /> </Target> </Project>
  15. 15. 參考資料 http://msdn.microsoft.com/zh-tw/library/dd393574.aspx http://ithelp.ithome.com.tw/question/10107277?tag=ithome.nq

×