1

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?

3

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.

  • I love that it's simple to achieve! I've gone years and years without ever using props files. I'll give it a go right away. – Matt DiTrolio Mar 21 '18 at 18:12
  • @MattDiTrolio, I too created all sorts of complex work-arounds to this until I discovered props files. I don't understand why they aren't more heavily publicised and documented by MS as they are so useful. Glad they helped you too. – David Arno Mar 21 '18 at 18:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.