Question

I'm using Msbuild to compile and generate .zip files and installers and I need the version number of my assembyInfo.

I'm using this code.

<Target Name="getversion">
    <GetAssemblyIdentity AssemblyFiles="$(BuildDir)\myprogram.exe">
        <Output TaskParameter="Assemblies" ItemName="fooAssemblyInfo"/>
    </GetAssemblyIdentity>
    <Message Text="Version = %(fooAssemblyInfo.Version)"/>
</Target>

But this returns Version = 2.0.0.29110, I need just the minor and major version.

Is there any way to read the assembyInfo.cs information without a custom task?

Was it helpful?

Solution

Finally I have used this code that not require additional task libraries

<Target Name="getversion">
    <GetAssemblyIdentity AssemblyFiles="$(BuildDir)\myfile.exe">
        <Output TaskParameter="Assemblies" ItemName="myAssemblyInfo"/>
    </GetAssemblyIdentity>
    <PropertyGroup>
        <Pattern>(\d+)\.(\d+)</Pattern>
        <In>%(myAssemblyInfo.Version)</In>
        <OutVersion>$([System.Text.RegularExpressions.Regex]::Match($(In), $(Pattern)))</OutVersion>
    </PropertyGroup>
</Target>

OTHER TIPS

It can be done using MSBuild Property Functions described here: https://msdn.microsoft.com/en-us/library/dd633440%28v=vs.120%29.aspx

<Target Name="getversion">
    <GetAssemblyIdentity AssemblyFiles="$(BuildDir)\myprogram.exe">
        <Output TaskParameter="Assemblies" ItemName="fooAssemblyInfo"/>
    </GetAssemblyIdentity>

    <Message Text="Version = $([System.Version]::Parse(%(fooAssemblyInfo.Version)).ToString(2))" Importance="high" />
</Target>

Output:

Done executing task "GetAssemblyIdentity".
Task "Message"
    Task Parameter:Text=Version = 12.0
    Task Parameter:Importance=high
    Version = 12.0
Done executing task "Message".

If you're using MsBuild 4.0 you can write your own inline custom Task to get this done.

<UsingTask 
  TaskName="GetVersionParts"
  TaskFactory="CodeTaskFactory"
  AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">

  <ParameterGroup>
    <AssemblyPath ParameterType="System.String" Required="true" />
    <MajorVersion ParameterType="System.Int32" Output="true" />
    <MinorVersion ParameterType="System.Int32" Output="true" />
    <BuildVersion ParameterType="System.Int32" Output="true" />
  </ParameterGroup>
  <Task>
    <Using Namespace="System.Diagnostics" />
    <Code Type="Fragment" Language="cs">
      <![CDATA[
        Log.LogMessage("Getting version details of assembly at: " + this.AssemblyPath, MessageImportance.High);

        Version v = Version.Parse(FileVersionInfo.GetVersionInfo(this.AssemblyPath).FileVersion);

        this.MajorVersion = v.Major;
        this.MinorVersion = v.Minor;
        this.BuildVersion = v.Build;    
      ]]>
    </Code>
  </Task>
</UsingTask>

and then reference the inline task elsewhere in your build script...

<GetVersionParts AssemblyPath="$(OutputDirAbsolute)/MyAssembly.dll">
  <Output TaskParameter="MajorVersion" PropertyName="MajorVersionNumber" />
  <Output TaskParameter="MinorVersion" PropertyName="MinorVersionNumber" />
  <Output TaskParameter="BuildVersion" PropertyName="BuildVersionNumber" />
</GetVersionParts>

Using the inline Task, you have can have easy access to each part of the assembly's build number. e.g $(MajorVersionNumber)

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