【问题标题】:Can I automatically set the AssemblyVersion in a Visual Studio proj file prior to a build?我可以在构建之前在 Visual Studio proj 文件中自动设置 AssemblyVersion 吗?
【发布时间】:2019-01-28 23:34:06
【问题描述】:

我是否可以在构建之前编写一个脚本来读取文本文件并将值写入我在 Visual Studio 中的应用程序的 proj 文件中?

特别是在构建之前,我想从解决方案中的简单文本文件中读取一个值,并将其写入 .csproj 文件夹中的 AssemblyVersion。 1.0.2.0

【问题讨论】:

    标签: visual-studio msbuild versioning


    【解决方案1】:

    从文件中读取版本可以通过ReadLinesFromFile内置任务来执行。


    .NET 框架

    获得版本后,需要将其传递给自定义目标:

    <Target Name="EntryVersion" BeforeTargets="Build">
    
        <Message Text="-----Entry-----" Importance="high"/>
        <Message Text=" Write Version " Importance="high"/>
        <Message Text="-----Entry-----" Importance="high"/>
    
        <ItemGroup>
            <Assembly Include="AssemblyProduct"><_Parameter1>$(Product)</_Parameter1></Assembly>
            <Assembly Include="AssemblyCompany"><_Parameter1>$(Company)</_Parameter1></Assembly>
            <Assembly Include="AssemblyVersion"><_Parameter1>$(Version)</_Parameter1></Assembly>
        </ItemGroup>
    
        <WriteCodeFragment AssemblyAttributes="@(Assembly)" Language="C#" OutputFile="..\GlobalAssemblyInfo.cs"/>
    </Target>
    

    此 sn-p 生成将包含程序集属性的源文件。此外,输出文件必须作为链接包含在项目中,使用的属性必须从AssemblyInfo中删除。


    .NET 核心 |标准

    可以使用来自MSBuild CLI 的本机属性Version 或从自定义目标覆盖它。该值会自动添加到程序集属性中。


    如果您使用Microsoft.Extensions.PlatformAbstractions 中的ApplicationVersion,请参阅Issue 237


    ReadLinesFromFile | WriteCodeFragment | Exec

    【讨论】:

    • 这是一个好的开始——谢谢。我对 MsBuild 一点也不熟悉,所以对我来说进展缓慢。我可以将我的值从外部文件读入一个变量,然后将它写到 GlobalAssemblyInfo 文件中。我在我的项目中包含了 GlobalAssemblyFile。我唯一的问题是,当我转储 Application.ApplicationVersion 时,它没有显示该值。
    • @MatthewAllen, Application.ApplicationVersion 这个属性来自Microsoft.Extensions.PlatformAbstractions?如果是,请参阅:Issue 237 可能有帮助。
    • 是的,我在这些扩展中使用 ApplicationConfig。暂时停留在 .Net Core 1.1 中,但希望能够很快迁移到 2.x。我现在就接听新电话——感谢您的提醒。
    • @MatthewAllen,在.NET Core 的情况下,您可以通过MSBuild CLI 使用本机属性Version,而无需任何GlobalAssemblyInfo 文件。
    • @MatthewAllen,对你有帮助吗?还是您还有问题?
    猜你喜欢
    • 1970-01-01
    • 2021-12-23
    • 2016-03-14
    • 2011-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-14
    • 1970-01-01
    相关资源
    最近更新 更多