Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify assembly version number as a command line argument in MSBuild

I would like to be able to specify the version number for all assemblies to be generated during a build as a MSBuild command argument like this:

MSBuild.exe /p:version=5.4.3.0  

I have looked over AssemblyInfoTask but it does not seem to me like a good solution in this case.

like image 354
Radu M. Avatar asked Dec 09 '11 14:12

Radu M.


People also ask

How use MSBuild command line?

Use MSBuild at a command prompt To run MSBuild at a command prompt, pass a project file to MSBuild.exe, together with the appropriate command-line options. Command-line options let you set properties, execute specific targets, and set other options that control the build process.

How do I check MSBuild version?

There is no way to determine which version of MSBuild was used. There is no days in the executable that says which version was used and nothing in them specific to MSBuild to use as a trail of breadcrumbs to determine it either. You dont even need MSBuild to build an executable.

How do I set environment variables in MSBuild?

Click on System and Security and then on System. In the left pane, click on Advanced system settings. At the very bottom of the pop up, click on Environment Variables. Edit the Path variable and append the folder's path that contains the MSBuild.exe to it (e.g., ;C:\Windows\Microsoft.NET\Framework64\v4.

How do I change the version number in Visual Studio?

Open the project's Property Pages dialog box. For details, see Set C++ compiler and build properties in Visual Studio. Select the Configuration Properties > Linker > General property page. Modify the Version property.


1 Answers

For SDK-style projects that are built using dotnet.exe, assembly version attributes are generated automatically, so you can use /p:Version=5.4.3.0 right out of the box.

If you use the old project format, you need to add the following BeforeBuild step to your .csproj file. No need to use extra .targets and extension packs, because MSBuild already has a nice built-in task which does most of the stuff:

<Target Name="BeforeBuild">   <ItemGroup>     <AssemblyAttributes Include="AssemblyVersion">       <_Parameter1>$(Version)</_Parameter1>     </AssemblyAttributes>   </ItemGroup>   <MakeDir Directories="$(IntermediateOutputPath)" />   <WriteCodeFragment Language="C#"                      OutputFile="$(IntermediateOutputPath)Version.cs"                      AssemblyAttributes="@(AssemblyAttributes)" />   <ItemGroup>     <Compile Include="$(IntermediateOutputPath)Version.cs" />   </ItemGroup> </Target> 

Just make sure you remove the existing AssemblyVersion attribute because it will now be generated during build.

Update 7/29/2020: Michael Parker has pointed out that if you use this approach and do a build from Visual Studio, you end up with an empty version in the Version.cs file. To overcome this, I suggest defining the default Version value in your .csproj or Directory.Build.props file as follows:

<PropertyGroup>   ...   <Version Condition="'$(Version)' == ''">1.0.0.0</Version> </PropertyGroup> 

This will set it to 1.0.0.0 if Version wasn't specified in the command line.

like image 153
Yarik Avatar answered Sep 22 '22 08:09

Yarik