Directory.Build.targets 中的 AssemblyName 属性 无效
AssemblyName property in Directory.Build.targets has no effect
我正在尝试覆盖我的 Azure Pipeline 中 .NET 项目中定义的 AssemblyName 属性。 (目的是标准化 NuGet 包的程序集名称。)
当我在使用 MSBuild 构建之前创建一个 Directory.Build.props 文件时它起作用。
问题是这仅在 .csproj 文件中未定义 AssemblyName 时有效。否则 .csproj 属性 优先。
根据文档,我应该能够使用 Directory.Build.targets 覆盖 .csproj 属性:
https://docs.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.
这是 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 期间显示文本消息,但未应用属性。
这是为什么,我该如何解决?
我无法让 .targets 工作,也找不到任何关于可以使用此方法覆盖的属性的文档。
相反,我添加了一个 PS 直接修改 .csproj 文件的脚本:
- 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
我正在尝试覆盖我的 Azure Pipeline 中 .NET 项目中定义的 AssemblyName 属性。 (目的是标准化 NuGet 包的程序集名称。)
当我在使用 MSBuild 构建之前创建一个 Directory.Build.props 文件时它起作用。
问题是这仅在 .csproj 文件中未定义 AssemblyName 时有效。否则 .csproj 属性 优先。
根据文档,我应该能够使用 Directory.Build.targets 覆盖 .csproj 属性: https://docs.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.
这是 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 期间显示文本消息,但未应用属性。
这是为什么,我该如何解决?
我无法让 .targets 工作,也找不到任何关于可以使用此方法覆盖的属性的文档。
相反,我添加了一个 PS 直接修改 .csproj 文件的脚本:
- 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