现在的位置: 首页 > 综合 > 正文

Windows Services中使用MSBuild实现Build Automation Management

2011年01月26日 ⁄ 综合 ⁄ 共 2537字 ⁄ 字号 评论关闭

Microsoft Build Engine MSBuild)是MicrosoftVisual Studio的新的生成平台。它使得开发人员能够在没有安装Visual Studio 的环境下进行编译。

Windows Service应用程序是一种需要长期运行的应用程序,它没有用户界面,并且也不会产生任何可视化的输出(任何用户消息都会被写入Windows事件日志或者其它自定义的日志中)。因此,它特别适合于服务器环境。在计算机启动时,Windows Service就会自动开始运行(需要设置服务类型为Automatic),它们不需要用户一定登录才行。

Build Automation Management是为了标准化企业的自动化编译过程。通过Windows ServiceMSBuild的结合实现企业的Automation BuildBuild Automation Management可以处理scheduled  Build,也可以处理unscheduled build

创建Windows Service

Windows ServiceAccount设置为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";   

 

获得所需要编译的设置选项。例如,需要编译DebugRelease两个版本。并将这些选项存放在一个临时变量中。    

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

抱歉!评论已关闭.