Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MSBuild import project dynamically

Tags:

msbuild

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.

like image 552
Ldoppea Avatar asked Nov 13 '13 15:11

Ldoppea


2 Answers

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.

like image 115
Chris Eldredge Avatar answered Nov 19 '22 20:11

Chris Eldredge


No,
firstly MSBuild import all "extensions", then build dependency graph, and finally run the tasks

like image 43
Alex Zharnasek Avatar answered Nov 19 '22 20:11

Alex Zharnasek