Publish ASP.Net web application using MSBuild and PowerShell

PowerShell script to build and publish ASP.Net web application, applying web.config tranformations, using MSBuild.

Considerations:

  • This works for a solution having  7 projects: 1 web application project and 6 related others (DataAccess, BussinessLogic,DomainEntities, Utils, etc).
  • All of these projects run under .Net Framework 3.5.
  • I’m using Visual Studio 2010.
  • Finally I got a MSBUILD error MSB1008 (Only one project can be specified.), but anyway, build and publish worked.

PowerShell script

$build = "$env:windir\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe"
$SolutionPath = "D:\SOLUTION_PATH"
$SolutionFile = "SEGU.sln"
$WebProjectFile = "SEGU\SEGU.csproj"
$WebConfigTransform = "Build-Web.Config.xml"
$OutputPath = "D:\SOLUTION_PATH\PrecompiledWeb\SEGU"
& $build "$SolutionPath\$SolutionFile" /t:rebuild
& $build "$SolutionPath\$WebProjectFile" "/t:ResolveReferences;_CopyWebApplication;publish" /p:OutDir="$OutputPath\bin\" /p:WebProjectOutputDir="$OutputPath"
& $build "$SolutionPath\$WebConfigTransform"

Build-Web.config.xml

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<UsingTask TaskName="TransformXml" AssemblyFile="C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.Tasks.Dll"/>
<Target Name="GenerateConfigs">
<Message Text="Transfom Config." />
<TransformXml Source="SEGU/Web.config" Transform="Web.Release.Config" Destination="PrecompiledWeb/SEGU/Web.config" />
<Message Text="Done Transfom Config." />
</Target>
</Project>

Web.Release.config

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<appSettings>
<add key="UserName" value="myuser" xdt:Transform="SetAttributes" xdt:Locator="Match(key)"/>
<add key="Password" value="password" xdt:Transform="SetAttributes" xdt:Locator="Match(key)"/>
<add key="SERVERNAME" value="192.168.10.14" xdt:Transform="SetAttributes" xdt:Locator="Match(key)"/>
<add key="urlServerReporting" value="http://RELEASE_SERVER/ReportServer" xdt:Transform="SetAttributes" xdt:Locator="Match(key)"/>
</appSettings>
<connectionStrings>
<add name="ConSEG" connectionString="Data Source=RELEASE_SERVER;Initial Catalog=Seguros;User ID=username;pwd=password" xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
  <system.web>
  <compilation xdt:Transform="RemoveAttributes(debug)" />
  </system.web>
</configuration>

References:

Replicate VS2008 “Publish Web Site” from command line
How do I publish a Asp.net web application using MSBuild?
How to Publish Web with msbuild?
MSBuild Error MSB1008
MSBUILD : error MSB1008: Only one project can be specified. Switch: Education

Web config transformations

Web.config Transformation Syntax for Web Application Project Deployment
How to: Transform Web.config When Deploying a Web Application Project
Transform app.config and web.config
Web.config Transformation Tester
_CopyWebApplication with web.config transformations
Using MSBuild 4.0 web.config Transformation to Generate Any Config File
Config transformations outside of web app builds
Deploying an ASP.NET Web Application to a Hosting Provider using Visual Studio or Visual Web Developer: Web.Config File Transformations – 3 of 12

2 responses to this post.

  1. […] Publish ASP.Net web application using MSBuild and PowerShell – 31/5/2012 · PowerShell script to build and publish ASP.Net web application, applying web.config tranformations, using MSBuild. Considerations: This works for a …… […]

    Responder

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s

A %d blogueros les gusta esto: