Question

I am trying to get MSBuild to automatically update a project's AssemblyVersion and AssemblyFileVersion.

Specifically, just the build and revision number.

So, if my source file starts out as

[assembly: AssemblyVersion("1.2.3.4")]

and my build and revision numbers are 13 and 14, I want this to become:

[assembly: AssemblyVersion("1.2.13.14")]

However, I am not getting the expected outcome. Instead, I am getting something like this - presumably because it is the 28th of November at the time I did the build.

[assembly: AssemblyVersion("1.0.1128.01")]

Relevant info from my .csproj file below.

  <Import Project="$(MSBuildExtensionsPath64)\ExtensionPack\4.0\MSBuild.ExtensionPack.tasks"/>
  <Import Project="$(MSBuildExtensionsPath64)\ExtensionPack\4.0\MSBuild.ExtensionPack.VersionNumber.targets"/>
  <PropertyGroup>
    <BuildDependsOn>
      AssemblyVersion;
      $(BuildDependsOn)
    </BuildDependsOn>
  </PropertyGroup>
  <ItemGroup>
    <AssemblyVersionFiles Include="$(MSBuildProjectDirectory)\Properties\AssemblyInfo.cs"/>
  </ItemGroup>
  <Target Name="AssemblyVersion" Inputs="@(AssemblyVersionFiles)" Outputs="UpdatedAssemblyVersionFiles">
    <Message Text="Setting AssemblyVersion..." Importance="High" />
    <AssemblyInfo
      AssemblyInfoFiles="%(AssemblyVersionFiles.FullPath)"
      AssemblyBuildNumber="12"
      AssemblyBuildNumberType="NoIncrement"
      AssemblyRevision="13"
      AssemblyRevisionType="NoIncrement"
      />
  </Target>

At this point I'm just about ready to scrap the extensions altogether, split my assembly and file versions out into two separate files, and use powershell to read and overwrite their values. Might be a more desireable solution as it'll only be able to take place on the build machine so I won't have to worry about developers getting odd build results if they don't have the expansion pack installed.

Still, I don't like to be beaten, so I'd love to find out how to make this work.

Any thoughts?


Full .csproj file below:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">x86</Platform>
    <ProductVersion>8.0.30703</ProductVersion>
    <SchemaVersion>2.0</SchemaVersion>
    <ProjectGuid>{452FDE9A-EAFE-44EF-A7AD-34F287AE1664}</ProjectGuid>
    <OutputType>WinExe</OutputType>
    <AppDesignerFolder>Properties</AppDesignerFolder>
    <RootNamespace>AssemblyInfoTest</RootNamespace>
    <AssemblyName>AssemblyInfoTest</AssemblyName>
    <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
    <TargetFrameworkProfile>Client</TargetFrameworkProfile>
    <FileAlignment>512</FileAlignment>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
    <PlatformTarget>x86</PlatformTarget>
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>bin\Debug\</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
    <PlatformTarget>x86</PlatformTarget>
    <DebugType>pdbonly</DebugType>
    <Optimize>true</Optimize>
    <OutputPath>bin\Release\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>  
  <ItemGroup>
    <Reference Include="System" />
    <Reference Include="System.Core" />
    <Reference Include="System.Xml.Linq" />
    <Reference Include="System.Data.DataSetExtensions" />
    <Reference Include="Microsoft.CSharp" />
    <Reference Include="System.Data" />
    <Reference Include="System.Deployment" />
    <Reference Include="System.Drawing" />
    <Reference Include="System.Windows.Forms" />
    <Reference Include="System.Xml" />
  </ItemGroup>
  <ItemGroup>
    <Compile Include="Form1.cs">
      <SubType>Form</SubType>
    </Compile>
    <Compile Include="Form1.Designer.cs">
      <DependentUpon>Form1.cs</DependentUpon>
    </Compile>
    <Compile Include="Program.cs" />
    <Compile Include="Properties\AssemblyInfo.cs" />
    <EmbeddedResource Include="Properties\Resources.resx">
      <Generator>ResXFileCodeGenerator</Generator>
      <LastGenOutput>Resources.Designer.cs</LastGenOutput>
      <SubType>Designer</SubType>
    </EmbeddedResource>
    <Compile Include="Properties\Resources.Designer.cs">
      <AutoGen>True</AutoGen>
      <DependentUpon>Resources.resx</DependentUpon>
    </Compile>
    <None Include="Properties\Settings.settings">
      <Generator>SettingsSingleFileGenerator</Generator>
      <LastGenOutput>Settings.Designer.cs</LastGenOutput>
    </None>
    <Compile Include="Properties\Settings.Designer.cs">
      <AutoGen>True</AutoGen>
      <DependentUpon>Settings.settings</DependentUpon>
      <DesignTimeSharedInput>True</DesignTimeSharedInput>
    </Compile>
  </ItemGroup>
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

  <!--<Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets"/>-->
  <Import Project="$(MSBuildExtensionsPath64)\ExtensionPack\4.0\MSBuild.ExtensionPack.tasks"/>
  <Import Project="$(MSBuildExtensionsPath64)\ExtensionPack\4.0\MSBuild.ExtensionPack.VersionNumber.targets"/>
  <PropertyGroup>
    <BuildDependsOn>
      AssemblyVersion;
      $(BuildDependsOn)
    </BuildDependsOn>
  </PropertyGroup>
  <ItemGroup>
    <AssemblyVersionFiles Include="$(MSBuildProjectDirectory)\Properties\AssemblyInfo.cs"/>
  </ItemGroup>
  <Target Name="AssemblyVersion" Inputs="@(AssemblyVersionFiles)" Outputs="UpdatedAssemblyVersionFiles">
    <Message Text="Setting AssemblyVersion..." Importance="High" />
    <AssemblyInfo
      SkipVersioning="true"
      AssemblyInfoFiles="%(AssemblyVersionFiles.FullPath)"
      AssemblyBuildNumber="12"
      AssemblyBuildNumberType="NoIncrement"
      AssemblyRevision="13"
      AssemblyRevisionType="NoIncrement"
      />
  </Target>
</Project>
Was it helpful?

Solution

Answer to this questions was: Scrap the extensions, use a powershell script to update the AssemblyInfo file directly before the build as part of CI.

Pity. Would have liked to get this working from the ground up. But the extensions just weren't worth the effort of bashing my head against them to make them work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top