0

I am trying to automatically update the Version element in a .csproj file before the project is compiled so that the outputted dll has that version number.

My requirements:

  1. Only run on release build
  2. Only run if project is actually being built (not skipped via incremental build processing)

Currently, I am stuck with the version output to the file being one version behind, as if the compilation/build process read my .csproj before my MSBuild target ran and ignored my updates.

I have the following:

  <PropertyGroup>
    <VersionPrefix>1.0.20</VersionPrefix>
    <VersionSuffix>03412bb</VersionSuffix>
  </PropertyGroup>
  <Target Name="ReleaseVersion" Condition="'$(Configuration)' == 'Release'" BeforeTargets="BeforeBuild">
    <Exec Command="{snip of command that updates/saves VersionPrefix/Suffix of this file}" />
  </Target>

Is there a better Target to use for the BeforeTargets value? Is there a way I can execute the command to return information that then sets a variable for msbuild to use when setting the Product version attribute of the generated dll?

4
  • Does this logic need to be within the csproj file? How are you compiling the application, with msbuild/dotnet cli? Commented Sep 27, 2023 at 20:23
  • Doesn't have to be written to csproj. Really only need the Product version set appropriately. And I am using the dotnet build command via tasks.json. But I that uses MSBuild right? Commented Sep 27, 2023 at 20:30
  • 1
    What about something like this? stackoverflow.com/a/53067759/1043380 Commented Sep 27, 2023 at 20:34
  • Pointed me in right direction. Using Directory.Build.props. Thanks. Commented Sep 27, 2023 at 22:07

1 Answer 1

0

@gunr2171 pointed me in the right direction pointing out the comment on Setting the version number for .NET Core projects.

All I was really trying to do was append the current git commit hash to the end of the file version via VersionSuffix.

Using /p:InformationalVersion="1.2.3-qa" in the dotnet cli had two immediate problems (that may be overcame)

  1. The version was going to be dynamic (by looking up the last commit hash) so I was going to have to run some sort of task to get this and then inject it into this command line.
  2. When building 'Site A' that uses 'Assembly A' and 'Assembly B' I needed to make sure that if Assembly A or B were going to build, I had to get their versions updated as well, so I didn't see how a pure 'dotnet cli' solution would work resulting in me having to use MSBuild tasks - which I had already failed at as the question indicated.

However, the next answer in the linked question discusses the Directory.Build.props file/feature. And this works out perfect for my use case.

In my 'publish' task, I first call my 'executable' that will detect the current commit for the current project and all its dependencies (obviously one behind if executed while there are files staged or in the working directory). Then, I generate/update each Directory.Build.props (located in same folder as *.csproj) with the latest commit hash.

The resulting Directory.Build.props file is displayed below.

<Project>
  <PropertyGroup>
    <VersionSuffix>919022d</VersionSuffix>
  </PropertyGroup>
</Project>

Since I generate/update this every time I publish and it is kind of like a 'code gen' file that doesn't need to be tracked in the repository, I added this file to the .gitignore file so that when I do a publish, if any Directory.Build.props files are generated/updated it doesn't require a new commit.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.