如何从正在处理的项目文件中访问msbuild命令行参数? [英] How to access the msbuild command line parameters from within the project file being processed?

查看:91
本文介绍了如何从正在处理的项目文件中访问msbuild命令行参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to get access to the msbuild command line parameters (the specified targets and properties in particular) from within the project file being processed in order to pass them down to the Properties of an <MSBuild> task.

我的msbuild文件使用了大量属性,并且我不知道提前通过命令行覆盖哪些属性,因此我正在寻找一种方法来向下传递这些属性,而无需手动指定每个属性. < MSBuild>的属性任务. bat文件中的$ *变量之类的东西.

My msbuild file uses a large number of properties, and I don't know ahead of time which ones will be overridden via the command line, so I'm looking for a way to pass these down without specifying each one manually to the Properties of the <MSBuild> task. Something like the $* variable in a bat file.

我该怎么做?

推荐答案

这个问题很古老,但是FWIW在这里是我如何处理获取MSBuild命令行参数的方法:

This question is ancient, but FWIW here is how I have handled getting the MSBuild command line parameters:

$([System.Environment]::CommandLine.Trim())

问题在于,使用dotnet build时,这将导致以下错误.

The problem is that this will cause the following error when using dotnet build.

'MSB4185:类型为"System.Environment"的函数"CommandLine"为 不能作为MSBuild属性函数执行.'

'MSB4185: The function "CommandLine" on type "System.Environment" is not available for execution as an MSBuild property function.'

选项2(FTW)

创建任务

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>

这篇关于如何从正在处理的项目文件中访问msbuild命令行参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆