Home Maintaining AssemblyInfo for multiple projects
Post
Cancel

Maintaining AssemblyInfo for multiple projects

When you are developing an application or manly a NuGet package, you might want to keep the same AssemblyInfo for all your packages, which involves updating each project when you want to publish a new version of your package according to the SemVer convention [Semantic Versioning 2.0.0Semantic Versioning](https://semver.org/).
In order to make it easier, with the improvements of dotnet core and the new csproj syntax, which I strongly recommend, MSBuild 15 introduced a pretty cool feature: Solution-wide project properties with Directory.Build.props [Customize your build - Visual StudioMicrosoft Docs](https://docs.microsoft.com/en-us/visualstudio/msbuild/customize-your-build?view=vs-2017). Basically, this allows you to define certain properties and use them in all your project in a centralised way, so you don’t have to update your projects one by one.

All you have to do is create a new text file named Directory.Build.props and place it where you have your solution file. Here is an example of the properties you can use:

1
2
3
4
5
6
7
8
9
<Project>
  <PropertyGroup>
    <Version>1.1.0</Version>
    <FileVersion>1.1.0</FileVersion>
    <Product>Fenergo.Platform.Common</Product>
    <Company>Fenergo</Company>
    <Copyright>Copyright © Fenergo 2018</Copyright>
  </PropertyGroup>
</Project>‍‍‍‍‍‍‍‍‍

In addition, you can inherit another funcionalities such as analysers. For example, if you want to use the Stylecop.Analyzers for all your project, you can add a Directory.Build.props like this:

1
2
3
4
5
6
7
8
9
10
11
<Project>
  <PropertyGroup>
    <TreatWarningsAsErrors>True</TreatWarningsAsErrors>
  </PropertyGroup>
  <ItemGroup>
    <AdditionalFiles Include="..\\StyleCop.json" />
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="StyleCop.Analyzers" Version="1.0.2" />
  </ItemGroup>
</Project>

If you don’t have your Directory.Build.props in your root directory, you can import it in the projects which you need it.

1
2
3
4
5
6
7
<Project>
  <!-- Import parent Directory.build.props -->
  <Import Project="../Directory.Build.props" />

  <!-- Properties common to all test projects -->
  <!-- ... -->
</Project>

I hope you found this post helpful,

Happy coding!

Alberto.

This post is licensed under CC BY 4.0 by the author.