2014-03-12 39 views
0

我正在處理ASP.NET 4 WebAPI項目,並且包含wpp.targets文件。我需要使用MSBuild.ExtensionPack.Xml.XmlFile來替換我的一個配置XML文件中的值。MSBuildProjectDirectory無法解析MSBuild.ExtensionPack

問題是我不想在所有機器上安裝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> 

回答

0

當你有一個輔助的DLL源位置超過1個選項...我喜歡用下面的風格去做。 (很明顯,你必須把真實位置放在「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"/> 

添加所有選項可能locations..and一個額外的「我沒有找到一個匹配」 ......

然後使用「UsingTask」。

「UsingTask」在~~ MyFoundMSBuildExtensionPackLocation(PropertyGroup)之後~~所以$(MyFoundMSBuildExtensionPackLocation)在調用UsingTask之前解決。