问题描述
我正在使用MSBuild任务的BuildInParallel属性来并行运行构建项目.根项目正在构建四个子项目.子项目使用自定义MSBuild任务,该任务使用System.Diagnostics.Process启动新进程. 由于某些原因,当UseShellExecute为false时,生成的进程无法正常运行.我不知道为什么会这样,我也不知道错误是什么-发生的只是Process.ExitCode为1,也不例外.
I'm using the BuildInParallel attribute of the MSBuild task to run build projects in parallel. The root project is building four child projects. The child projects are using a custom MSBuild task which starts a new process using System.Diagnostics.Process. For some reason the spawned process fail to run properly when UseShellExecute is false. I've no idea why this is and I can't figure out what the error is - all that happens is the Process.ExitCode is 1, no exception..
这是自定义MSBuild任务:
Here's the custom MSBuild task:
使用系统;
使用Microsoft.Build.Utilities;
使用System.Diagnostics;
公共类MSBuildProcessTask:任务
{
公用字符串可执行{放; }
公共字符串参数{get;放; }
公共重写bool Execute()
{
使用(var p = new Process())
{
试试
{
p.StartInfo =新的ProcessStartInfo(可执行文件,自变量)
{
UseShellExecute = false,
//RedirectStandardOutput = true
};
//p.OutputDataReceived + =(o,e)=>
//{
// if(e.Data!= null)
// {
// Log.LogMessage(MessageImportance.Normal,e.Data);
// }
//};
p.Start();
//p.BeginOutputReadLine();
p.WaitForExit();
}
catch(异常e)
{
扔; //用于设置断点
}
终于
{
如果(p.ExitCode!= 0)
{
Log.LogError("错误. 退出代码:" + p.ExitCode);
}
p.Close();
}
}
返回true;
}
}
using System;
using Microsoft.Build.Utilities;
using System.Diagnostics;
public class MSBuildProcessTask : Task
{
public string Executable { get; set; }
public string Arguments { get; set; }
public override bool Execute()
{
using (var p = new Process())
{
try
{
p.StartInfo = new ProcessStartInfo(Executable, Arguments)
{
UseShellExecute = false,
//RedirectStandardOutput = true
};
//p.OutputDataReceived += (o, e) =>
//{
// if (e.Data != null)
// {
// Log.LogMessage(MessageImportance.Normal, e.Data);
// }
//};
p.Start();
//p.BeginOutputReadLine();
p.WaitForExit();
}
catch (Exception e)
{
throw; // for setting breakpoint
}
finally
{
if (p.ExitCode != 0)
{
Log.LogError("Error. Exit code: " + p.ExitCode);
}
p.Close();
}
}
return true;
}
}
这是MSBuild项目的根文件(Test.proj,实际上我只是在这里构建两个子项目,但仍然会收到错误.):
And here's the root MSBuild project file (Test.proj, actually I'm only building two child project here but still get the error..):
<?xml version ="1.0" encoding ="utf-8" ?>
< Project xmlns ="http://schemas.microsoft.com/developer/msbuild/2003">
< UsingTask TaskName =" MSBuildProcessTask" ;; AssemblyFile ="C:\ Users \ Tom \ Sandbox \ repository_trunk \ MSBuildProcessTask \ MSBuildProcessTask \ bin \ Debug \ MSBuildProcessTask.dll" />
<目标名称=默认">
< ItemGroup>
< ProjectFiles Include ="$(MSBuildProjectDirectory)\ Test.1.proj" />
< ProjectFiles Include ="$(MSBuildProjectDirectory)\ Test.2.proj" />
<!-< ProjectFiles Include =''$(MSBuildProjectDirectory)\ Test.3.proj'' />
< ProjectFiles Include ="$(MSBuildProjectDirectory)\ Test.4.proj" />->
</ItemGroup>
< MSBuild BuildInParallel ="true"; Projects ="@@(ProjectFiles)" />
</Target>
</Project>
<?xml version="1.0" encoding="utf-8" ?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<UsingTask TaskName="MSBuildProcessTask" AssemblyFile="C:\Users\Tom\Sandbox\repository_trunk\MSBuildProcessTask\MSBuildProcessTask\bin\Debug\MSBuildProcessTask.dll" />
<Target Name="Default">
<ItemGroup>
<ProjectFiles Include="$(MSBuildProjectDirectory)\Test.1.proj" />
<ProjectFiles Include="$(MSBuildProjectDirectory)\Test.2.proj" />
<!--<ProjectFiles Include="$(MSBuildProjectDirectory)\Test.3.proj" />
<ProjectFiles Include="$(MSBuildProjectDirectory)\Test.4.proj" />-->
</ItemGroup>
<MSBuild BuildInParallel="true" Projects="@(ProjectFiles)" />
</Target>
</Project>
这是子项目文件(Test.1.proj)的示例:
And here's an example of the child project files (Test.1.proj):
<?xml version ="1.0" encoding ="utf-8" ?>
< Project xmlns ="http://schemas.microsoft.com/developer/msbuild/2003">
< UsingTask TaskName =" MSBuildProcessTask" ;; AssemblyFile ="C:\ Users \ Tom \ Sandbox \ repository_trunk \ MSBuildProcessTask \ MSBuildProcessTask \ bin \ Debug \ MSBuildProcessTask.dll" />
<目标名称=默认">
< Message Text ="Hello" />
< MSBuildProcessTask Executable ="sqlcmd";参数=-S.\ SQLEXPRESS -Q& quot; SELECT @@ VERSION"". />
</Target>
</Project>
<?xml version="1.0" encoding="utf-8" ?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<UsingTask TaskName="MSBuildProcessTask" AssemblyFile="C:\Users\Tom\Sandbox\repository_trunk\MSBuildProcessTask\MSBuildProcessTask\bin\Debug\MSBuildProcessTask.dll" />
<Target Name="Default">
<Message Text="Hello" />
<MSBuildProcessTask Executable="sqlcmd" Arguments="-S .\SQLEXPRESS -Q "SELECT @@VERSION"" />
</Target>
</Project>
我的命令行是:C:\ Windows \ Microsoft.NET \ Framework \ v4.0.30319 \ MSBuild.exe/m/nr:false C:\ Users \ Tom \ Sandbox \ repository_trunk \ MSBuildProcessTask \ MSBuild ProcessTask \ Test.proj
My command line is: C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe /m /nr:false C:\Users\Tom\Sandbox\repository_trunk\MSBuildProcessTask\MSBuild ProcessTask\Test.proj
这是示例输出:
C:\ Users \ Tom> C:\ Windows \ Microsoft.NET \ Framework \ v4.0.30319 \ MSBuild.exe/m/nr:false C:\ Users \ Tom \ Sandbox \ repository_trunk \ MSBuildProcessTask \ MSBuildPr
ocessTask \ Test.proj
Microsoft(R)Build Engine版本4.0.30319.1
[Microsoft .NET Framework版本4.0.30319.235]
版权所有(C)Microsoft Corporation2007.保留所有权利.
构建从2011年8月16日开始22:22:06.
1>项目"C:\ Users \ Tom \ Sandbox \ repository_trunk \ MSBuildProcessTask \ MSBuildProcessTask \ Test.proj"在节点1上(默认目标).
1>项目"C:\ Users \ Tom \ Sandbox \ repository_trunk \ MSBuildProcessTask \ MSBuildProcessTask \ Test.proj" (1)正在构建"C:\ Users \ Tom \ Sandbox \ reposito
ry_trunk \ MSBuildProcessTask \ MSBuildProcessTask \ Test.1.proj"(2)在节点1上(默认目标).
2>默认值:
你好
1>项目"C:\ Users \ Tom \ Sandbox \ repository_trunk \ MSBuildProcessTask \ MSBuildProcessTask \ Test.proj" (1)正在构建"C:\ Users \ Tom \ Sandbox \ reposito
ry_trunk \ MSBuildProcessTask \ MSBuildProcessTask \ Test.2.proj"(3)在节点2(默认目标)上.
-------------------------------------------------- -------------------------------------------------- --------------------------------------------------
-------------------------------------------------- -------------------------------------------------- --------------------------------------------------
Microsoft SQL Server 2008(SP1)-10.0.2531.0(X64)
2009年3月29日10:11:52
版权所有(c)1988-2008 Microsoft Corporation
Windows NT 6.1< X64>上的Express Edition(64位) (内部版本7601:Service Pack 1)
(受影响的1行)
3>默认值:
你好
2>完成的建筑项目"C:\ Users \ Tom \ Sandbox \ repository_trunk \ MSBuildProcessTask \ MSBuildProcessTask \ Test.1.proj" (默认目标).
3> C:\ Users \ Tom \ Sandbox \ repository_trunk \ MSBuildProcessTask \ MSBuildProcessTask \ Test.2.proj(6,3):错误:错误.退出代码:1
3>完成的建筑项目"C:\ Users \ Tom \ Sandbox \ repository_trunk \ MSBuildProcessTask \ MSBuildProcessTask \ Test.2.proj" (默认目标).
1>完成的建筑项目"C:\ Users \ Tom \ Sandbox \ repository_trunk \ MSBuildProcessTask \ MSBuildProcessTask \ Test.proj" (默认目标).
构建成功.
"C:\ Users \ Tom \ Sandbox \ repository_trunk \ MSBuildProcessTask \ MSBuildProcessTask \ Test.proj" (默认目标)(1)->
"C:\ Users \ Tom \ Sandbox \ repository_trunk \ MSBuildProcessTask \ MSBuildProcessTask \ Test.2.proj" (默认目标)(3)->
(默认目标)->
C:\ Users \ Tom \ Sandbox \ repository_trunk \ MSBuildProcessTask \ MSBuildProcessTask \ Test.2.proj(6,3):错误:错误.退出代码:1
0警告
1个错误
时间已过00:00:00.23
C:\ Users \ Tom>
C:\Users\Tom>C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe /m /nr:false C:\Users\Tom\Sandbox\repository_trunk\MSBuildProcessTask\MSBuildPr
ocessTask\Test.proj
Microsoft (R) Build Engine Version 4.0.30319.1
[Microsoft .NET Framework, Version 4.0.30319.235]
Copyright (C) Microsoft Corporation 2007. All rights reserved.
Build started 16/08/2011 22:22:06.
1>Project "C:\Users\Tom\Sandbox\repository_trunk\MSBuildProcessTask\MSBuildProcessTask\Test.proj" on node 1 (default targets).
1>Project "C:\Users\Tom\Sandbox\repository_trunk\MSBuildProcessTask\MSBuildProcessTask\Test.proj" (1) is building "C:\Users\Tom\Sandbox\reposito
ry_trunk\MSBuildProcessTask\MSBuildProcessTask\Test.1.proj" (2) on node 1 (default targets).
2>Default:
Hello
1>Project "C:\Users\Tom\Sandbox\repository_trunk\MSBuildProcessTask\MSBuildProcessTask\Test.proj" (1) is building "C:\Users\Tom\Sandbox\reposito
ry_trunk\MSBuildProcessTask\MSBuildProcessTask\Test.2.proj" (3) on node 2 (default targets).
------------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------------
Microsoft SQL Server 2008 (SP1) - 10.0.2531.0 (X64)
Mar 29 2009 10:11:52
Copyright (c) 1988-2008 Microsoft Corporation
Express Edition (64-bit) on Windows NT 6.1 <X64> (Build 7601: Service Pack 1)
(1 rows affected)
3>Default:
Hello
2>Done Building Project "C:\Users\Tom\Sandbox\repository_trunk\MSBuildProcessTask\MSBuildProcessTask\Test.1.proj" (default targets).
3>C:\Users\Tom\Sandbox\repository_trunk\MSBuildProcessTask\MSBuildProcessTask\Test.2.proj(6,3): error : Error. Exit code: 1
3>Done Building Project "C:\Users\Tom\Sandbox\repository_trunk\MSBuildProcessTask\MSBuildProcessTask\Test.2.proj" (default targets).
1>Done Building Project "C:\Users\Tom\Sandbox\repository_trunk\MSBuildProcessTask\MSBuildProcessTask\Test.proj" (default targets).
Build succeeded.
"C:\Users\Tom\Sandbox\repository_trunk\MSBuildProcessTask\MSBuildProcessTask\Test.proj" (default target) (1) ->
"C:\Users\Tom\Sandbox\repository_trunk\MSBuildProcessTask\MSBuildProcessTask\Test.2.proj" (default target) (3) ->
(Default target) ->
C:\Users\Tom\Sandbox\repository_trunk\MSBuildProcessTask\MSBuildProcessTask\Test.2.proj(6,3): error : Error. Exit code: 1
0 Warning(s)
1 Error(s)
Time Elapsed 00:00:00.23
C:\Users\Tom>
如您所见,我们仅从SQLCMD命令之一获得输出.问题在于新进程似乎并不总是在运行,并且不会产生任何错误消息(仅ExitCode 1).
As you can see we only get output from one of the SQLCMD commands. The problem is that the new process doesn't always seem to run, and doesn't produce any error message (only ExitCode 1).
推荐答案
谢谢您的问题.
我是试图让熟悉此主题的人进一步研究此问题.可能会有一些时间延迟.感谢您的耐心.
谢谢为您的理解和支持.
这篇关于MSBuild BuildInParallel,自定义任务生成过程无法运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!