MsBuild and MsDeploy with multiple environments

The first attempt failed because Package target doesn’t exist in the solution file. When using MSBuild on a solution file, a temporary MSBuild project is created (SamplePackage.sln.metaproj); this project file contains only some targets (Build, Clean, Rebuild, Publish, …)

Solution : DeployOnBuild & DeployTarget properties

One way to do what you want is to use DeployOnBuild property like this :

<PropertyGroup Condition="'$(Configuration)' == ''">
  <Platform>Any Cpu</Platform>
  <Configuration>Dev</Configuration>
  <PackageLocation>$(MSBuildProjectDirectory)\package.zip</PackageLocation>
</PropertyGroup>

<Target Name="Build">
  <MSBuild Projects="SamplePackage.sln"
           Targets="Build"/>
</Target>

<Target Name="BuildWebPackage">
  <MSBuild Projects="SamplePackage.sln"
           Properties="Platform=$(Platform);
                       Configuration=$(Configuration);
                       DeployOnBuild=true;
                       DeployTarget=Package;
                       PackageLocation=$(PackageLocation);"/>
</Target>
  • DeployOnBuild=true : deployment must be made when Build is called
  • DeployTarget=Package : for deployment creates a package
  • PackageLocation : indicates the filepath of the package file

Additional links :

  • Creating web packages using MSBuild
  • How can I get TFS2010 to run MSDEPLOY for me through MSBUILD?
  • How to “Package/Publish” Web Site project using VS2010 and MsBuild
  • Automated Deployment in ASP.NET 4 – Frequently Asked Questions

Leave a Comment