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

查看:119
本文介绍了如何使用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)\Properties\AssemblyInfo.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天全站免登陆