I have an Msbuild project and a solution with 20 projects. This needs to compile both under VS and under dotnet cli (w/o special arguments)
Some of the projects are on the root of the solution and others are in sub folders:
SolutionRoot
/Proj1
/Proj2
/Tests
/Proj1Tests
/Proj2Tests
/shared
CommonSettings.target
I have an Imported target file which contains a bunch of rules, GlobalSupressions that are shared among the products:
<Project>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
<PropertyGroup>
<WarningsNotAsErrors>618, 672</WarningsNotAsErrors>
<NoWarn>1701;1702;AD0001;CA5394</NoWarn>
</PropertyGroup>
<ItemGroup>
<PackageReference .../>
</ItemGroup>
<ItemGroup>
<Compile Include="..\shared\GlobalSuppressions.cs" Link="GlobalSuppressions.cs" />
</ItemGroup>
</Project>
However, this will not work for the projects in the subfolders because the path is relative to the loading project. To make it work there, I'd need `....\shared\CommonSettings.target
How can I make this work across sub-folders?
I can make this work in VS by using $(SolutionDir), but for msbuild, I am not sure.
CodePudding user response:
Look into Directory.Build.props and Directory.Build.tragets: https://learn.microsoft.com/en-us/visualstudio/msbuild/customize-your-build?view=vs-2022#directorybuildprops-and-directorybuildtargets
CodePudding user response:
In general, it is a good idea to base relative paths in imported .props
/.targets
files on the location of the .props
/.targets
file itself.
The path will then be independent of the path of the project into which it is imported, e. g.
<Compile Include="$(MSBuildThisFileDirectory)GlobalSuppressions.cs" ... />
That goes for both explicitly imported files and implicitly imported files such as Directory.Build.props
and the like.
Note that those well-known MSBuild location properties such as MSBuildThisFileDirectory
usually already contain a trailing slash.