Get minor and major version from MSBUILD script

后端 未结 3 1470
借酒劲吻你
借酒劲吻你 2020-12-15 11:20

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.



        
相关标签:
3条回答
  • 2020-12-15 12:05

    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)

    0 讨论(0)
  • 2020-12-15 12:12

    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".
    
    0 讨论(0)
  • 2020-12-15 12:13

    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>
    
    0 讨论(0)
提交回复
热议问题