【问题标题】:Get minor and major version from MSBUILD script从 MSBUILD 脚本获取次要和主要版本
【发布时间】:2013-09-22 21:56:35
【问题描述】:

我正在使用 Msbuild 编译和生成 .zip 文件和安装程序,我需要我的 assembyInfo 的版本号。

我正在使用此代码。

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

但这返回版本 = 2.0.0.29110,我只需要次要和主要版本。

有没有什么方法可以在没有自定义任务的情况下读取 assembyInfo.cs 信息?

【问题讨论】:

  • 使用内联代码并使其成为您需要的两个数字
  • 提供一个例子@stijn ?
  • @DavEvans Carlos 发布的答案正是这样做的:它使用 C# 正则表达式来获取前两个数字

标签: .net msbuild assemblyinfo


【解决方案1】:

最后我使用了不需要额外任务库的这段代码

<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>

【讨论】:

  • 对于像我一样遇到麻烦的人,我必须包含参考 Microsoft.Build 和 Microsoft.Build.Framework 才能工作,否则,它会给我一个空字符串。
  • wixproject中如何引用Microsoft.Build?
  • 对我不起作用。只返回空字符串。无法添加 Microsoft.Build,因为我的项目适用于 .NET Framework 2.0
【解决方案2】:

可以使用此处描述的 MSBuild 属性函数来完成: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>

输出:

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

【讨论】:

  • 谢谢!! Спасибо!))
  • 亚历克斯,Пожалуйста :)
【解决方案3】:

如果您使用的是 MsBuild 4.0,您可以编写自己的 inline custom Task 来完成这项工作。

<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>

然后在构建脚本的其他地方引用内联任务...

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

使用内联任务,您可以轻松访问程序集内部版本号的每个部分。例如 $(MajorVersionNumber)

【讨论】:

    猜你喜欢
    • 2011-12-06
    • 2012-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-22
    • 1970-01-01
    相关资源
    最近更新 更多