The easiest way to publish a Visual Studio web project from the command line is to follow the steps below.
- Open VS
- Right click on the Web project and select Publish
- Import your .publishSettings file (or manually configure the settings)
- Save the profile (i.e. .pubxml file)
- From the command line publish with the command line passing in PublishProfile
For more details on this you can see ASP.NET Command Line Deployment. This is pretty simple and very easy, but it does require that you manually create the .pubxml file. In some cases you’d just like to download the .publishsettings file from your hosting provider and use that from the command line. This post will show you how to do this.
In order to achieve the goal we will need to extend the build/publish process. There are two simple ways to do this; 1. Place a .wpp.targets file in the same directory as the web project or 2. Pass an additional property indicating the location of the .wpp.targets file. I’ll first go over the technique where you place the file directly inside of the directory where the project is. After that I’ll show you how to use this file from a well known location.
One way to do this is to create a .wpp.targets file. This .wpp.targets file will be imported into the build/publish process automatically. This .targets file will enable us to pass in PublishSettingsFile as an MSBuild property. It will then read the .publishsettings file and output the properties needed to execute the publish process.
.wpp.targets in the project directory
Let’s take a look at the .targets file and then we will discuss it’s contents. Below you will find the contents of the full file.
MSDeploy WebPublish GetPublishPropertiesFromPublishSettings; $(PipelineDependsOn); <_BaseQuery>/publishData/publishProfile[@publishMethod='MSDeploy'][1]/ WMSVC <_MSDeployXPath Include="WebPublishMethod"> $(_BaseQuery)@publishMethod <_MSDeployXPath Include="MSDeployServiceURL">$(_BaseQuery)@publishUrl <_MSDeployXPath Include="SiteUrlToLaunchAfterPublish">$(_BaseQuery)@destinationAppUrl <_MSDeployXPath Include="DeployIisAppPath">$(_BaseQuery)@msdeploySite <_MSDeployXPath Include="UserName">$(_BaseQuery)@userName <_MSDeployXPath Include="Password">$(_BaseQuery)@userPWD
You can place this file in the root of your project (next to the .csproj/.vbproj file) with the name {ProjectName}.wpp.targets.
This .targets file is pretty simple. It defines a couple properties and a single target, GetPublishPropertiesFromPublishSettings. In order to publish your project from the command line you would execute the command below.
msbuild.exe MyProject /p:VisualStudioVersion=11.0 /p:DeployOnBuild=true /p:PublishSettingsFile=
Here is some info on the properties that are being passed in.
The VisualStudioVersion property indicates that we are using the VS 2012 targets. More info on this at http://sedodream.com/2012/08/19/VisualStudioProjectCompatabilityAndVisualStudioVersion.aspx.
DeployOnBuild, when true it indicates that we want to publish the project. This is the same property that you would normally pass in.
PublishSettingsFile, this is a new property which the .targets file recognizes. It should be set to the path of the .publishSettings file.
The properties at the top of the .targets file (WebPublishMethod and DeployTarget) indicate what type of publish operation is happening. The default values for those are MSDeploy and WebPublish respectively. You shouldn’t need to change those, but if you do you can pass them in on the command line.
The GetPublishPropertiesFromPublishSettings target uses the XmlPeek task to read the .publishsettings file. It then emits the properties required for publishing.
OK this is great an all, but it still requires that an additional file (the .wpp.targets file) be placed in the directory of project. It is possible to avoid this as well. Let’s move to that
.wpp.targets from a well known location
If you’d like to avoid having to place the .wpp.targets file in the directory of the project you can easily do this. Place the file in a well known location and then execute the same msbuild.exe call adding an additional property. See the full command below.
msbuild.exe MyProject /p:VisualStudioVersion=11.0 /p:DeployOnBuild=true /p:PublishSettingsFile=/p:WebPublishPipelineCustomizeTargetFile=
Once you do this you no longer need to create the Publish Profile in VS if you want to publish from the command line with a .publishsettings file.
FYI you can find the complete sample at https://github.com/sayedihashimi/publish-samples/tree/master/PubWithPublishSettings.
Sayed Ibrahim Hashimi | http://msbuildbook.com | @SayedIHashimi
Comments are closed.