Skip to content

Commit 9f968f5

Browse files
authored
Merge pull request #262 from grokky1/master
Added multi-level scanning/merging use case
2 parents bc9ee59 + 22ca3a8 commit 9f968f5

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

docs/msbuild/customize-your-build.md

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ translation.priority.ht:
2323
- "fr-fr"
2424
- "it-it"
2525
- "ja-jp"
26-
- "ko-kr"
27-
- "pl-pl"
26+
- "ko-kr" - "pl-pl"
2827
- "pt-br"
2928
- "ru-ru"
3029
- "tr-tr"
@@ -70,6 +69,39 @@ Directory.Build.props is imported very early in Microsoft.Common.props, so prope
7069

7170
Directory.Build.targets is imported from Microsoft.Common.targets after importing .targets files from NuGet packages. So, it can be used to override properties and targets defined in most of the build logic, but at times it may be necessary to do customizations within the project file after the final import.
7271

72+
## Use case: multi-level merging
73+
74+
Suppose you have this standard solution structure:
75+
76+
````
77+
\
78+
MySolution.sln
79+
Directory.Build.props (1)
80+
\src
81+
Directory.Build.props (2-src)
82+
\Project1
83+
\Project2
84+
\test
85+
Directory.Build.props (2-test)
86+
\Project1Tests
87+
\Project2Tests
88+
````
89+
90+
It might be desirable to have common properties for all projects `(1)`, common properties for `src` projects `(2-src)`, and common properties for `test` projects `(2-test)`.
91+
92+
For msbuild to correctly merge the "inner" files (`2-src` and `2-test`) with the "outer" file (`1`), you must take into account that once msbuild finds a `Directory.Build.props` file, it stops further scanning. To continue scanning, and merge into the outer file, place this into both inner files:
93+
94+
`<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props'))" />`
95+
96+
A summary of msbuild's general approach is as follows:
97+
98+
- For any given project, msbuild finds the first `Directory.Build.props` upward in the solution structure, merges it with defaults, and stops scanning for more
99+
- If you want multiple levels to be found and merged, then [`<Import...>`](http://docs.microsoft.com/en-us/visualstudio/msbuild/property-functions#msbuild-getpathoffileabove) (shown above) the "outer" file from the "inner" file
100+
- If the "outer" file does not itself also import something above it, then scanning stops there
101+
- To control the scanning/merging process, use `$(DirectoryBuildPropsPath)` and `$(ImportDirectoryBuildProps)`
102+
103+
Or more simply: the first `Directory.Build.props` which doesn't import anything, is where msbuild stops.
104+
73105
## See Also
74106
[MSBuild Concepts](../msbuild/msbuild-concepts.md)
75107
[MSBuild Reference](../msbuild/msbuild-reference.md)

0 commit comments

Comments
 (0)