MSBuild Conditional Imports

2019-08-06 08:42发布

问题:

Is there a workaround for conditional imports in MSBuild?

I've found evidence here and here detailing a bug in the MSBuild IDE interface. In particular, Import statements do not reload when building:

This is a known limitation. VS will only ever process the tags once, when the project is loaded. So whatever tag is active at the time the project is first loaded (based on whatever values your properties have at that time)... that's the tag that you will get for the lifetime of that project in the IDE

For example, I might want to import the bar or baz project based on the value of foo:

<Import Project="bar.targets" Condition="'$(foo)' == 'bar'" />
<Import Project="baz.targets" Condition="'$(foo)' == 'baz'" />

Is there a workaround or different approach I can use for accomplishing the desired functionality?

回答1:

I don't think that you can overcome this using the conditional import mechaism. What are you really trying to accomplish?

Sayed Ibrahim Hashimi

My Book: Inside the Microsoft Build Engine : Using MSBuild and Team Foundation Build



回答2:

Depends on what is in your targets files, but if you are just setting properties based on the $(foo) property then you could use a prebuild event, or prebuild target to do the same job.