Publishing a ClickOnce project with command-line MSBuild

You can build the Publish target of a .csproj with the command-line MSBuild.exe, but when the Visual Studio IDE publishes a project it also copies it to a designated location and in Beta 2 appears to do something clever with preserving old .application manifests.

Is there a way to have MSBuild.exe perform these extra steps from the command line Perhaps I'm building the wrong target


Answer this question

Publishing a ClickOnce project with command-line MSBuild

  • Hany M. Magdy

    FYI, This was the solution to my problem...

    <Project DefaultTargets="DoPublish" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
        <PropertyGroup>
     <MSBuildCommunityTasksPath>$(MSBuildProjectDirectory)\lib\msbuild</MSBuildCommunityTasksPath>
        </PropertyGroup>
        <Import Project="$(MSBuildCommunityTasksPath)\MSBuild.Community.Tasks.Targets"/>
        <PropertyGroup Condition="!Exists('$(Configuration)')">
            <Configuration>Debug</Configuration>
        </PropertyGroup>
        <PropertyGroup Condition="!Exists('$(CCNetLabel)')">
            <CCNetLabel>0</CCNetLabel>
        </PropertyGroup>
        <PropertyGroup>
     <ClickOnceBuildDirectory>AlgoCollateral\bin\$(Configuration)\AlgoCollateral.publish</ClickOnceBuildDirectory>
            <Version>1.0.0.$(CCNetLabel)</Version>
    <ClickOnceInstallDirectory>\\mtlv5webdev\Deployment\AlgoCollateral</ClickOnceInstallDirectory>
        </PropertyGroup>
        <ItemGroup>
            <ClickOnceFiles Include="$(ClickOnceBuildDirectory)\**\*.*"/>
            <IndigoServiceFiles Include="Algo.Collateral.Services\**\*.*"/>
        </ItemGroup>
        <Target Name="DoPublish">
            <MSBuild Projects="AlgoCollateral.v5.sln" Targets="Clean;Build" Properties="ApplicationVersion=$(Version)"/>
            <MSBuild Projects="AlgoCollateral/AlgoCollateral.csproj" Targets="Publish" Properties="ApplicationVersion=$(Version)" />
            <!-- Write publish.htm file for ClickOnce -->
            <Copy SourceFiles=".\Build\publish.htm" DestinationFiles="$(ClickOnceInstallDirectory)\publish.htm"/>
            <Copy SourceFiles="AlgoCollateral\bin\$(Configuration)\AlgoCollateral.application" DestinationFiles="$(ClickOnceInstallDirectory)\AlgoCollateral.application"/>
            <FileUpdate Files="$(ClickOnceInstallDirectory)\publish.htm"
                        IgnoreCase="true"
                        Multiline="true"
                        Singleline="false"
                        Regex="{VERSION}"
                        ReplacementText="$(Version)"
                        Force="true"/>                      
            <!-- Move Click Once Files -->
            <MakeDir Directories="$(ClickOnceInstallDirectory)"/>
            <Copy SourceFiles="@(ClickOnceFiles)"
     DestinationFiles="@(ClickOnceFiles->'$(ClickOnceInstallDirectory)\%(RecursiveDir)%(Filename)%(Extension)')"/>     
        </Target>
    </Project>


  • SHB

    When looking at the publish.htm, that file contains no info about what version of all those present is the current one. However, it contains some java script dealing with reg exp which appears to figure those things out for itself. Thus, there should be no reason to actually regenerate that file. The fact that the IDE does regenerate it every time may very well mean nothing at all. In fact I tried exchanging the newly generated file for the file from the last version with no ill effects at all.

    I'm also having trouble with the version number thingy. You can supply the version number as a parameter (not bad at all if you execute your msbuild from some other application, like NAnt). However, it doesn't really seem to care what version you supply, so I guess I've got it all wrong.

  • aing

    Upon further investigation into the Microsoft.Common.targets file in C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727, I found the PublishDir property used... Once I added it to the msbuild command line arguments (/p:PublishDir=c:\Inetpub\wwwroot\publish), then msbuild properly built the ClickOnce deployment there... Next up, I need to figure out how to get the folder to auto increment--as done by Visual Studio. All this done to hopefully configure CruiseControl.Net to auto-deploy my ClickOnce project.

    --
    Brady P. Merkel
    Go Gators! --;==;<

  • TJM_MC

    When you run "msbuild /t:publish" is it possible to have it create the publish.htm page and also iterate the version # of the folder


  • isjam

    This answer is actually wrong...it will publish to the directory of your choice but it doesn't create the Publish.htm. The settings within a .csproj file are msbuild targets, tasks, and properties. The property for where to publish can be passed in.

    This works fine:

    <MSBuild Projects="@(ProjectsToBuild)"

    Targets="Publish"

    Properties="Configuration=$(Configuration);

    Platform=$(Platform);

    PublishDir=$(BuildDropLocation)\;

    ApplicationVersion=$(ApplicationVersion);

    PublisherName=$(Company);

    PublishUrl=http://localhost/SmartClient/;

    InstallUrl=http://localhost/SmartClient/">

    <Output ItemName="OutputFiles" TaskParameter="TargetOutputs"/>

    </MSBuild>

    Since the property can be passed in via an MSBuild script then it can be passed in via commandline too.

    -Ken Villines


  • Brian Rak

    Can you tell me how you're keeping track/incrementing the ApplicationVersion number It appears you are simply passing it in through the CCNetLabel property. That's my biggest challenge right now with trying to build ClickOnce from the command-line.

  • OliSchwarz

    Graeme,

    MSBuild won't actually perfom the publish (i.e. the physical transfer of files etc) when building from the command-line - as you've discovered. The simple reason is that the functionality to do that is in the IDE itself.

    MSBuild will only perform the build related tasks.

    Sorry!

    ..Kieran

  • David Lundell

    Has anyone had any luck with msbuild command line generating the publish.htm file I am able to get it to publish to the directory of my choice (/p:PublishDir) but it does not produce two files... AppName_1_0_0_0.application and publish.htm. Am I missing something
  • Reed14676

    I'm confused after having read through the above.

    So, can I (or can I not) use MSBuild to publish my Win app to a specified URL

    For our app, we must publish it to 10 different servers (10 different contained cattle feed yards). So I would love to have my version ready to be deployed, build it, and then go to the command prompt and be able to type something like:

    msbuild /target:publish /property:PublishUrl=http://123.45.67.89/MyApp
    msbuild /target:publish /property:PublishUrl=http://123.45.67.90/MyApp
    msbuild /target:publish /property:PublishUrl=http://123.45.67.91/MyApp

    etc

    because then I could write a script to automate the publish to multiple locations instead of having to do it 10x in the VS IDE every time)

    But from everything I'm reading, MSBuild will not publish to a URL, but only to a local folder. And it does not handle publish.htm and other needed files.

    So should I abandon MSBuild or will it be able to do what I need

    Thanks!
    Ron



  • Amir Duany

    Sorry.
    I was wrong. Stupidity is a desease hard to cure, so please don't hit me. It'll only make it worse...

  • skayal

    the CCNetLabel comes from the "CruiseControl.net" label the it increments on its own.

  • reevolution

    I'm having trouble with too. Running msbuild from the command line to publish, everything seems identical to running publish from the IDE except the publish.htm file is missing. Does anyone have any ideas

  • C Davis

    Couldn't you use msbuild to publish to a local dir on the web server (publishDir), and later put a virtual directory matching publishUrl on top of it so it becomes accessible via your publishUrl
  • Publishing a ClickOnce project with command-line MSBuild