如何在PostBuildEvents之前首先运行app.config转换任务?

时间:2011-07-26 12:00:50

标签: c# build-process app-config transformation csproj

我有一个控制台应用程序,它使用app.config中定义的数据库连接字符串。我有一些转换来根据构建配置更改字符串。

我还有一些将app.config复制到其他项目输出的后期构建事件。 问题是构建后事件首先触发,我复制未转换的app.config。稍后转换任务启动并应用转换(所以我知道它有效)。我使用Visual Studio 2010和.NET 4.

现在行动是[1],[ 3 ],[ 2 ],我需要将它们重新排序为[1],[2],[3] ]

1)建造 2)运行转换
3)运行post-build事件

这是我从.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>

这是我的后期制作活动

<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>

非常感谢任何帮助

2 个答案:

答案 0 :(得分:1)

如果无法对它们进行重新排序,您可以将它们组合起来。在构建后的事件中进行转换(在顶部)。但是,命令提示语法(我知道)中没有任何好的xml转换方法。你可以让它调用你自己的xml转换可执行文件/批处理文件,传递你的文件名并将名称转换为参数。

如果您不想创建自己的实用程序,则有很多there

答案 1 :(得分:1)

您可以将文件复制为转化目标的一部分,而不是使用帖子构建事件来复制文件。

在上面的XML中使用之前的任务。 见http://msdn.microsoft.com/en-us/library/3e54c37h.aspx