如何使用 MSBuild 更改 AssemblyProduct、AssemblyTitle?

发布于 2024-09-15 23:42:01 字数 507 浏览 7 评论 0原文

我有一个 MSBuild 脚本,可以编译现有的解决方案,但我想在编译时更改解决方案内的项目之一的某些属性,包括但不限于 AssemblyProduct 和 AssemblyTitle。

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

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

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

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

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>

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.

Can I selectively change AssemblyInfo properties at build time?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

楠木可依 2024-09-22 23:42:01

使用 MSBuild 扩展包,您的方向是正确的。

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

无论哪种方式,您可能希望使用 MSBuild 扩展包或 MSBuild 社区任务 来使用各自的 AssemblyInfo 任务。

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

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

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

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.

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>
忆伤 2024-09-22 23:42:01

斯尼尔的回答非常有帮助,但我想展示我实际上最终做了什么。我没有编辑 csproj 文件(有多个),而是将任务添加到我的构建脚本中。这是一个片段:

    <PropertyGroup>  
        <ProductName>MyApp</ProductName>
        <CompanyName>MyCompany</CompanyName>
        <Major>1</Major>
        <Minor>0</Minor>
        <Build>0</Build>
        <Revision>0</Revision>
    </PropertyGroup> 

    <ItemGroup>
        <AssemblyVersionFiles Include="..\MyMainProject\Properties\AssemblyInfo.cs"/>
    </ItemGroup>

<Target Name="AssemblyVersionMAIN" Inputs="@(AssemblyVersionFiles)" Outputs="UpdatedAssemblyVersionFiles">
        <Attrib Files="%(AssemblyVersionFiles.FullPath)" Normal="true"/>
        <AssemblyInfo
            CodeLanguage="CS"
            OutputFile="%(AssemblyVersionFiles.FullPath)"
            AssemblyProduct="$(ProductName)"
            AssemblyTitle="$(ProductName)"
            AssemblyCompany="$(CompanyName)"
            AssemblyCopyright="© $(CompanyName) 2010"
            AssemblyVersion="$(Major).$(Minor).$(Build).$(Revision)"
            AssemblyFileVersion="$(Major).$(Minor).$(Build).$(Revision)"
            AssemblyInformationalVersion="$(Major).$(Minor).$(Build).$(Revision)">
            <Output TaskParameter="OutputFile" ItemName="UpdatedAssemblyVersionFiles"/>
        </AssemblyInfo>
    </Target>

 <Target Name="Compile" DependsOnTargets="AssemblyVersionMAIN">
      <MSBuild Projects="..\MySolution.sln"     
               Properties="Configuration=Release;Platform=x86;Optimize=true" />
 </Target>

然后,我可以从命令行或批处理脚本覆盖我的变量,如下所示:

set MAJ=1
set MIN=2
set BLD=3
set REV=4

msbuild buildScript.xml /t:Compile /p:Major=%MAJ% /p:Minor=%MIN% /p:Build=%BLD% /p:Revision=%REV%

Sneal's answer was very helpful, but I'd like to show what I actually ended up doing. Instead of editing csproj files (there are several) I instead added tasks to my build script. Here's a snippet:

    <PropertyGroup>  
        <ProductName>MyApp</ProductName>
        <CompanyName>MyCompany</CompanyName>
        <Major>1</Major>
        <Minor>0</Minor>
        <Build>0</Build>
        <Revision>0</Revision>
    </PropertyGroup> 

    <ItemGroup>
        <AssemblyVersionFiles Include="..\MyMainProject\Properties\AssemblyInfo.cs"/>
    </ItemGroup>

<Target Name="AssemblyVersionMAIN" Inputs="@(AssemblyVersionFiles)" Outputs="UpdatedAssemblyVersionFiles">
        <Attrib Files="%(AssemblyVersionFiles.FullPath)" Normal="true"/>
        <AssemblyInfo
            CodeLanguage="CS"
            OutputFile="%(AssemblyVersionFiles.FullPath)"
            AssemblyProduct="$(ProductName)"
            AssemblyTitle="$(ProductName)"
            AssemblyCompany="$(CompanyName)"
            AssemblyCopyright="© $(CompanyName) 2010"
            AssemblyVersion="$(Major).$(Minor).$(Build).$(Revision)"
            AssemblyFileVersion="$(Major).$(Minor).$(Build).$(Revision)"
            AssemblyInformationalVersion="$(Major).$(Minor).$(Build).$(Revision)">
            <Output TaskParameter="OutputFile" ItemName="UpdatedAssemblyVersionFiles"/>
        </AssemblyInfo>
    </Target>

 <Target Name="Compile" DependsOnTargets="AssemblyVersionMAIN">
      <MSBuild Projects="..\MySolution.sln"     
               Properties="Configuration=Release;Platform=x86;Optimize=true" />
 </Target>

Then, I can override my variables from the command line, or a batch script, like so:

set MAJ=1
set MIN=2
set BLD=3
set REV=4

msbuild buildScript.xml /t:Compile /p:Major=%MAJ% /p:Minor=%MIN% /p:Build=%BLD% /p:Revision=%REV%
枕头说它不想醒 2024-09-22 23:42:01
<Target Name="SetVersion">
<ItemGroup>
  <AssemblyInfoFiles  Include="$(TargetDir)\**\AssemblyInfo.cs"/>
</ItemGroup>

<Message Text="change the Version number for:"/>
<Message Text="%(AssemblyInfoFiles.FullPath)"/>

<MSbuild.ExtensionPack.Framework.AssemblyInfo 
 AssemblyInfoFiles="@(AssemblyInfoFiles)"
                                              AssemblyTitle="newTitle"
                                              AssemblyMajorVersion="2"
                                              AssemblyMinorVersion="0"/>

 </Target>
<Target Name="SetVersion">
<ItemGroup>
  <AssemblyInfoFiles  Include="$(TargetDir)\**\AssemblyInfo.cs"/>
</ItemGroup>

<Message Text="change the Version number for:"/>
<Message Text="%(AssemblyInfoFiles.FullPath)"/>

<MSbuild.ExtensionPack.Framework.AssemblyInfo 
 AssemblyInfoFiles="@(AssemblyInfoFiles)"
                                              AssemblyTitle="newTitle"
                                              AssemblyMajorVersion="2"
                                              AssemblyMinorVersion="0"/>

 </Target>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文