Menu

MSBuild, Publish Profiles and Web Site Projects

Gotcha with Publish Profiles for Websites

Microsoft recently released ASP.NET and Web Tools 2012.2. You can read
about it here.
One of the things I was particularly excited about was the fact that web site projects (not web application projects) were getting the
same publishing tooling as web application projects. Cue everyone asking
why am I still using Web Site Projects rather than Web Application
projects... A post for another day...

I have been trying to get MSBuild to play nice with the publish
profiles. I used the tooling to create a new publish profile. The tool
creates two files inside the website:

website.publishproj - this is basically an msbuild file

App_Data\PublishProfiles\%PROFILENAME%.pubxml - this is a
settings file that stores properties.

I then tried to build the project file, by running the following in the
command line:

Here is a brief explanation of the flags I have used:

DeployOnBuild: tells msbuild to do a deploy

PublishProfile: Name of the publish profile I created earlier

VisualStudioVersion: This is required when building a project
file rather than solution, see
here.

Logger: Whilst debugging I needed logging, so this gives
me that.

When I ran this, I was getting the following error:

C:\Program Files
(x86)\MSBuild\Microsoft\VisualStudio\v11.0\Web\Microsoft.Web.Publishing.targets(4253,5):
error : The value for PublishProfile is set to 'QA.pubxml', expected
to find the file at 'E:\Dev\trunk\website__profiles\QA.pubxml'
but it could not be found.

Clearly MSBuild is looking in the wrong place for the qa.pubxml file. It
seems to be looking in a folder called "__profiles" rather than
"App_Data\publishprofiles\QA.pubxml". I debugged this and eventually
found an issue with the Microsoft.Web.Publishing.targets file. It turns
out that if you specify an extension (.pubxml) on your profile name,
MSBuild will look in the wrong folder. I reran the MSBuild command
below:

It started working perfectly.

Details on the issue

The following section exists in the Microsoft.Web.Publishing.target
file:

As you can see here (in the second PropertyGroup) the property WebPublishProfileFile will only get set correctly to the App_Data
path if PublishProfileName != '' **AND **WebPublishProfileFile
''. **If we look at the first **PropertyGroup now, we can see that PublishProfileName **only gets set if **_PublishProfileExtension
'' **AND **_PublishProfileDirectory == ''. ** During my
debugging, I could see that **_PublishProfileExtension was set to
".pubxml" so the condition would fail. I removed the ".pubxml" from the
PublishProfile MSBuild property as shown above and it started
working.