6

In Visual Studio 2010, you know how you can change your configuration (debug, release, etc), right-click a project in the solution explorer, click publish, and have all the important web app project files for the selected configuration copied to a target folder along with an xdt-transformed web.config? Well, I am looking for the MSBUILD equivalent of exactly that.

My challenge to you: Provide the ONE LINE that I need to execute at my command prompt in order to accomplish this. No third party programs. No tutorial videos. Just a single, straight-up command that I can literally copy from your response, paste into a command window, modify as necessary to support my directory structure, and then hit enter.

If not, then perhaps someone could provide a link to a complete MSBUILD reference showing every command, switch, and value I can use at the command line.

oscilatingcretin
  • 10,457
  • 39
  • 119
  • 206

2 Answers2

6

Put the below to ProjectPublish.MSBuild.xml file (change PropertyGroup as needed):

<?xml version="1.0" encoding="utf-8" ?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Publish"> 
    <PropertyGroup>
        <ProjectFile>Path\To\Web.csproj</ProjectFile>
        <PublishDir>Path\For\Publish\Output</PublishDir>
        <TempDir>Path\To\Temp\Folder</TempDir>
        <BuildConfig>Release|Debug</BuildConfig>
    </PropertyGroup>

    <Target Name="Publish">
        <MSBuild Projects="$(ProjectFile)"
                 Properties="Configuration=$(BuildConfig);WebProjectOutputDir=$(PublishDir);OutDir=$(TempDir)\;BuildingProject=true"
                 Targets="ResolveReferences;_CopyWebApplication" />
    </Target>
</Project>

Calling this from command line (or .bat file) should do the trick:

%windir%\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe ProjectPublish.MSBuild.xml
Ramunas
  • 3,853
  • 1
  • 30
  • 31
  • Never seen this kind of thing before. Do I just add that XML file to the project? I currently don't have such an XML file. – oscilatingcretin Apr 18 '11 at 20:16
  • It's an XML file that MSBuild understands and can process. Your project and solution files are of the same breed. Having such files near/in your solution makes life easier – Ramunas Apr 19 '11 at 06:36
  • So I just tried this and the publish seems to work. However, it doesn't appear to apply transformations. When I publish manually, my web.config is properly transformed. Any idea what I can do? – oscilatingcretin Apr 22 '11 at 20:05
  • http://stackoverflow.com/questions/2905151/msbuild-script-and-vs2010-publish-apply-web-config-transform – Ramunas Apr 24 '11 at 20:41
0

I found the solution I was looking for after all these months here

In case the above link goes bad, here's the skinny of what it says:

Unload then edit your project file. Look for the line where it's importing Microsoft.WebApplication.targets. Will look like:

<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" />

Beneath that line, paste in this XML:

<Target Name="PublishToFileSystem" DependsOnTargets="PipelinePreDeployCopyAllFilesToOneFolder">
    <Error Condition="'$(PublishDestination)'==''" Text="The PublishDestination property must be set to the intended publishing destination." />
    <MakeDir Condition="!Exists($(PublishDestination))" Directories="$(PublishDestination)" />

    <ItemGroup>
        <PublishFiles Include="$(_PackageTempDir)\**\*.*" />
    </ItemGroup>

    <Copy SourceFiles="@(PublishFiles)" DestinationFiles="@(PublishFiles->'$(PublishDestination)\%(RecursiveDir)%(Filename)%(Extension)')" SkipUnchangedFiles="True" />
</Target>

Now, run this in a command prompt within the same folder as your project file:

msbuild TestWebApp.csproj "/p:Platform=AnyCPU;Configuration=Debug;PublishDestination=C:\pub" /t:PublishToFileSystem

Remember to specify the path to MSBUILD in the command or add the path to your global path environmental variable (which is what I did). On my machine, it was here:

C:\Windows\Microsoft.NET\Framework\v4.0.30319

To test this, I put a config transform in my Web.Release.config to add an AppSetting key (if you do this, make sure the AppSettings node is present in your base config file or you will get an error). When I used the above command to build the debug configuration, the key was not present in the published config file as expected. However, when I used the release config, the key was successfully added to the file.

I really wish Microsoft hadn't obfuscated the heck out of this. At any rate, this is the simplest solution I have found anywhere on the internet. I hope it helps the rest of you.

oscilatingcretin
  • 10,457
  • 39
  • 119
  • 206
  • 1
    Unmarked as answer. Apparently, this doesn't work right when you're referencing another project in your web app. Researching it and will post an update when I find an answer. – oscilatingcretin Aug 22 '11 at 23:24