Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi-level merging of msbuild Directory.Build.props files

Tags:

.net

msbuild

I have this structure:

\
  MySolution.sln
  Directory.Build.props     (1)
  \src
    Directory.Build.props   (2-src)
    \Project1
    \Project2
  \test
    Directory.Build.props   (2-test)
    \Project1Tests
    \Project2Tests

I have common properties for all projects (1), common properties for src projects (2-src), and common properties for test projects (2-test).

For (2-src) and (2-test) to import (1), according to advice given at the repo, I added to each of them:

<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props'))" />

That doesn't work (I get an error that the import causes a circular dependency.). So I tried:

<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '../'))" />

Which also doesn't work for the same reason. But this does work:

<Import Project="../Directory.Build.props" />

I prefer to use the msbuild commands (which support a deeper directory structure), rather than a hardcoded value. Is that possible?

like image 901
grokky Avatar asked Aug 31 '17 06:08

grokky


2 Answers

You can work around it by using the folder name of the current file ($(MSBuildThisFileDirectory)):

  <Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)..\'))" />

This is required because relative paths are interpreted as relative to the project so just using ../ as the second parameter would always be "one up from the csproj" file regardless of the location of the file this statement is in.

like image 70
Martin Ullrich Avatar answered Nov 15 '22 01:11

Martin Ullrich


Worked for me, with a minor improvement:

<PropertyGroup>
  <ParentDirectoryBuildPropsPath>$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)..\'))</ParentDirectoryBuildPropsPath>
</PropertyGroup>

<ImportGroup>
  <Import Project="$(ParentDirectoryBuildPropsPath)" />
</ImportGroup>

Condition accordingly, of course.

like image 38
Michael W. Powell Avatar answered Nov 15 '22 00:11

Michael W. Powell