在Visual Studio中的MSBuild社区任务BeforeBuild步骤期间,在路径中找不到xsd.exe [英] Can't find xsd.exe in the path during MSBuild Community Tasks BeforeBuild step in Visual Studio

查看:335
本文介绍了在Visual Studio中的MSBuild社区任务BeforeBuild步骤期间,在路径中找不到xsd.exe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MSBuild社区任务来运行Xsd.exe作为Visual Studio中构建的一部分,因此:

I am using MSBuild Community Tasks to run Xsd.exe as part of my build in Visual Studio as thus:

<Import Project="$(SolutionDir)Common.targets" />
<PropertyGroup>
  <MSBuildCommunityTasksPath>$(SolutionDir)TeamBuildTypes</MSBuildCommunityTasksPath>
</PropertyGroup>
<Import Project="$(MSBuildCommunityTasksPath)\MSBuild.Community.Tasks.Targets" />
<UsingTask TaskName="XSD" AssemblyFile="$(VCTargetsPath)$(CPPTasks)" />
<Target Name="BeforeBuild">
  <!--Exec Command="'$(DevEnvDir)..\Tools\vsvars32.bat'" /-->
  <XSD Sources="MySchema.xsd" GenerateFromSchema="classes" Language="CS" />
</Target>

Common.targets 符合此处.

但是,在构建过程中出现以下错误:

However, I get the following error during build:

"XSD"任务意外失败.

The "XSD" task failed unexpectedly.

Microsoft.Build.Shared.InternalErrorException:MSB0001:内部MSBuild错误:xsd.exe意外不是根目录

Microsoft.Build.Shared.InternalErrorException: MSB0001: Internal MSBuild Error: xsd.exe unexpectedly not a rooted path

已在MSDN论坛上给出了解决方案

A solution has been given on MSDN forum here, which is to add a path to xsd.exe to the path environment variable. But, as indicated by Pico Ohms' answer, this is not maintainable as it is version-dependent and requires every dev to perform an extra step in order to build, just because of Xsd.exe.

我找到了另一个解决方案此处,它是用于预先调用 vsvars32.bat 的.这是上面代码中的注释行.这没有用,所以我在此处找到了解决方案希望它能正常工作,那就是使用/useenv参数调用 DEVENV .

I found another solution here which is to call vsvars32.bat beforehand. This is the commented-out line in the code above. This didn't work, so I found a solution here that I hoped would make it work, which was to call DEVENV with the /useenv parameter.

然后我在此处找到了另一种解决方案,即将<xs:include schemaLocation="MSBuild\MSBuild.Community.Tasks.xsd"/>添加到 Microsoft.Build.xsd .也没用.

Then I found another solution here which is to add <xs:include schemaLocation="MSBuild\MSBuild.Community.Tasks.xsd"/> to Microsoft.Build.xsd. Didn't work either.

所以,现在我没主意了.如何通过使用/useenv解决方案或其他解决方案,在不要求开发人员更新其 path 变量的情况下,如何使MSBuild社区任务XSD任务正常运行?我知道我可以将 xsd.exe 放入Visual Studio解决方案中,但这对我来说似乎是一种廉价的解决方法.

So, now I'm out of ideas. How do I get the MSBuild Community Tasks XSD task working without requiring devs to update their path variable, either by using the /useenv solution, or some other solution? I'm aware I can put xsd.exe in the Visual Studio solution, but this seems like a cheap workaround to me.

推荐答案

我必须做一些研究才能记住评论我在另一线程上发布了有关此内容的文章,但是您可以通过从XSD任务派生并覆盖GenerateFullPathToTool()方法来为您的任务实现自定义搜索路径.

I had to do some research to remember the comment I posted on the other thread about this, but you can implement a custom search path for your task by deriving from the XSD task and overriding the GenerateFullPathToTool() method.

当您在DotPeek中查看XSD任务时,您可以看到它源自Microsoft.Build.Utilities.ToolTask​​. ToolTask​​包含一个名为"GenerateFullPathToTool()"的方法.调用此方法将返回其名称所暗示的完整路径.

When you look at the XSD task in DotPeek you can see it derives from Microsoft.Build.Utilities.ToolTask. ToolTask contains a method called "GenerateFullPathToTool()". This method is called to return the full path to the tool as its name implies.

Microsoft.Build.Utilities类中的ToolLocationHelper可用于为已安装的xsd.exe查找合适的位置.

The ToolLocationHelper in Microsoft.Build.Utilities class can be used to find the appropriate location for your installed xsd.exe.

它包含一种名为 GetPathToDotNetFrameworkSdkFile ,这将为您提供点网sdk的fileName位置.输入"xsd.exe"作为参数,您应该一切顺利.

It contains a method called GetPathToDotNetFrameworkSdkFile which will give you the location of fileName for the dot net sdk. Put "xsd.exe" as the parameter and you should be good to go.

如果该方法未返回所需的路径,则还有其他方法可从注册表中返回各种路径及其基础,因此比设置显式路径更可移植.

If that method doesn't return the path you need, there are other methods for returning the various paths and its based off of the registry so its more portable than setting an explicit path.

这篇关于在Visual Studio中的MSBuild社区任务BeforeBuild步骤期间,在路径中找不到xsd.exe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆