Add nuget packages to every project
Jan 11, 2024
Sometimes you want a package added to every dotnet project. One example might be static analysers that protect you from misusing asynchronous libraries.
You can achieve this by adding a Directory.Build.targets file to the root of the solution:
<Project>
<Choose>
<When Condition="$(OutputType) == 'Exe' AND $(MSBuildProjectName.EndsWith('Tests')) == false">
<ItemGroup>
<PackageReference Include="NewRelic.Agent" Version="10.12.1" />
<PackageReference Include="NewRelic.Agent.Api" Version="10.12.1" />
<PackageReference Include="NewRelic.LogEnrichers.Serilog" Version="1.2.0" />
</ItemGroup>
</When>
</Choose>
</Project>
The example shows how you can use conditional logic to avoid adding the libraries to every project. Here we only want to add them to executables and certainly not unit test projects.