[ACCEPTED]-MSBuild passing parameters to CallTarget-msbuild-target

Accepted answer
Score: 85

MSBuild targets aren't designed to receive 9 parameters. Instead, they use the properties 8 you define for them.

<PropertyGroup>
    <Environment>myValue</Environment>
</PropertyGroup>

<Target Name="Deploy">
    <!-- Use the Environment property -->
</Target>

However, a common scenario 7 is to invoke a Target several times with 6 different parameters (i.e. Deploy several 5 websites). In that case, I use the MSBuild 4 MSBuild task and send the parameters as Properties:

<Target Name="DoDeployments">
    <MSBuild Projects ="$(MSBuildProjectFullPath)"
             Properties="VDir=MyWebsite;Path=C:\MyWebsite;Environment=$(Environment)"
             Targets="Deploy" />

    <MSBuild Projects ="$(MSBuildProjectFullPath)"
             Properties="VDir=MyWebsite2;Path=C:\MyWebsite2;Environment=$(Environment)"
             Targets="Deploy" />
</Target>

$(MSBuildProjectFullPath) is 3 the fullpath of the current MSBuild script 2 in case you don't want to send "Deploy" to 1 another file.

Hope this helps!

Score: 25

You can 'foreach' over an ItemGroup with a target, only 9 you have to do it in declaritive manner. You 8 can even have additional metadata in items, like 7 in the code example:

<ItemGroup>
    <What Include="Dev">
        <How>With bugs</How>
    </What>
    <What Include="Test">
        <How>With tests</How>
    </What>
    <What Include="Chicken">
        <How>Deep fried</How>
    </What>
</ItemGroup>

<Target Name="Deploy">
    <Message Text="@(What), %(How)" />
</Target>

Using an item group 6 as a scalar value @(What) inside a target does 5 the trick, and %(How) references a metadata element 4 in a foreach item.

It's a natural way of 3 doing things in msbuild, for example you 2 can find this pattern everywhere in project 1 files generated with Visual Studio.

Score: 0

There might be a better way to do this in 10 MSBuild, but in Ant, I would use global 9 properties to carry information from one 8 task to the next. It was a lousy solution, but 7 I didn't see a better way at the time. You 6 should be able to do this in MSBuild, but 5 bear in mind that you will need to use the 4 CreateProperty task to dynamically assign a property.

On 3 the other hand, it's pretty easy to implement 2 tasks in C# (or VB or whatever). Maybe that's 1 a better solution for you.

Score: 0
    <CreateProperty
        Value="file1">
        <Output
            TaskParameter="Value"
            PropertyName="filename" />
    </CreateProperty>
    <CallTarget Targets="Deploy"/>
    <Message Text="$(filename)"/>
    <CreateProperty
        Value="file2">
        <Output
            TaskParameter="Value"
            PropertyName="filename" />
    </CreateProperty>
    <Message Text="$(filename)"/>   
    <CallTarget Targets="Deploy"/>

0

More Related questions