On my .csproj I would like to import a .target file depending on a path calculated from a task.
Is it possible to do something like this?
<PropertyGroup>
<TargetPath>/*Some calculation from task*/</TargetPath>
</PropertyGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(TargetPath)\Custom.targets" />
I tried to do it from another target, but it doesn't work as the import is called before the target evaluation.
You cannot invoke a target before targets are imported, but you can still dynamically generate a path to import from a property group.
Visual Studio does this when you create a Web project, as in this example from one of my projects:
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">12.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
<Import Project="$(VSToolsPath)\WebApplications\Microsoft.WebApplication.targets" Condition="'$(VSToolsPath)' != ''" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v12.0\WebApplications\Microsoft.WebApplication.targets" />
So you could definitely define properties using conditions:
<PropertyGroup>
<ImportPath Condition="Exists('path\to\some\thing.targets')">path\to\some\thing.targets</ImportPath>
</PropertyGroup>
<Import Project="$(ImportPath)" Condition=" '$(ImportPath)' != '' "/>
Microsoft.Bcl.Build does this, so you can too.
No,
firstly MSBuild import all "extensions", then build dependency graph, and finally run the tasks
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With