Search code examples
.netmsbuilditaskitem

How to pass data to an ITaskItem property of an MSBuild task?


i have a custom Task which i use in MSBuild. Works great. Previously, i had some properties which accepted some string data. It was suggested I should change them to ITaskItem's. This way, if i have spaces, i shouldn't have a problem.

Previous code

public class CompressorTask : Task
    {
    ....
    public string CssFiles { get; set; }
    public string JavaScriptFiles { get; set; }
}

example msbuild file (eg. MsBuildSample.xml)...

<CompressorTask
    CssFiles="StylesheetSample1.css, StylesheetSample2.css, 
              StylesheetSample3.css, StylesheetSample4.css"
    JavaScriptFiles="jquery-1.2.6-vsdoc.js"
... />

Notice how i've got 4 css files? i manually extracted them by the , or space delimeter. Kewl.

New Code

 public ITaskItem[] CssFiles { get; set; }
 public ITaskItem[] JavaScriptFiles { get; set; }

Now, i'm not sure what values i need to set, in the CssFiles property, in the MSBuild file.

any suggestions?


Solution

  • I think you now need to put the files into an itemlist with the name of the property:

    <ItemGroup>
        <CssFiles Include="StylesheetSample1.css"/>
        <CssFiles Include="StylesheetSample2.css"/>
        <CssFiles Include="StylesheetSample3.css"/>
        <CssFiles Include="StylesheetSample4.css"/>
        <!-- or <CssFiles Include="*.css" /> -->
        <JavaScriptFiles Include="jquery-1.2.6-vsdoc.js"/>
    </ItemGroup>
    <CompressorTask CssFiles="@(CssFiles)" JavaScriptFiles="@(JavaScriptFiles)"/>