2013-05-11 62 views
1

我有asp.net 4項目變更申請自定義轉換:應用的TransformXML後文件被複制到web包目錄

<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> 
    ... 
    <None Include="Configuration\System.Debug.xml"> 
    <DependentUpon>System.xml</DependentUpon> 
    </None> 
    <Content Include="Configuration\System.xml"> 
    <TransformOnBuild>true</TransformOnBuild> 
    </Content> 
    ... 
    <Import Project="$(SolutionDir)\MsBuild\TransformFiles.targets" /> 
</Project> 

轉型邏輯是在單獨的文件中定義:

<?xml version="1.0" encoding="utf-8"?> 
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> 
    <UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.Tasks.dll"/> 
    <ItemDefinitionGroup> 
    <!-- Set the default value to false here --> 
    <None> 
     <TransformOnBuild>false</TransformOnBuild> 
    </None> 
    <Content> 
     <TransformOnBuild>false</TransformOnBuild> 
    </Content> 
    <Resource> 
     <TransformOnBuild>false</TransformOnBuild> 
    </Resource> 
    <EmbeddedResource> 
     <TransformOnBuild>false</TransformOnBuild> 
    </EmbeddedResource> 

    <_FilesToTransform> 
     <IsAppConfig>false</IsAppConfig> 
    </_FilesToTransform> 
    </ItemDefinitionGroup> 

    <PropertyGroup> 
    <TransformAllFilesDependsOn> 
     DiscoverFilesToTransform; 
    </TransformAllFilesDependsOn> 
    </PropertyGroup> 

    <Target Name="TransformAllFiles" DependsOnTargets="$(TransformAllFilesDependsOn)" AfterTargets="Build;_CopyAppConfigFile"> 
    <!-- Now we have the item list _FilesToTransformNotAppConfig and _AppConfigToTransform item lists --> 
    <!-- Transform the app.config file --> 

    <ItemGroup> 
     <_AppConfigTarget Include="@(AppConfigWithTargetPath->'$(OutDir)%(TargetPath)')" /> 
    </ItemGroup> 

    <PropertyGroup> 
     <_AppConfigDest>@(_AppConfigTarget->'%(FullPath)')</_AppConfigDest> 
     <_PkgPathFull>$([System.IO.Path]::GetFullPath($(WPPAllFilesInSingleFolder)))</_PkgPathFull> 
    </PropertyGroup> 

    <MakeDir Directories="@(_FilesToTransformNotAppConfig->'$(_PkgPathFull)\%(RelativeDir)')" 
     Condition="Exists('%(RootDir)%(Directory)%(Filename).$(Configuration)%(Extension)')"/> 

    <TransformXml Source="@(_AppConfigToTransform->'%(FullPath)')" 
       Transform="%(RelativeDir)%(Filename).$(Configuration)%(Extension)" 
       Destination="$(_AppConfigDest)" 
       Condition=" Exists('%(RelativeDir)%(Filename).$(Configuration)%(Extension)') " /> 


    <TransformXml Source="@(_FilesToTransformNotAppConfig->'%(FullPath)')" 
       Transform="%(RootDir)%(Directory)%(Filename).$(Configuration)%(Extension)" 
       Destination="$(_PkgPathFull)\%(RelativeDir)%(Filename)%(Extension)" 
       Condition=" Exists('%(RootDir)%(Directory)%(Filename).$(Configuration)%(Extension)') "/> 
    </Target> 

    <Target Name="DiscoverFilesToTransform"> 
    <ItemGroup> 
     <_FilesToTransform Include="@(None);@(Content);@(Resource);@(EmbeddedResource)" 
        Condition=" '%(TransformOnBuild)' == 'true' "/> 
    </ItemGroup> 

    <PropertyGroup> 
     <_AppConfigFullPath>@(AppConfigWithTargetPath->'%(RootDir)%(Directory)%(Filename)%(Extension)')</_AppConfigFullPath> 
    </PropertyGroup> 

    <!-- Now look to see if any of these are the app.config file --> 
    <ItemGroup> 
     <_FilesToTransform Condition=" '%(FullPath)'=='$(_AppConfigFullPath)' "> 
     <IsAppConfig>true</IsAppConfig> 
     </_FilesToTransform> 
    </ItemGroup> 

    <ItemGroup> 
     <_FilesToTransformNotAppConfig Include="@(_FilesToTransform)" 
           Condition=" '%(IsAppConfig)'!='true'"/> 

     <_AppConfigToTransform Include="@(_FilesToTransform)" 
          Condition=" '%(IsAppConfig)'=='true'"/> 
    </ItemGroup> 
    </Target> 
</Project> 

問題是在發佈過程之前應用了轉換,因此轉換後的文件將被源覆蓋:

1>------ Build started: Project: TestProject, Configuration: Debug Any CPU ------ 
... 
1>TransformAllFiles: 
1> Transforming Source File: C:\sorces\TestProject\Configuration\System.xml 
1> Applying Transform File: C:\sorces\TestProject\Configuration\System.Debug.xml 
1> Output File: C:\sorces\TestProject\obj\Debug\Package\PackageTmp\Configuration\System.xml 
1> Transformation succeeded 
1> 
1>Build succeeded. 
1> 
1>Time Elapsed 00:00:01.42 
2>------ Publish started: Project: TestProject, Configuration: Debug Any CPU ------ 
... 
2>Copying Configuration\System.xml to obj\Debug\Package\PackageTmp\Configuration\System.xml. 
... 

將文件複製到軟件包目錄後,是否可以應用轉換? 我嘗試了不同的建議,但沒有運氣。

更新1: 一個有趣的事情:文件TransformFiles.targets沒有分析在發佈過程的。

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> 
    <Message-bla-bla Importance="high" Text="test001" /> 
</Project> 

因此,建立失敗:

1>------ Build started: Project: TestProject, Configuration: Debug Any CPU ------ 
1>Build started 11.05.2013 10:18:56. 
1>C:\sources\TestProject\MsBuild\TransformFiles.targets(3,3): error MSB4067: The element <Message-bla-bla> beneath element <Project> is unrecognized. 
1> 
1>Build FAILED. 
1> 
1>Time Elapsed 00:00:00 
========== Build: 0 succeeded, 1 failed, 1 up-to-date, 0 skipped ========== 

但發佈成功(!!!): 我在文件中所做的錯誤

1>------ Publish started: Project: TestProject, Configuration: Debug Any CPU ------ 
... 
1>Publish is successfully deployed. 
1> 
========== Build: 0 succeeded, 0 failed, 2 up-to-date, 0 skipped ========== 
========== Publish: 1 succeeded, 0 failed, 0 skipped ========== 

回答

0

有2個修復:

1更改AfterTargets屬性:

<Target Name="TransformAllFiles" DependsOnTargets="$(TransformAllFilesDependsOn)" AfterTargets="CopyAllFilesToSingleFolderForMsdeploy;"> 

2重新啓動Visual Studio(!)。

相關問題