如何使用 MSBuild 更改 AssemblyProduct、AssemblyTitle? [英] How can I change AssemblyProduct, AssemblyTitle using MSBuild?

查看:20
本文介绍了如何使用 MSBuild 更改 AssemblyProduct、AssemblyTitle?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 MSBuild 脚本,它编译我现有的解决方案,但我想在编译时更改解决方案中的一个项目的一些属性,包括但不限于 AssemblyProduct 和 AssemblyTitle.

I have an MSBuild script which compiles my existing solution but I'd like to change some properties of one of the projects within the solution at compile-time, including but not limited to AssemblyProduct and AssemblyTitle.

这是我的构建脚本的片段:

Here's a snippet of my build script:

  <Target Name="Compile" >
 <MSBuild Projects="..MySolution.sln" 
             Properties="Configuration=MyReleaseConfig;Platform=x86" />
  </Target>

我有一个主要的可执行文件和几个已编译的 DLL.我知道 MSBuild 扩展包,我怀疑它可能会帮助我到达我需要的地方,虽然我不确定如何进行.

I've got one main executable and several DLLs that are compiled. I am aware of the MSBuild Extension Pack and I suspect it might help me to get to where I need to be, although I'm not sure how to proceed.

我可以在构建时有选择地更改 AssemblyInfo 属性吗?

Can I selectively change AssemblyInfo properties at build time?

推荐答案

使用 MSBuild 扩展包,您走在了正确的轨道上.

You're on the right track with the MSBuild Extension Pack.

我发现在构建时有条件地生成程序集详细信息的最简单方法是将AssemblyVersion"目标直接添加到需要更新的 AssemblyInfo 文件的 .csproj 文件中.您可以将目标直接添加到需要更新的 AssemblyInfo 文件的每个 csproj 文件中,或者按照我的喜好,使用 AssemblyVersion 目标创建自定义目标文件,并让每个 csproj 文件包含您的自定义目标文件.

I find the easiest way to conditionally generate the assembly details at build time is to add an "AssemblyVersion" target directly to my .csproj file(s) that require an updated AssemblyInfo file. You can add the target directly to each csproj file that requires an updated AssemblyInfo file, or as I prefer to do it, create a custom targets file with the AssemblyVersion target and have each csproj file include your custom targets file.

无论哪种方式,您都可能希望使用 MSBuild 扩展包或 MSBuild 社区任务 来使用他们的相应的 AssemblyInfo 任务.

Either way you likely want to use the MSBuild Extension Pack or the MSBuild Community Tasks to use their respective AssemblyInfo task.

以下是我们构建脚本中的一些代码:

Here's some code from our build scripts:

<!-- Import the AssemblyInfo task -->
<Import Project="$(MSBuildCommunityTasksPath)MSBuild.Community.Tasks.Targets"/>

<!-- Overriding the Microsoft.CSharp.targets target dependency chain -->
<!-- Call our custom AssemblyVersion target before build, even from VS -->
<PropertyGroup>
    <BuildDependsOn>
        AssemblyVersion;
        $(BuildDependsOn)
    </BuildDependsOn>
</PropertyGroup>

<ItemGroup>
    <AssemblyVersionFiles Include="$(MSBuildProjectDirectory)PropertiesAssemblyInfo.cs"/>
</ItemGroup>

<Target Name="AssemblyVersion"
                Inputs="@(AssemblyVersionFiles)"
                Outputs="UpdatedAssemblyVersionFiles">
    <Attrib Files="%(AssemblyVersionFiles.FullPath)"
                    Normal="true"/>
    <AssemblyInfo
        CodeLanguage="CS"
        OutputFile="%(AssemblyVersionFiles.FullPath)"
        AssemblyCompany="$(CompanyName)"
        AssemblyCopyright="Copyright $(CompanyName), All rights reserved."
        AssemblyVersion="$(Version)"
        AssemblyFileVersion="$(Version)">
        <Output TaskParameter="OutputFile"
                        ItemName="UpdatedAssemblyVersionFiles"/>
    </AssemblyInfo>
</Target>

这篇关于如何使用 MSBuild 更改 AssemblyProduct、AssemblyTitle?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆