Microsoft Build Engine (MSBuild)是Microsoft和Visual Studio的新的產生平台。它使得開發人員能夠在沒有安裝Visual Studio 的環境下進行編譯。
Windows Service應用程式是一種需要長期啟動並執行應用程式,它沒有使用者介面,並且也不會產生任何可視化的輸出(任何使用者訊息都會被寫入Windows事件記錄或者其它自訂的日誌中)。因此,它特別適合於伺服器環境。在電腦啟動時,Windows Service就會自動開始運行(需要設定服務類型為Automatic),它們不需要使用者一定登入才行。
Build Automation Management是為了標準化企業的自動化編譯過程。通過Windows Service與MSBuild的結合實現企業的Automation Build。Build Automation Management可以處理scheduled Build,也可以處理unscheduled build。
建立Windows Service
將Windows Service的Account設定為User。其它賬戶可能無權使用Proces。
在Windows Service中添加Build Code。
MSBuild.exe位於%SystemRoot%\Micorsoft .NET\Framework\V2.0.502727目錄下。因此,擷取該目錄所謂的絕對路徑。
string systemRoot = Path.GetDirectoryName(Environment.SystemDirectory);
string FrameworkPath = systemRoot + @"\Microsoft.NET\Framework";
if (!Directory.Exists(FrameworkPath))
{
throw new DirectoryNotFoundException("The framework directory could not be located.");
}
string msBuildDir = FrameworkPath + @"\v2.0.50727";
獲得所需要編譯的設定選項。例如,需要編譯Debug和Release兩個版本。並將這些選項存放在一個臨時變數中。
string[] configs = new string[] { "Debug", "Release", };
獲得項目路徑並調用MSBuild進行編譯。
string projectPath = @"e:\temp\test\test.sln";
eventLog1.WriteEntry("Start Build");
foreach (string str in configs)
{
Process process = new Process();
process.StartInfo.FileName = msBuildDir + @"\msbuild.exe";
process.StartInfo.Arguments = projectPath + @" /p:Configuration=" + str + "\"";
Console.WriteLine(process.StartInfo.Arguments);
process.Start();
}
完整代碼如下:
string systemRoot = Path.GetDirectoryName(Environment.SystemDirectory);
string FrameworkPath = systemRoot + @"\Microsoft.NET\Framework";
if (!Directory.Exists(FrameworkPath))
{
throw new DirectoryNotFoundException("The framework directory could not be located.");
}
string msBuildDir = FrameworkPath + @"\v2.0.50727";
string[] configs = new string[] { "Debug", "Release};
string projectPath = @"e:\temp\test\test.sln";
eventLog1.WriteEntry("Start Build");
foreach (string str in configs)
{
Process process = new Process();
process.StartInfo.FileName = msBuildDir + @"\msbuild.exe";
process.StartInfo.Arguments = projectPath + @" /p:Configuration=" + str + "\"";
Console.WriteLine(process.StartInfo.Arguments);
process.Start();
}