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 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();
}