Directory.Build.targets 中的 AssemblyName 属性无效

发布于 2025-01-13 08:33:38 字数 2386 浏览 2 评论 0原文

我正在尝试覆盖 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 技术交流群。

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

发布评论

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

评论(1

墨落画卷 2025-01-20 08:33:38

我无法让 .targets 工作,也找不到任何有关可以使用此方法覆盖的属性的文档。

相反,我添加了一个直接修改 .csproj 文件的 PS 脚本:

- task: PowerShell@2
  displayName: Modify .csproj file
  inputs:
    targetType: 'inline'
    script: |
      # This script will overwrite csproj properties that are already defined.
      # If they are not defined, no changes are made - values are not added.
      # The case of missing properties is handled by adding a .props file in the next step.
      # Note that changing the assembly name may break the build when the library contains xaml files.
      # Read more below.

      $path = "$(projectFolder)\${{ parameters.projectName }}.csproj"
      Write-Host "Reading project file: " $path

      [xml]$xml =(gc $path)

      $projectAssemblyName = $xml.Project.PropertyGroup.AssemblyName

      Write-Host "AssemblyName: " $projectAssemblyName

      if ($projectAssemblyName -like "$(packageName)")
      {
         Write-Host "AssemblyName corresponds to package name."            
      }
      else 
      {
         Write-Host $projectAssemblyName "is different from package name:" $(packageName)

         Write-Host "Overwriting AssemblyName with " $(packageName)
         $xml.Project.PropertyGroup |? AssemblyName |% {$_.AssemblyName="$(packageName)"}            
      }

      $xml.Project.PropertyGroup |? Company |% {$_.Company="MyCompany"}    
      $xml.Project.PropertyGroup |? GenerateDocumentationFile |% {$_.GenerateDocumentationFile="True"}    

      $xml.Save($path)

      $fileContent = Get-Content $path          
      
      Write-Host "Success. File contents: " $fileContent

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:

- task: PowerShell@2
  displayName: Modify .csproj file
  inputs:
    targetType: 'inline'
    script: |
      # This script will overwrite csproj properties that are already defined.
      # If they are not defined, no changes are made - values are not added.
      # The case of missing properties is handled by adding a .props file in the next step.
      # Note that changing the assembly name may break the build when the library contains xaml files.
      # Read more below.

      $path = "$(projectFolder)\${{ parameters.projectName }}.csproj"
      Write-Host "Reading project file: " $path

      [xml]$xml =(gc $path)

      $projectAssemblyName = $xml.Project.PropertyGroup.AssemblyName

      Write-Host "AssemblyName: " $projectAssemblyName

      if ($projectAssemblyName -like "$(packageName)")
      {
         Write-Host "AssemblyName corresponds to package name."            
      }
      else 
      {
         Write-Host $projectAssemblyName "is different from package name:" $(packageName)

         Write-Host "Overwriting AssemblyName with " $(packageName)
         $xml.Project.PropertyGroup |? AssemblyName |% {$_.AssemblyName="$(packageName)"}            
      }

      $xml.Project.PropertyGroup |? Company |% {$_.Company="MyCompany"}    
      $xml.Project.PropertyGroup |? GenerateDocumentationFile |% {$_.GenerateDocumentationFile="True"}    

      $xml.Save($path)

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