我需要从正在处理的项目文件中访问msbuild命令行参数(特别是指定的目标和属性),以便将它们传递给<MSBuild>任务的属性。
我的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>