【问题标题】:Could not resolve 'CustomTransform' as a type of Transform无法将“CustomTransform”解析为一种转换
【发布时间】:2016-09-28 17:31:12
【问题描述】:
按照 appharbor here 上的示例,我进行了自定义 web.config 转换,我们将其命名为 CustomTransform。与示例中的一样,我的继承自 Transform。我还确保像这样导入我的自定义转换:
此时我可以毫无问题地运行构建。只有当我尝试使用 CustomTransform 时我才会遇到任何问题。变换是这样使用的
但是,当我在这个项目上运行 msbuild 时,我收到以下错误:
错误:无法将“CustomTransform”解析为一种变换
我应该如何引用 CustomTransform 以便 msbuild 找到它?
【问题讨论】:
标签:
.net
msbuild
web.config-transform
【解决方案1】:
我正在使用 CustomTransformation 并且遇到完全相同的问题,在我的情况下,我必须为 MsBuild 覆盖 ToolsVersion,/tv:4.0 适合我。
我拥有的项目从 VS 2008 转换为 VS 2010,然后转换为 VS 2013 项目,不知何故手动编辑 .csproj 并将项目属性更改为 ToolsVersion=12.0 后,CustomTransformation 开始失败,经过一段时间跟踪和错误,我发现它与 MsBuild ToolsVersion 有关。
【解决方案2】:
我在开始使用 Visual Studio 2019 后遇到了同样的错误。
为了解决这个错误,我必须:
- 从 .csproj 文件中删除一些与转换相关的旧 xml
- 确保具有自定义转换的引用库引用了相同版本的 Microsoft.Web.Xdt (2.1.1) - 与 SlowCheetah 目前使用的相同。首先尝试绑定重定向,但没有帮助。
删除的行作为文本:
<PropertyGroup Label="SlowCheetah">
<SlowCheetahToolsPath>$([System.IO.Path]::GetFullPath( $(MSBuildProjectDirectory)\..\packages\SlowCheetah.2.5.14\tools\))</SlowCheetahToolsPath>
<SlowCheetah_EnableImportFromNuGet Condition=" '$(SlowCheetah_EnableImportFromNuGet)'=='' ">true</SlowCheetah_EnableImportFromNuGet>
<SlowCheetah_NuGetImportPath Condition=" '$(SlowCheetah_NuGetImportPath)'=='' ">$([System.IO.Path]::GetFullPath( $(MSBuildProjectDirectory)\Properties\SlowCheetah\SlowCheetah.Transforms.targets ))</SlowCheetah_NuGetImportPath>
<SlowCheetahTargets Condition=" '$(SlowCheetah_EnableImportFromNuGet)'=='true' and Exists('$(SlowCheetah_NuGetImportPath)') ">$(SlowCheetah_NuGetImportPath)</SlowCheetahTargets>
</PropertyGroup>
<UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Web\Microsoft.Web.Publishing.Tasks.dll" />
<Target Name="AfterCompile" Condition="Exists('app.$(Configuration).config')">
<!--Generate transformed app config in the intermediate directory-->
<TransformXml Source="app.config" Destination="$(IntermediateOutputPath)$(TargetFileName).config" Transform="app.$(Configuration).config" />
<!--Force build process to use the transformed configuration file from now on.-->
<ItemGroup>
<AppConfigWithTargetPath Remove="app.config" />
<AppConfigWithTargetPath Include="$(IntermediateOutputPath)$(TargetFileName).config">
<TargetPath>$(TargetFileName).config</TargetPath>
</AppConfigWithTargetPath>
</ItemGroup>
</Target>
<!--Override After Publish to support ClickOnce AfterPublish. Target replaces the untransformed config file copied to the deployment directory with the transformed one.-->
<Target Name="AfterPublish">
<PropertyGroup>
<DeployedConfig>$(_DeploymentApplicationDir)$(TargetName)$(TargetExt).config$(_DeploymentFileMappingExtension)</DeployedConfig>
</PropertyGroup>
<!--Publish copies the untransformed App.config to deployment directory so overwrite it-->
<Copy Condition="Exists('$(DeployedConfig)')" SourceFiles="$(IntermediateOutputPath)$(TargetFileName).config" DestinationFiles="$(DeployedConfig)" />
</Target>
<Import Project="$(SlowCheetahTargets)" Condition="Exists('$(SlowCheetahTargets)')" Label="SlowCheetah" />
在预览转换时由 Visual Studio 添加):
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
...
<Error Condition="!Exists('..\packages\Microsoft.VisualStudio.SlowCheetah.3.2.26\build\Microsoft.VisualStudio.SlowCheetah.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.VisualStudio.SlowCheetah.3.2.26\build\Microsoft.VisualStudio.SlowCheetah.targets'))" />
</Target>
<Import Project="..\packages\Microsoft.VisualStudio.SlowCheetah.3.2.26\build\Microsoft.VisualStudio.SlowCheetah.targets" Condition="Exists('..\packages\Microsoft.VisualStudio.SlowCheetah.3.2.26\build\Microsoft.VisualStudio.SlowCheetah.targets')" />