我需要从正在处理的项目文件中获取msbuild命令行参数(尤其是指定的目标和属性),以便将它们传递给任务的属性。我的msbuild文件使用了许多属性,而我事先不知道哪些属性会被命令行覆盖,因此我正在寻找一种方法,在不手动指定每个属性到任务属性的情况下传递它们。就像bat文件中的$*变量一样。我该如何实现这一点?
这个问题很古老,但是 FWIW 这里是我处理获取 MSBuild 命令行参数的方法:
$([System.Environment]::CommandLine.Trim())
dotnet build
时,这将导致以下错误。
'MSB4185:类型为“System.Environment”的“CommandLine”函数无法用作 MSBuild 属性函数执行。'
using System;
using System.Linq;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
public sealed class GetCommandLineArgs : Task {
[Output]
public ITaskItem[] CommandLineArgs { get; private set; }
public override bool Execute() {
CommandLineArgs = Environment.GetCommandLineArgs().Select(a => new TaskItem(a)).ToArray();
return true;
}
}
<GetCommandLineArgs>
<Output TaskParameter="CommandLineArgs" ItemName="CommandLineArg" />
</GetCommandLineArgs>
<PropertyGroup>
<CommandLineArgs>@(CommandLineArg, ' ')</CommandLineArgs>
<PropertyGroup>