如何将版本附加到.NET标准2.0库项目中的汇编输出文件名

发布于 2025-02-06 07:13:41 字数 288 浏览 2 评论 0 原文

我正在使用VS2022 17.2.3。

在.NET标准2.0库项目中,我希望更改输出组件文件名。

项目 - >属性 - >汇编名称,我看到它设置为 $(msbuildProjectName)

我想将其更改为 $(msbuildProjectName)$(assemblyversion),但是 $(assemblyversion) 不起作用。

我想问是否有人可以指向正确的事情。

I am using VS2022 17.2.3.

In a .NET Standard 2.0 Library Project I wish to change the output assembly file name.

In Project -> Properties -> Assembly Name, I see that it is set to $(MSBuildProjectName).

I want to change it to $(MSBuildProjectName)$(AssemblyVersion), but $(AssemblyVersion) is not working.

I want to ask if anyone can point to right thing.

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

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

发布评论

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

评论(1

走野 2025-02-13 07:13:42

这是由于 msbuild的属性评估顺序

设置 assemblyVersion csproj file 之前,请先设置 assemblyName 工作正常:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <AssemblyVersion>1.0.0.0</AssemblyVersion>
    <AssemblyName>$(MSBuildProjectName)$(AssemblyVersion)</AssemblyName>
  </PropertyGroup>
</Project>
1>CSharpScratchpad -> C:\Projects\CSharpScratchpad\bin\Debug\net6.0\CSharpScratchpad1.0.0.0.dll

在注释中,您还说您要想要使用通配符汇编,例如 1.0。*

我不知道访问MSBuild版本内部生成的扩展的最终形式的好方法,因此我只能提供稍微丑陋的后构建副本,从内置的组装中检索版本:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <AssemblyVersion>1.0.*</AssemblyVersion>
    <Deterministic>false</Deterministic>
  </PropertyGroup>

  <Target Name="PostBuild" AfterTargets="PostBuildEvent">
    <GetAssemblyIdentity AssemblyFiles="$(TargetPath)">
      <Output TaskParameter="Assemblies" ItemName="AssemblyIdentity"/>
    </GetAssemblyIdentity>
    <Exec Command='COPY "$(TargetPath)" "$(TargetDir)$(TargetName)%(AssemblyIdentity.Version)$(TargetExt)" /Y' />
  </Target>
</Project>

在后续评论中,您想要仅将主要版本附加到文件名。

您可以创建 system.version 汇编版本的实例,并调用其 toString(int fieldCount)方法,以便它仅返回前两个段。这将是新的帖子构建目标 - 我已经将结果存储在 majorminor 属性 sanity 可读性中,但是如果您喜欢:

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
  <GetAssemblyIdentity AssemblyFiles="$(TargetPath)">
    <Output TaskParameter="Assemblies" ItemName="AssemblyIdentity"/>
  </GetAssemblyIdentity>
  <PropertyGroup>
    <MajorMinor>$([System.Version]::new("%(AssemblyIdentity.Version)").ToString(2))</MajorMinor>
  </PropertyGroup>
  <Exec Command='COPY "$(TargetPath)" "$(TargetDir)$(TargetName)$(MajorMinor)$(TargetExt)" /Y' />
</Target>

我建议能够在几个月内向您的大学或您自己解释。也许有人知道这是一个不那么令人费解的解决方案。

This is due to MSBuild's Property Evaluation Order.

Setting AssemblyVersion in the csproj file before setting AssemblyName works fine:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <AssemblyVersion>1.0.0.0</AssemblyVersion>
    <AssemblyName>$(MSBuildProjectName)$(AssemblyVersion)</AssemblyName>
  </PropertyGroup>
</Project>
1>CSharpScratchpad -> C:\Projects\CSharpScratchpad\bin\Debug\net6.0\CSharpScratchpad1.0.0.0.dll

In the comments, you also stated you want to use a wildcard AssemblyVersion, like 1.0.*.

I don't know a good way to access the expanded, final form of the version MSBuild generates internally, so I can only offer a slightly ugly post build copy retrieving the version from the built assembly:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <AssemblyVersion>1.0.*</AssemblyVersion>
    <Deterministic>false</Deterministic>
  </PropertyGroup>

  <Target Name="PostBuild" AfterTargets="PostBuildEvent">
    <GetAssemblyIdentity AssemblyFiles="$(TargetPath)">
      <Output TaskParameter="Assemblies" ItemName="AssemblyIdentity"/>
    </GetAssemblyIdentity>
    <Exec Command='COPY "$(TargetPath)" "$(TargetDir)$(TargetName)%(AssemblyIdentity.Version)$(TargetExt)" /Y' />
  </Target>
</Project>

In a follow-up comment, you wanted to only append the major.minor version to the file name.

You can create a System.Version instance of the assembly version, and call its ToString(int fieldCount) method so that it only returns the first 2 segments. This would be the new post build target - I've stored the result in a MajorMinor property for sanity readability, but you can bash it all into one line if you prefer:

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
  <GetAssemblyIdentity AssemblyFiles="$(TargetPath)">
    <Output TaskParameter="Assemblies" ItemName="AssemblyIdentity"/>
  </GetAssemblyIdentity>
  <PropertyGroup>
    <MajorMinor>$([System.Version]::new("%(AssemblyIdentity.Version)").ToString(2))</MajorMinor>
  </PropertyGroup>
  <Exec Command='COPY "$(TargetPath)" "$(TargetDir)$(TargetName)$(MajorMinor)$(TargetExt)" /Y' />
</Target>

I suggest being able to explain this to your collegues or yourself in a few months. And maybe someone else knows a less convoluted solution for this.

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