Home > Software design >  Add packages only to test projects in a folder using Directory.Build.props
Add packages only to test projects in a folder using Directory.Build.props

Time:01-21

I am already using Directory.Build.props to add certain packages to all the C# projects like this:

<Project>
  <ItemGroup>
    <PackageReference 
      Include="StyleCop.Analyzers" 
      Version="1.2.0-beta.435"
      PrivateAssets="all" 
      Condition="$(MSBuildProjectExtension) == '.csproj'" 
    />
  </ItemGroup>
</Project>

Now I added a Test folder to the solution where I want to store all the tests, the folder tree look like this:

enter image description here

The folder it's a solution folder and appears in the sln file.

How can I create a condition to add xunit and Moq to only the csproj inside that folder?

CodePudding user response:

Following approach only works if Test is a folder on the file system instead of a solution folder:

You can add another Directory.Build.Props into the Test folder and include the parent folder's Directory.Build.Props like this:

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

Since your question is about packages you could also consider Central Package Management as an alternative.

  • Related