Azure DevOps流水线中TransformXml任务加载失败问题求助
问题概述
在VS 2019控制台应用中添加TransformXml任务后,本地构建正常,但Azure DevOps流水线构建时抛出TransformXml task could not be loaded错误。
错误详情
C:__w\1\s\Sith\SithWatchDog\SithWatchDog.csproj(138,4): error MSB4062: The "TransformXml" task could not be loaded from the assembly C:\Program Files\dotnet\sdk\7.0.403\Microsoft\VisualStudio\v17.0\Web\Microsoft.Web.Publishing.Tasks.dll. Could not load file or assembly 'C:\Program Files\dotnet\sdk\7.0.403\Microsoft\VisualStudio\v17.0\Web\Microsoft.Web.Publishing.Tasks.dll'. The system cannot find the path specified. Confirm that the declaration is correct, that the assembly and all its dependencies are available, and that the task contains a public class that implements Microsoft.Build.Framework.ITask.
本地配置的任务代码
添加到csproj文件中的初始TransformXml任务代码如下:
<UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Web\Microsoft.Web.Publishing.Tasks.dll" /> <Target Name="ApplyConfigurationConfigFile" AfterTargets="PrepareForBuild" Condition="Exists('App.$(Configuration).config')"> <ItemGroup> <AppConfigWithTargetPath Remove="App.config" /> <AppConfigWithTargetPath Include="$(IntermediateOutputPath)$(TargetFileName).config"> <TargetPath>$(TargetFileName).config</TargetPath> </AppConfigWithTargetPath> </ItemGroup> <TransformXml Source="App.config" Destination="$(IntermediateOutputPath)$(TargetFileName).config" Transform="App.$(Configuration).config" /> </Target>
本地机器中Microsoft.Web.Publishing.Tasks.dll的实际路径为:C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Microsoft\VisualStudio\v16.0\Web\Microsoft.Web.Publishing.Tasks.dll
不清楚流水线代理中该文件的安装路径,希望解决两个问题:
- 如何查找流水线中该文件的路径
- 是否有其他方法在流水线中应用TransformXml
尝试的更新方案(2024年2月2日)
参考建议后,更新了csproj文件,尝试适配VS2019和VS2022的不同MSBuild路径,修改后的代码如下:
... <PropertyGroup Condition="!Exists('$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Web\Microsoft.Web.Publishing.Tasks.dll')"> <MSBuildExtensionsPath32>C:\Program Files\Microsoft Visual Studio\2022\Enterprise\MSBuild</MSBuildExtensionsPath32> </PropertyGroup> <PropertyGroup Condition="!Exists('$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Web\Microsoft.Web.Publishing.Tasks.dll')"> <MSBuildExtensionsPath32>C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild</MSBuildExtensionsPath32> </PropertyGroup> <UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Web\Microsoft.Web.Publishing.Tasks.dll" /> <Target Name="ApplyConfigurationConfigFile" AfterTargets="PrepareForBuild" Condition="Exists('App.$(Configuration).config')"> <ItemGroup> <AppConfigWithTargetPath Remove="App.config" /> <AppConfigWithTargetPath Include="$(IntermediateOutputPath)$(TargetFileName).config"> <TargetPath>$(TargetFileName).config</TargetPath> </AppConfigWithTargetPath> </ItemGroup> <TransformXml Source="App.config" Destination="$(IntermediateOutputPath)$(TargetFileName).config" Transform="App.$(Configuration).config" /> </Target> </Project>
内容的提问来源于stack exchange,提问作者samsu

