MSBuild import project dynamically

In my .csproj, I would like to import the .target file depending on the path computed from the 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 this for a different purpose, but it does not work, since the import is called before the target evaluation.

+6
source share
2 answers

You cannot call a target before importing goals, but you can still dynamically generate a path to import from a group of properties.

Visual Studio does this when creating 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" /> 

This way you can 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.

+3
source

No,
first MSBuild imports all the "extensions", then builds a dependency graph and finally runs the tasks

0
source

Source: https://habr.com/ru/post/958007/


All Articles