【问题标题】:Read build parameters in MSBuild task在 MSBuild 任务中读取构建参数
【发布时间】:2023-03-25 19:11:01
【问题描述】:

我正在尝试创建一些需要在目标项目构建完成后运行脚本的工具。 为此,在我的工具中,我还创建了 MSBuild 任务。

我的问题是我需要知道我的任务中的 ConfigurationName 和 TargetPath 变量是什么。

public class MyTask : Task
{
    public override bool Execute()
    {
        var output = // TargetPath variable
        var configuration = // ConfigurationName variable
        RunScript(output, configuration);
        return true;
    }
}

如何在 MSBuild 任务中读取构建变量?

【问题讨论】:

    标签: c# msbuild


    【解决方案1】:

    最可靠的方法是简单地传递这些属性 将您的任务作为自己的属性:

    public class MyTask : Task
    {
        [Required]
        public string ConfigurationName { get; set; }
        [Required]
        public string TargetPath { get; set; }
    
        public override bool Execute()
        {
            var output = this.TargetPath; // TargetPath variable
            var configuration = this.ConfigurationName; // ConfigurationName variable
            RunScript(output, configuration);
            return true;
        }
    }
    

    您可以将它们声明为“必需”(参见上面的[Required] 属性),也可以不声明。根据您的需要。

    然后从 .targets 或 .*proj 文件中相应地设置它们:

    <MyTask
        Configuration="$(Configuration)"
        TargetPath="$(... whatever ...)"
    />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-25
      • 2014-11-07
      • 2019-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多