Search code examples
msbuildmsbuildcommunitytasks

How do I import the msbuildcommunitytasks project from another msbuild project with a relative file path?


Please go easy I am new to msbuild and msbuildtasks!

How can I set a property which represents a relative file path to a targets file which I want to import? I need relative references so it will work on all dev machines. But the target for import is trying to use the relative file path internally, which won't work as it is re-evaluated relative to the imported target!

Effectively I am trying to work around the documented behaviour of imported projects:

All relative paths in imported projects are interpreted relative to the directory of the imported project. Therefore, if a project file is imported into several project files in different locations, the relative paths in the imported project file will be interpreted differently for each imported project.


Solution

  • Ok, I've found the answer. Essentially you have to set the property MSBuildCommunityTasksPath as a relative path back to the original containing directory.

    For example, given a folder structure like this:

    Root---project---Build---{My msbuild project}
               |
               |-Tools---MSBuildCommunityTasks---{Binaries and Targets}
    
    Where :
    {My msbuild project} is in Root\Project\Build\
    {MSbuildCommunityTasks} is in Root\Project\Tools\MsBuildCommunityTasks

    To get the targets project to reference its binaries via the property MSBuildCommunityTasksPath, it will find the tasks file like this:

    <PropertyGroup>
        <MSBuildCommunityTasksPath>..\MSBuildCommunityTasks\</MSBuildCommunityTasksPath> <!--Relative path back to yourself-->
    </PropertyGroup>
    

    Then you can import the targets file with another relative file reference :

      <Import Project="..\..\Tools\MSBuildCommunityTasks\MsBuild.Community.Tasks.Targets"/>