How do I run app.config transformation task first

2019-07-09 01:11发布

问题:

I have a console application that uses a database connection string which is defined in app.config. I have a few transformations to change the string depending on the build configuration.

I also have a few post-build events that copy app.config to other projects output. The problem is post-build event fires first and I copy the untransformed app.config. Later the transformation task kicks in and apply the transformation (so I know it works). I use with Visual Studio 2010 and .NET 4.

Right now the actions are [1], [3], [2], I need to reorder them to [1], [2], [3]

1) Build
2) Run transformation
3) Run post-build event

Here is my transformation from .csproj

<UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.Tasks.dll" />
  <Target Name="Transformation" Condition="exists('app.$(Configuration).config')" >
    <TransformXml Source="app.config" Destination="$(IntermediateOutputPath)$(TargetFileName).config" Transform="app.$(Configuration).config" />
    <ItemGroup>
      <AppConfigWithTargetPath Remove="app.config" />
      <AppConfigWithTargetPath Include="$(IntermediateOutputPath)$(TargetFileName).config">
        <TargetPath>$(TargetFileName).config</TargetPath>
      </AppConfigWithTargetPath>
    </ItemGroup>
  </Target>

And this is my post-build events

<PropertyGroup>
    <RunPostBuildEvent>OnBuildSuccess</RunPostBuildEvent>
  </PropertyGroup>

 <PropertyGroup>
    <PostBuildEvent>copy $(ProjectDir)app.config $(OutDir)\TH.Presentation.LocalAgent\$(TargetFileName).config
copy $(ProjectDir)app.config $(OutDir)\TH.Services\$(TargetFileName).config</PostBuildEvent>
  </PropertyGroup>

Any help would be greatly appreciated

回答1:

Instead of using a post build event to copy the files you can copy them as part of the Transformation target.

Use the task just before in your above XML. See http://msdn.microsoft.com/en-us/library/3e54c37h.aspx



回答2:

If there is no way of reordering them, you could combine them. Do the transform inside of your post-build events (at the top). However, there is not any nice xml transform methods in the command-prompt syntax (that I know of). You could have it call your own xml transform executable/batch file passing it your filename and transform name as parameters.

If you don't want to create your own utility, there are plenty out there