Directory.Build.targets 中的 AssemblyName 属性无效
我正在尝试覆盖 Azure Pipeline 中 .NET 项目中定义的 AssemblyName 属性。 (目的是标准化 NuGet 包的程序集名称。)
当我在使用 MSBuild 构建之前创建 Directory.Build.props 文件时,它会起作用。
问题是,只有在 .csproj 文件中未定义 AssemblyName 时,这才有效。否则 .csproj 属性优先。
根据文档,我应该能够使用 Directory.Build.targets 来覆盖 .csproj 属性: https://learn.microsoft.com/en-us/visualstudio/msbuild/customize-your-build?view=vs-2022#choose- Between-adding-properties-to-a-props-or-targets -文件
如果您需要覆盖属性,请在 .targets 文件中执行此操作,然后 所有用户项目自定义都有机会生效。
这就是 Directory.Build.targets 文件包含的内容:
<Project>
<Target Name="IncludeProjectMatchMessage" Condition="$(MSBuildProjectName) == ''${{ parameters.projectName }}''" BeforeTargets="Build">
<Message Text="The custom Directory.Build.targets properties will be applied to this project $(MSBuildProjectName), AssemblyName = $(packageName)" Importance="high" />
</Target>
<PropertyGroup Condition="$(MSBuildProjectName) == ''${{ parameters.projectName }}''">
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<Company>MyCompany</Company>
<AssemblyName>$(packageName)</AssemblyName>
</PropertyGroup>
</Project>
我也尝试过此操作,结果相同:
<Project>
<Target Name="IncludeProjectMatchMessage" Condition="$(MSBuildProjectName) == ''${{ parameters.projectName }}''" BeforeTargets="Build">
<Message Text="The custom Directory.Build.targets properties will be applied to this project $(MSBuildProjectName), AssemblyName = $(packageName)" Importance="high" />
<PropertyGroup Condition="$(MSBuildProjectName) == ''${{ parameters.projectName }}''">
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<Company>MyCompany</Company>
<AssemblyName>$(packageName)</AssemblyName>
</PropertyGroup>
</Target>
</Project>
MSBuild 期间显示文本消息,但未应用属性。
为什么会这样,我该如何解决?
I am trying to override the AssemblyName property defined in .NET projects in my Azure Pipeline. (The purpose is to standardize assembly names for NuGet packages.)
It works when I create a Directory.Build.props file right before building with MSBuild.
The problem is that this only works if the AssemblyName has not been defined in the .csproj file. Otherwise the .csproj property takes precedence.
According to the docs, I should be able to use Directory.Build.targets to override .csproj properties:
https://learn.microsoft.com/en-us/visualstudio/msbuild/customize-your-build?view=vs-2022#choose-between-adding-properties-to-a-props-or-targets-file
If you need to override properties, do it in a .targets file, after
all user-project customizations have had a chance to take effect.
This is what the Directory.Build.targets file contains:
<Project>
<Target Name="IncludeProjectMatchMessage" Condition="$(MSBuildProjectName) == ''${{ parameters.projectName }}''" BeforeTargets="Build">
<Message Text="The custom Directory.Build.targets properties will be applied to this project $(MSBuildProjectName), AssemblyName = $(packageName)" Importance="high" />
</Target>
<PropertyGroup Condition="$(MSBuildProjectName) == ''${{ parameters.projectName }}''">
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<Company>MyCompany</Company>
<AssemblyName>$(packageName)</AssemblyName>
</PropertyGroup>
</Project>
I have also tried this, with the same result:
<Project>
<Target Name="IncludeProjectMatchMessage" Condition="$(MSBuildProjectName) == ''${{ parameters.projectName }}''" BeforeTargets="Build">
<Message Text="The custom Directory.Build.targets properties will be applied to this project $(MSBuildProjectName), AssemblyName = $(packageName)" Importance="high" />
<PropertyGroup Condition="$(MSBuildProjectName) == ''${{ parameters.projectName }}''">
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<Company>MyCompany</Company>
<AssemblyName>$(packageName)</AssemblyName>
</PropertyGroup>
</Target>
</Project>
The text message is displayed during the MSBuild, but the properties are not applied.
Why is that, and how do I fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我无法让 .targets 工作,也找不到任何有关可以使用此方法覆盖的属性的文档。
相反,我添加了一个直接修改 .csproj 文件的 PS 脚本:
I could not get .targets to work, nor could I find any documentation on the properties that it is possible to override using this method.
Instead, I added a PS script that modifies the .csproj file directly: