How to pass Special Characters to MsBuild

Some time, we want to pass parameter including Special Characters to Msbuild, for example,

 <PropertyGroup>
      <CAR>+!Microsoft.Performance#CA1812;+!Microsoft.Performance#CA1822</CAR>
      <CustomPropertiesForBuild>
        $(CustomPropertiesForBuild);CodeAnalysisRules=$(CAR)
      </CustomPropertiesForBuild>
  </PropertyGroup>

 

If the value is hardcode, it will work well. But when the property “CAR” is an output of a task, it will not work. For example,

<Target Name="BeforeCompileSolution">
    <MyTask>
      <Output TaskParameter="OutputList" PropertyName="CodeAnalysisRules" />
    </MyTask>
    <Message Text="CodeAnalysisRules=$(CodeAnalysisRules)"></Message>
    <PropertyGroup>
      <CustomPropertiesForBuild>CodeAnalysisRules=$(CodeAnalysisRules)</CustomPropertiesForBuild>
    </PropertyGroup>
    <Message Text="CustomPropertiesForBuild=$(CustomPropertiesForBuild)"></Message>
</Target>

We set the value of “OutputList” (type: String)to “+!Microsoft.Performance#CA1812;+!Microsoft.Performance#CA1822” in the dll. but the value that get in MSbuild is “CodeAnalysisRules=+!Microsoft.Performance#CA1812%3b+!Microsoft.Performance#CA1822” though <Message> Task show the correct value.

 

To Fix this issue, we can use itemgroup instead of property to get/set the value  of CodeAnalysisRules.

Fist: in your code, using ITaskItem[] as output.
For Example :

public class MyTask : Task
    {
        [Output]
        public ITaskItem[] OutputList { get; set; }
        public override bool Execute()
        {
            var list = new List<ITaskItem>();
            list.Add(new TaskItem("+!Microsoft.Performance#CA1812"));
            list.Add(new TaskItem("+!Microsoft.Performance#CA1822"));
            OutputList = list.ToArray();
            return true;
        }
    }

Second: In TFSBuild.proj, use ItemName instead of PropertyName.

<Target Name="BeforeCompileSolution">
   
    <MyTask>
      <Output TaskParameter="OutputList" ItemName="CodeAnalysisRules" />
    </MyTask>
    <Message Text="CodeAnalysisRules=@(CodeAnalysisRules)"></Message>
    <PropertyGroup>
      <CustomPropertiesForBuild>CodeAnalysisRules=@(CodeAnalysisRules)</CustomPropertiesForBuild>
    </PropertyGroup>
    <Message Text="CustomPropertiesForBuild=$(CustomPropertiesForBuild)"></Message>
  </Target>

 

Now, MSBuild will recognize it correctly.

CodeAnalysisRules=+!Microsoft.Performance#CA1812%3b+!Microsoft.Performance#CA1822

posted on 2009-10-26 13:52  Ruiz  阅读(411)  评论(0编辑  收藏  举报

导航