MSBuild Script and VS2010 publish apply Web.config Transform

asp.netXmlVisual Studio-2010MsbuildWeb Config-Transform

asp.net Problem Overview


So, I have VS 2010 installed and am in the process of modifying my MSBuild script for our TeamCity build integration. Everything is working great with one exception.

How can I tell MSBuild that I want to apply the Web.conifg transform files that I've created when I publish the build...

I have the following which produces the compiled web site but, it outputs a Web.config, Web.Debug.config and, Web.Release.config files (All 3) to the compiled output directory. In studio when I perform a publish to file system it will do the transform and only output the Web.config with the appropriate changes...

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

Any help would be great..!

I know this can be done by other means but I would like to do this using the new VS 2010 way if possible

asp.net Solutions


Solution 1 - asp.net

I was looking for similar information and didn't quite find it, so I did some digging around in the .targets files that come with Visual Studio 2010 and MSBuild 4.0. I figured that was the best place to look for the MSBuild task that would perform the transformation.

As far as I have been able to tell, the following MSBuild task is used:

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

I have tested the above and can confirm that it works. You might need to tweak the structure a bit to fit with your build script better.

Solution 2 - asp.net

You should be able to accomplish this by using the Package target and specifying the temp directory.

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/

Solution 3 - asp.net

Alternatively, you try using the XDT Transformation Tool:

http://ctt.codeplex.com

I'm using this instead of messing with obscure msbuild targets. Works with app.config not just web.config.

Solution 4 - asp.net

It worked for me with the following change

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

From Microsoft.WebApplication.targets file under MsBuild folder

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

Solution 5 - asp.net

I'm no expert with MSBuild, but I was able to use the information from this link to accomplish the same task:

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

There is a section related to MSBuild near the bottom of the article. Hope this helps.

Solution 6 - asp.net

Another answer to this topic after I've been searching for days before I tackled this issue:

Your publish profile and configuration names should match.

In my case mine didn't. Manually publishing through the publishing profile gave me the result I wanted, because my configuration was set in the publish profile. MSBuild however tries to be intelligent and magically connects the publish profile and configuration based on name. (Adding the /p:Configuration in the command resulted in other strange errors about the outputpath of a referenced project).

Just to be exactly clear in what I mean:

MSBuild statement from command line

msbuild myproject.csproj -t:Clean -t:Rebuild /p:DeployOnBuild=true /p:PublishProfile="Development"

WORKS

  • Publish Profile name: Development
  • Solution Configuration name: Development

DOES NOT WORK

  • Publish Profile name: Development
  • Solution Configuration name: Dev

Hope this helps!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionxspydrView Question on Stackoverflow
Solution 1 - asp.netUmar Farooq KhawajaView Answer on Stackoverflow
Solution 2 - asp.netpattersoncView Answer on Stackoverflow
Solution 3 - asp.netBrian ChavezView Answer on Stackoverflow
Solution 4 - asp.netSyamView Answer on Stackoverflow
Solution 5 - asp.netJonathan S.View Answer on Stackoverflow
Solution 6 - asp.netHutjepowerView Answer on Stackoverflow