6

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.

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.