MSBuildProjectDirectory无法解析MSBuild.ExtensionPack

时间:2014-03-12 14:17:40

标签: c# asp.net asp.net-web-api msbuild

我正在开发一个ASP.NET 4 WebAPI项目,并且包含一个wpp.targets文件。我需要使用MSBuild.ExtensionPack.Xml.XmlFile来替换我的一个配置XML文件中的值。

问题是我不想在所有机器上安装MSBuild.ExtensionPack,所以我把它与项目打包在一起。在我的本地版本中,MSBuild.ExtensionPack.dll的路径正确解析。但是,在我的构建计算机上,我不断收到此错误:The "MSBuild.ExtensionPack.Xml.XmlFile" task could not be loaded from the assembly C:\Program Files (x86)\MSBuild\ExtensionPack\4.0\MSBuild.ExtensionPack.dll.

它似乎正在解析程序包的默认安装位置。

这是我的wpp.targets文件中的内容:

<?xml version="1.0" encoding="utf-8"?>

<!-- Sets the assembly which will run the transformation on Web.config (Should be installed on Dev machines) -->
<UsingTask TaskName="TransformXml"
           AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v11.0\Web\Microsoft.Web.Publishing.Tasks.dll"/>

<!-- Get the path to the MSBuild.Extension.Pack -->
<PropertyGroup>
    <TPath>$(MSBuildProjectDirectory)\..\packages\MSBuild.Extension.Pack.1.3.0\tools\net40\MSBuild.ExtensionPack.tasks</TPath>
    <TPath Condition="Exists('$(MSBuildProjectDirectory)\..\packages\MSBuild.Extension.Pack.1.3.0\tools\net40\MSBuild.ExtensionPack.tasks')">$(MSBuildProjectDirectory)\..\packages\MSBuild.Extension.Pack.1.3.0\tools\net40\MSBuild.ExtensionPack.tasks</TPath>
</PropertyGroup>

<!--Import the MSBuild.Extension.Pack package -->
<Import Project="$(TPath)"/>

<!-- Make sure web.config and transformation files exist -->
<Target Name="ConfigurationTransform" BeforeTargets="BeforeBuild" Condition="Exists('Web.config')" />
<Target Name="ConfigurationTransform" BeforeTargets="BeforeBuild" Condition="Exists('Web.$(Configuration).config')" />

<!-- Make sure web.config will be there even for package/publish -->
<Target Name="CopyWebConfig" BeforeTargets="Build;Rebuild">
    <Copy SourceFiles="Web.Base.config"
          DestinationFiles="Web.config"
          OverwriteReadOnlyFiles="true"
          SkipUnchangedFiles="false" />
</Target>

<!-- Run Web.Config transformation on a build as well (not just a publish) -->
<Target Name="CustomTransformWebConfigOnBuild" AfterTargets="CopyWebConfig" >
    <Message Text="Transforming: Web.$(Configuration).config" Importance="high" />
    <TransformXml Source="Web.Base.config"
                  Transform="Web.$(Configuration).config"
                  Destination="Web.config" />
</Target>

<!-- Update Web.Config's config attribute -->
<Target Name="UpdateConfigAttribute" AfterTargets="CustomTransformWebConfigOnBuild" Condition="$(Configuration) != 'Release'">
    <Message Text="Transforming: Web.config" Importance="high" />       
    <MSBuild.ExtensionPack.Xml.XmlFile TaskAction="UpdateAttribute"
                                       File="Web.config"
                                       XPath="/configuration/appSettings/add[@key='config_url']"
                                       Key="value"
                                       Value="www.randomurl.com"/>
</Target>

1 个答案:

答案 0 :(得分:0)

如果helper-dll的源位置有多个选项...我喜欢使用下面的样式。

这样的事情(显然,你必须在“PossibleLocationOne”和“PossibleLocationTwo”中填入真实的位置。)

<PropertyGroup>
    <MyFoundMSBuildExtensionPackLocation Condition="Exists('..\PossibleLocationOne\MSBuild.ExtensionPack.dll')">..\PossibleLocationOne\MSBuild.ExtensionPack.dll</MyFoundMSBuildExtensionPackLocation>
    <MyFoundMSBuildExtensionPackLocation Condition="Exists('..\PossibleLocationTwo\MSBuild.ExtensionPack.dll')">..\PossibleLocationTwo\MSBuild.ExtensionPack.dll</MyFoundMSBuildExtensionPackLocation>
    <!--Now check to see if either of the two above resolved , if not , add something to the path so you can at least -->
    <MyFoundMSBuildExtensionPackLocation Condition="'$(MyFoundMSBuildExtensionPackLocation)'==''">DID_NOT_FIND_A_PATH_FOR_MSBUILDEXENSIONPACK\MSBuild.ExtensionPack.dll</MyFoundMSBuildExtensionPackLocation>
</PropertyGroup>

<UsingTask AssemblyFile="$(MyFoundMSBuildExtensionPackLocation)" TaskName="TransformXml"/>

为可能的位置添加所有选项..还有一个额外的“我找不到匹配”....

然后使用“UsingTask”。

“UsingTask”是~~之后的〜MyFoundMSBuildExtensionPackLocation(PropertyGroup).......所以$(MyFoundMSBuildExtensionPackLocation)在调用UsingTask之前解析。