MSBuild脚本和VS2010发布应用Web.config变换

所以,我已经安装了VS 2010,并且正在修改我们的TeamCity构build集成的MSBuild脚本。 除了一个例外,一切都很好。

我怎样才能告诉MSBuild,我想要应用我发布构build时创build的Web.conifg转换文件…

我有以下产生编译的网站,但是,它输出一个Web.config,Web.Debug.config和Web.Release.config文件(所有3)到编译的输出目录。 在工作室,当我执行一个发布到文件系统它将做转换,只输出与适当的更改的Web.config …

<Target Name="CompileWeb"> <MSBuild Projects="myproj.csproj" Properties="Configuration=Release;" /> </Target> <Target Name="PublishWeb" DependsOnTargets="CompileWeb"> <MSBuild Projects="myproj.csproj" Targets="ResolveReferences;_CopyWebApplication" Properties="WebProjectOutputDir=$(OutputFolder)$(WebOutputFolder); OutDir=$(TempOutputFolder)$(WebOutputFolder)\;Configuration=Release;" /> </Target> 

任何帮助将是伟大的..!

我知道这可以通过其他方式来完成,但我希望如果可能的话,使用新的VS 2010方法

我正在寻找类似的信息,并没有完全find它,所以我在Visual Studio 2010和MSBuild 4.0附带的.targets文件中进行了一些挖掘。 我认为这是寻找将执行转换的MSBuild任务的最佳位置。

据我所知,下面的MSBuild任务被使用:

 <Project ToolsVersion="4.0" DefaultTargets="Deploy" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.Tasks.dll"/> <PropertyGroup> <ProjectPath>C:\Path to Project\Here</ProjectPath> <DeployPath>C:\Path to Deploy\There</DeployPath> <TransformInputFile>$(ProjectPath)\Web.config</TransformInputFile> <TransformFile>$(ProjectPath)\Web.$(Configuration).config</TransformFile> <TransformOutputFile>$(DeployPath)\Web.config</TransformOutputFile> <StackTraceEnabled>False</StackTraceEnabled> </PropertyGroup> <Target Name="Transform"> <TransformXml Source="$(TransformInputFile)" Transform="$(TransformFile)" Destination="$(TransformOutputFile)" Condition="some condition here" StackTrace="$(StackTraceEnabled)" /> </Target> </Project> 

我已经testing了以上,并可以确认它的工作原理。 您可能需要稍微调整结构以更好地适应您的构build脚本。

您应该可以通过使用Package目标并指定临时目录来完成此操作。

 msbuild solution.sln /p:Configuration=Release;DeployOnBuild=true;DeployTarget=Package;_PackageTempDir=..\publish 

http://pattersonc.com/blog/index.php/2010/07/15/visual-studio-2010-publish-command-from-msbuild-command-line/

或者,您可以尝试使用XDT转换工具

http://ctt.codeplex.com

我正在使用这个而不是搞砸msbuild目标。 与app.config不只是web.config

它为我工作了以下变化

 <MSBuild Projects="$(ProjectFile)" Targets="ResolveReferences;_WPPCopyWebApplication" Properties="WebProjectOutputDir=TempOutputFolder;OutDir=$(WebProjectOutputDir);Configuration=$(Configuration);" /> 

从MsBuild文件夹下的Microsoft.WebApplication.targets文件

 _CopyWebApplication This target will copy the build outputs along with the content files into a _PublishedWebsites folder. This Task is only necessary when $(OutDir) has been redirected to a folder other than ~\bin such as is the case with Team Build. The original _CopyWebApplication is now a Legacy, you can still use it by setting $(UseWPP_CopyWebApplication) to true. By default, it now change to use _WPPCopyWebApplication target in Microsoft.Web.Publish.targets. It allow to leverage the web.config trsnaformation. 

这是一个关于自定义转换的优秀写作:

http://www.diaryofaninja.com/blog/2011/09/14/using-custom-webconfig-transformations-in-msbuild

我们需要定制Web部署比平常更多一些,因为我们不得不容纳大量的经典ASP和其他的缺陷。 本文节省了挖掘MS目标的时间。

我不是MSBuild的专家,但我可以使用这个链接的信息来完成相同的任务:

http://www.hanselman.com/blog/ManagingMultipleConfigurationFileEnvironmentsWithPreBuildEvents.aspx

在文章底部附近有一个与MSBuild相关的部分。 希望这可以帮助。