How to access the msbuild command line parameters from within the project file being processed?

前端 未结 1 1840
我寻月下人不归
我寻月下人不归 2021-02-08 18:29

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 th

相关标签:
1条回答
  • 2021-02-08 19:16

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

    Option 1 (not recommended)

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

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

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

    Option 2 (FTW)

    Create a Task

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

    Use the Task to create an Item for each argument

    <GetCommandLineArgs>
      <Output TaskParameter="CommandLineArgs" ItemName="CommandLineArg" />
    </GetCommandLineArgs>
    

    Optionally, reconstruct the arguments into a single string

    <PropertyGroup>
      <CommandLineArgs>@(CommandLineArg, ' ')</CommandLineArgs>
    <PropertyGroup>
    
    0 讨论(0)
提交回复
热议问题