17

I've got a series of .NET 4 based web applications (WCF and Web) within the same solution, but need to selectively publish, from the command line.

I've tried various things so far, MSBuild, aspnet_compiler, but nothing, to date has worked.

I need to be able to specify the Project, not the solution, have any transforms run and have the output redirected to a folder...basically mimick the right mouse click 'Publish' option, using the File System.

In addition to all of this, I'd like to leave the projects alone - not adding msbuild files all over the place, as this is a specific build, and not necessarily related to the project.

Stuff I've tried:

Community
  • 1
  • 1
Kieron
  • 26,748
  • 16
  • 78
  • 122
  • check the answer of this [question](http://stackoverflow.com/questions/776635/replicate-vs2008-publish-web-site-from-command-line) – Amir Ismail Aug 16 '11 at 10:51
  • 2
    Thanks, but that's for web projects, not web applications. They, unfortunately, don't work the same. – Kieron Aug 16 '11 at 10:53

4 Answers4

15

Save the following script as publishProject.bat

rem publish passed project 
rem params: %configuration% %destDir% %srcDir% %proj%
@echo off
SET DestPath=d:\projects\Publish\%2
SET SrcPath=d:\projects\Src\%3\
SET ProjectName=%4
SET Configuration=%1

RD /S /Q "%DestPath%" rem clear existed directory
:: build project
MSBuild  "%SrcPath%%ProjectName%.vbproj" /p:Configuration=%Configuration%
:: deploy project
::/t:TransformWebConfig
MSBuild "%SrcPath%%ProjectName%.vbproj" /target:_CopyWebApplication /property:OutDir=%DestPath%\ /property:WebProjectOutputDir=%DestPath% /p:Configuration=%Configuration%
xcopy "%SrcPath%bin\*.*" "%DestPath%\bin\" /k /y

echo =========================================
echo %SrcPath%%3.vbproj is published
echo =========================================

I call it from another batch file

@echo off
rem VS2010. For VS2008 change %VS100COMNTOOLS% to %VS90COMNTOOLS%
call "%VS100COMNTOOLS%\vsvars32.bat" 
SET ex=.\publishProject.bat Release

call %ex% KillerWebApp1 KillerWebApp1\KillerWebApp1 KillerWebApp1
call %ex% KillerWebApp2 KillerWebApp2\KillerWebApp2 KillerWebApp2
call %ex% KillerWebApp3 KillerWebApp3\KillerWebApp3 KillerWebApp3
call %ex% KillerWebApp4 KillerWebApp4\KillerWebApp4 KillerWebApp4

EDIT: Code above works for most cases but not for all. I.e. we use another asp .net application and link it as virtual folder in IIS. For this situation VS2008 worked fine with code above but VS2010 also copy files from virtual directory while deploying. The following code works properly also in VS2010 (solution was found here)

Add to your project file (*.csproj, *.vbproj)

<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>

Change publishProject.bat to:

rem publish passed project 
rem params: %configuration% %destDir% %srcDir% %proj%
@echo off
SET DestPath=d:\projects\Publish\%2
SET SrcPath=d:\projects\Src\%3\
SET ProjectName=%4
SET Configuration=%1

:: clear existed directory
RD /S /Q "%DestPath%"

:: build and publish project
MSBuild "%SrcPath%%ProjectName%.vbproj" "/p:Configuration=%Configuration%;AutoParameterizationWebConfigConnectionStrings=False;PublishDestination=%DestPath%" /t:PublishToFileSystem
Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
Cheburek
  • 2,103
  • 21
  • 32
4

I know this is an old question, but I just learned something, so I decided I'd share: While it is 100% true that the "_CopyWebApplication" target exists and works, as of .NET 4.0 it has been superseded by the "_WPPCopyWebApplication" target in Microsoft.Web.Publishing.targets, which supports new features like web.config transformation syntax, etc.

JerKimball
  • 16,584
  • 3
  • 43
  • 55
0

My solution for CCNET with the Web.config transformation:

<tasks>
    <msbuild>
        <executable>C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe</executable>
        <workingDirectory>E:\VersionesCC\Trunk_4\SBatz\Gertakariak_Orokorrak\GertakariakMS\Web</workingDirectory>
        <projectFile>GertakariakMSWeb2.vbproj</projectFile>
        <targets>Build</targets>
        <timeout>600</timeout>
        <logger>C:\Program Files\CruiseControl.NET\server\ThoughtWorks.CruiseControl.MSBuild.dll</logger>
        <buildArgs>
            /noconsolelogger /p:Configuration=Release /v:diag
            /p:DeployOnBuild=true
            /p:AutoParameterizationWebConfigConnectionStrings=false
            /p:DeployTarget=Package
            /p:_PackageTempDir=E:\Aplicaciones\GertakariakMS2\Web
        </buildArgs>
        </msbuild>
</tasks>
Asier
  • 57
  • 1
  • 1
    This answer lacks any explanantion on why you do it this way, what to do with the snippet you're listing, how all those hard-coded values need to be adapted to the reader's own situation, and what you mean by "for CCNET". – chiccodoro Mar 03 '15 at 14:35
0

Have you checked out WebDeploy?

This should do all the steps you need to have - it can bundle up a web app into a deployable file (a ZIP, basically), and there's an "engine" on the server that can interpret that deployable package and do all the heavy lifting for you.

Also see Scott Hanselman's Web Deployment Made Awesome: If You're Using XCopy, You're Doing It Wrong blog post - very enlightening!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • This is preparation for deployment to our controlled servers that we don't have access to, so we need to provide a folder with the code for the change team to deploy. – Kieron Aug 16 '11 at 10:56
  • @Kieron: why can't you provide a ZIP file instead of a directory?? – marc_s Aug 16 '11 at 10:57
  • Not my process unfortunately. But we're also not deploying to IIS, we're deploying to a folder. – Kieron Aug 16 '11 at 11:00