lyndsayp
lyndsayp

Reputation: 31

MSBuild meta-properties - how to retrieve a property value where the property name is a composition of other properties?

I'd like to be able to write the below, but I can't in MSBuild:

<Target Name="SetDynamicPropertyValues"> 
   <PropertyGroup>
      <TargetHost>$($(Target-Environment)-Host)</TargetHost>
   </PropertyGroup>
</Target>

This is easily done in NAnt using the property::get-value function. The answer to an earlier question shows an approach using the Condition attribute.

Is there a nicer way to do this?

Upvotes: 3

Views: 299

Answers (1)

Sergio Rykov
Sergio Rykov

Reputation: 4296

MSBuild processes property names once. To do that kind of feature it must call the preprocessing several times. I think it would be better to use a Condition-based approach.

<PropertyGroup>
   <TargetHost Condition="'$(Target-Environment)'=='Env1'">Host_1</TargetHost>
   <TargetHost Condition="'$(Target-Environment)'=='Env2'">Host_2</TargetHost>
   <TargetHost Condition="'$(TargetHost)'==''">DefaultHost</TargetHost>
</PropertyGroup>

Upvotes: 1

Related Questions