32

Good day!

I want to have ability to build ASP.NET MVC 2 project using VS2010 Publish dialog and from command-line.

For command-line I get the following to work:

C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe .\SolutionFolder\MyProject.csproj /p:Configuration=Release;DeployOnBuild=True;PackageAsSingleFile=False;outdir=c:\_OutputFolder\

The only problem I have that Web.config transformation are not applied (but added to WebDeploy package). I don't use WebDeploy. Is there any way to apply Web.config transformations?

Thanks!

artvolk
  • 9,448
  • 11
  • 56
  • 85
  • Possible duplicate of [MSBuild Script and VS2010 publish apply Web.config Transform](http://stackoverflow.com/questions/2905151/msbuild-script-and-vs2010-publish-apply-web-config-transform) – Frédéric Hamidi Jan 31 '11 at 11:37

3 Answers3

25

You can also try using the XDT Transformation Tool:

http://ctt.codeplex.com was moved to Github https://github.com/greenfinch/ctt

I'm using this instead of messing with obscure msbuild targets.

mybrave
  • 1,662
  • 3
  • 20
  • 37
Brian Chavez
  • 8,048
  • 5
  • 54
  • 47
20

Here is another approach, which uses msbuild to transform Web.config file:

http://sedodream.com/2010/04/26/ConfigTransformationsOutsideOfWebAppBuilds.aspx

In my tests the results were better. Basically, you create a project file to perform only a TransformXML task:

<Project ToolsVersion="4.0" DefaultTargets="Demo" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<UsingTask TaskName="TransformXml"
         AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.Tasks.dll"/>

<Target Name="Demo">
    <TransformXml Source="app.config"
                  Transform="Transform.xml"
                  Destination="app.prod.config"/>
</Target>
</Project>

Save the project file and then apply the transformation, running the following command:

msbuild trans.proj /t:Demo

Where trans.proj is the name of the project file and Demo is the name of task target.

Diogo
  • 616
  • 6
  • 10
  • 4
    This is an awesomely simple way to do this. Replace `v10.0` with `v15.0` for Visual Studio 2017 to allow it to find the dll. – Luke Jan 03 '18 at 23:10
  • This works wonders. It even output warnings for keys that couldn't be transformed (e.g. keys with a "replace" transform in the transform file that weren't found in the file the transform is being applied to). – Triynko Aug 28 '18 at 18:06
  • I can't get this work with Visual studio 2017 Community. I got this error: error MSB4062: The "TransformXml" task could not be loaded from the assembly C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v15.0\Web\Microsoft.Web.Pub lishing.Tasks.dll. Could not load file or assembly. – FredPonch Oct 29 '18 at 13:40
5

I think it's worth to mention that you can also use with PowerShell the DLL that Visual Studio is using: Microsoft.Web.XmlTransform.dll

PowerShell script, see: Web.Config transforms outside of Microsoft MSBuild?

To load the DLL instead of copying around, I do like this (so you see where to find this DLL, at least in my scenario at work we had to look-up these locations):

if (Test-Path "C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\Web\Microsoft.Web.XmlTransform.dll") {
    Add-Type -LiteralPath "C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\Web\Microsoft.Web.XmlTransform.dll"
} elseif (Test-Path "C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v12.0\Web\Microsoft.Web.XmlTransform.dll") {
    Add-Type -LiteralPath "C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v12.0\Web\Microsoft.Web.XmlTransform.dll"
} else {
    throw [System.IO.FileNotFoundException] "Microsoft.Web.XmlTransform.dll not found."
}
firepol
  • 1,731
  • 22
  • 39