【问题标题】:MSBuild outputting incorrect DateTime.UtcNow formatMSBuild 输出不正确的 DateTime.UtcNow 格式
【发布时间】:2020-01-09 03:21:29
【问题描述】:

对于我在 csproj 中的版本标签,我有以下内容:

    <PropertyGroup>
        <Version>
            $([System.DateTime]::UtcNow.Year).$([System.DateTime]::UtcNow.ToString("MM")).$([System.DateTime]::UtcNow.ToString("dd")).$([System.DateTime]::UtcNow.ToString("HHmm"))
        </Version>
    </PropertyGroup>

但它的输出如下:Vysn.Voice.2020.1.9.309.nupkg 用于 nuget 包,当我从程序集中获取版本信息时:Assembly.GetExecutingAssembly().GetName().Version

但是当我这样做时

var assembly = Assembly.GetExecutingAssembly();
            var informationalVersionAttribute = assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>();

Console.WriteLine(informationalVersionAttribute.InformationalVersion);

它输出正确的 DateTime 字符串:2020.01.08.1018。我不确定我在指定版本时是否做错了,但我希望它正确输出版本:2020.MM.dd.HHmm

【问题讨论】:

    标签: asp.net-core .net-core msbuild csproj


    【解决方案1】:

    但它的输出如下:Vysn.Voice.2020.1.9.309.nupkg for nuget packages

    这是一种设计行为。

    1. 首先,Vysn.Voice 被称为包标识符2020.1.9.309 被命名为包版本。正如official docs 所述:

      始终使用其包标识符确切的版本号

      来引用特定包
      • 默认情况下,包版本匹配Version。因此,您可以通过在*.csproj 中自定义&lt;Version/&gt; 元素来更改包版本。请注意软件包版本不会影响软件包标识符
      • 默认情况下,包标识符匹配*.csproj文件中&lt;PackageId&gt;Vysn.Voice&lt;/PackageId&gt;的元数据。如果&lt;PackageId/&gt; 未指定或为空,它将使用AssemblyName。详情请见Package Id
    2. 另外,请注意&lt;Major&gt;.&lt;Minor&gt;.&lt;Build&gt; 中的版本号应该是Integer。并且当使用NuGet 打包项目时,版本号中的前导零 总是从MajorMinorBuild 部分中删除 .因此,您将获得2020.1.9,而不是获得2020.01.09。此行为已记录在 here

    3. 最后,当你调用

      assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()
      

      您实际上是在尝试获取与VersionPackageVersion 不同的InformationalVersionInformationalVersion 可能包含任何字符串。这个InformationalVersion默认匹配Version,可能看起来像2020.01.09.0704(注意会有一些前导零。)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多