40

Is there a way to publish a web project in MS Visual Studio 2010 using CLI? I use DevEnv.exe /Build to build a project and it works fine, but I could not find option to Publish a project.

One other thing I want to mention. I am trying to publish web project NOT to the IIS directly. I have a location where I publish several projects and then build them automatically into NSIS bundle to be deployed.

sha
  • 17,824
  • 5
  • 63
  • 98

4 Answers4

49

From ASP.NET Web Deployment using Visual Studio: Command Line Deployment, you can use

msbuild myproject.csproj /p:DeployOnBuild=true /p:PublishProfile=MyPublishProfile

where MyPublishProfile is the profile name that you've already set up somewhere

KyleMit
  • 30,350
  • 66
  • 462
  • 664
dten
  • 2,364
  • 21
  • 28
30

What works best is to add following target to the project file:

<Target Name="AfterBuild">
   <Message Text="Copying to Deployment Dir:" />
   <Copy SourceFiles="@(Content)" DestinationFolder="..\XXX\%(Content.RelativeDir)" />
      <CreateItem Include="$(OutputPath)\*">
        <Output TaskParameter="Include" ItemName="Binaries"/>
      </CreateItem>
   <Copy SourceFiles="@(Binaries)" DestinationFolder="..\XXX\bin" />
</Target>

This way, whenever project got build (from command line or from IDE) it automatically get deployed to specified folder. Thank you everybody for pointing me to right direction.

KyleMit
  • 30,350
  • 66
  • 462
  • 664
sha
  • 17,824
  • 5
  • 63
  • 98
  • Problem I have with this is that it publishes both debug and release modes doesn't it? That could have serious consequences for a live site if your debug config gets published. Think what we need is a CLI batch that does: 'build Release and Publish with this publish setting'. – Rob Kent Jan 13 '12 at 17:01
  • 1
    @RobKent you can set up config file transformations which you can then use to trigger the above behavior only for certain build configurations. Just google .net config transforms. – Brian Sweeney Jun 13 '14 at 13:45
  • does it tale care of web.config merging? – Toolkit Jan 10 '21 at 12:39
6

The /t:publish switch is for ClickOnce applications only, it's not applicable to web projects. Hence the error saying it's unpublishable. :)

Ted Nyberg
  • 7,001
  • 7
  • 41
  • 72
0

@RobKent As https://stackoverflow.com/a/2775437/21233364 using

<Target Name="AfterBuild" Condition=" '$(Configuration)' == 'Release' ">
   <Message Text="Copying to Deployment Dir:" />
   <Copy SourceFiles="@(Content)" DestinationFolder="..\XXX\%(Content.RelativeDir)" />
      <CreateItem Include="$(OutputPath)\*">
        <Output TaskParameter="Include" ItemName="Binaries"/>
      </CreateItem>
   <Copy SourceFiles="@(Binaries)" DestinationFolder="..\XXX\bin" />
</Target>

you can have publish only on release compiling.

Paolo.T
  • 1
  • 1