How to Share Common NuGet Package Settings Between Projects (csproj 2017)

netnuget

I would like to share common NuGet package values between projects in the same solution, such as Author, Company, Copyright, and Product.

Previously (using the 2015 project format), I would use a combination of AssemblyInfo.cs and a shared (linked) GlobalAssemblyInfo.cs that contained all the common settings. Now that I'm using the 2017 csproj format and the dotnet CLI, I'd like replace that older technique with one that better fits the current tools. However, my problem stems from the fact that each setting now resides directly inside the csproj file, making it difficult to separate out common values.

How might one do this?

Best Answer

That's simple to achieve: use a Directory.Build.props file in the solution folder and put those values in there. It takes the following format (just like the new .csproj files):

<Project>
    <PropertyGroup>
        <Version>3.1.0</Version>
        <Company>Me</Company>
        <TreatWarningsAsErrors>True</TreatWarningsAsErrors>
        <RepositoryUrl>https://github.com/...</RepositoryUrl>
        <RepositoryType>git</RepositoryType>
        <PackageLicenseUrl>https://raw.githubusercontent.com/...</PackageLicenseUrl>
        ...
    </PropertyGroup>
</Project>

By default, MSBuild looks for Directory.Build.props files in all the parent directories from the project's location and adds those to the project configuration on build.

Related Topic