Replicate VS2008 "Publish Web Site" from command line
Asked Answered
M

11

36

I am trying to replicate the exact functionality of this dialogue in Visual Studio 2008 in a build script: alt text

This is an ASP.NET web site, not a web application.

I have Googled around this and turned up quite a bit of stuff involving MSBuild, but this all seems to concern solutions laid out as ASP.NET Web Applications:

http://www.driebier.net/post/Using-MSBuild-to-deploy-visual-studio-2005-web-applications.aspx http://blog.donnfelker.com/post/TFS-Build-Not-Publishing-Web-Applications.aspx

This article seems to be relevant to ASP.NET Web Sites, but I find that I'm getting an error when trying to build using those suggestions:

C:\dev\T&A>msbuild /t:_CopyWebApplication /property:OutDir=c:\temp\taweb\ /prope
rty:WebProjectOutputDir=c:\temp\taweb\

Microsoft (R) Build Engine Version 3.5.30729.1
[Microsoft .NET Framework, Version 2.0.50727.3074]
Copyright (C) Microsoft Corporation 2007. All rights reserved.

Build started 22/04/2009 11:50:42.
Project "C:\dev\T&A\TAWeb.sln" on node 0 (_CopyWebApplication target(s)).
  Building solution configuration "Debug|.NET".
C:\dev\T&A\TAWeb.sln : error MSB4057: The target "_CopyWebApplication" does not
 exist in the project.
Done Building Project "C:\dev\T&A\TAWeb.sln" (_CopyWebApplication target(s)) --
 FAILED.


Build FAILED.

"C:\dev\T&A\TAWeb.sln" (_CopyWebApplication target) (1) ->
  C:\dev\T&A\TAWeb.sln : error MSB4057: The target "_CopyWebApplication" does n
ot exist in the project.

0 Warning(s)

1 Error(s)

Time Elapsed 00:00:00.06

The solution I'm trying to publish (inherited, not my own) doesn't have .csproj files (where I could import the _CopyWebApplication target from C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v9.0\WebApplications\Microsoft.WebApplication.targets)

Perhaps this is a Visual Studio 2005/2008 difference?

Anyway, I feel that I'm going down the wrong path there.

Essentially I just need to achieve exactly what the above dialogue does, but from the command line.

Thanks very much

Mutable answered 22/4, 2009 at 10:58 Comment(0)
P
38

The following command duplicates the Publish Web Site dialog with default settings.

Command for Publish Web Site with Default Settings

aspnet_compiler -nologo -v / -p "C:\WebSite1" -u "C:\TargetPath"

Reference

1) See Community Content titled You want Publish a site but you have not Visual Studio then... at http://msdn.microsoft.com/en-us/library/20yh9f1b(classic).aspx.

  • Microsoft Visual Studio 2005 > Visual Studio 2005 Command Prompt
  • Microsoft Visual Studio 2008 > Visual Studio 2008 Command Prompt
  • Microsoft .NET Framework SDK v2.0 > SDK Command Prompt

2) See "ASP.NET Compilation Tool (Aspnet_compiler.exe)" at http://msdn.microsoft.com/en-us/library/ms229863.aspx.

3) Following excerpt from Walkthrough: Deploying an ASP.NET Web Application Using XCOPY at http://msdn.microsoft.com/en-us/library/f735abw9.aspx

As an alternative to using the XCOPY command-line tool, which is supported by all versions of the .NET Framework, you can use the new .NET Framework 2.0 tool located at %SystemRoot%\Microsoft.NET\Framework\version 2 or later\Aspnet_compiler.exe to compile and deploy your Web application. For more information, see ASP.NET Compilation Tool (Aspnet_compiler.exe).

4) Following excerpt from How to: Precompile ASP.NET Web Sites for Deployment at http://msdn.microsoft.com/en-us/library/ms227976.aspx.

If your Web site is not an Internet Information Services (IIS) application and therefore has no entry in the IIS metabase, used the following value for the -v switch.

aspnet_compiler -p physicalOrRelativePath -v / targetPath

In this case, the physicalOrRelativePath parameter refers to the fully qualified directory path in which the Web site files are located, or a path relative to the current directory. The period (.) operator is allowed in the physicalOrRelativePath parameter. The -v switch specifies a root that the compiler will use to resolve application-root references (for example, with the tilde (~) operator). When you specify the value of / for the -v switch the compiler will resolve the paths using the physical path as the root.

Piquet answered 28/10, 2009 at 23:24 Comment(1)
@AMissico, do you have to run the aspnet_compiler command manually? (I will upvote your answer). Thanks.Villagomez
W
6

I think you are looking for the AspNetCompiler task

<Target Name="PublishToIIS" DependsOnTargets="Publish">
    <AspNetCompiler    
            VirtualPath="$(IISVirtualPath)"
            TargetPath="$(IISTargetPath)"
            PhysicalPath="$(MSBuildProjectDirectory)/trunk/InternalAppCS/Web.UI/"
            Force="true"
            Debug="$(IISDebug)"
     />

</Target>
Wish answered 22/4, 2009 at 11:35 Comment(2)
Hmmm, everything seems to lead back to aspnetcompiler in the end - but my understanding is that you need a target IIS with a virtual directory for the site, although this seems a bit ambiguous. I will do some more digging on this later.Mutable
You can publish to any remote directory without a virtual directory. I use this for my MVC projects and just set TargetPath to a shared directory on the web server.Flavorsome
T
6

This "magic" combination does what you're looking for. )It only took two days to get the right combination for my project.) The key is to include the _CopyWebApplication target and the ResolveReferences target.

msbuild "/t:_CopyWebApplication;ResolveReferences;publish" /p:OutDir="C:\inetpub\wwwroot\[appname]\bin\" /p:WebProjectOutputDir="C:\inetpub\wwwroot\[appname]" c:\directory\[appname].csproj    
Tradeswoman answered 8/10, 2010 at 18:12 Comment(1)
Sorry for commenting on a 6 year old post, but this appears to relate to Web Applications, not Web Sites. Visual Studio Web Site applications do not have a .csproj file.Mutable
L
5

Add to your .csproj file:

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

Change "..\PreCompiledWeb" for the folder you want to publish to or you can specify a variable something like: $(OutputFolder) which you can pass

then go the folder of your webapplication and do:

msbuild /t:Build

Then you can copy those files wherever you want using xcopy:

xcopy "..\PreCompiledWeb\*.*" "C:\MySite\" /e

That should do it.

Lisp answered 10/9, 2009 at 21:47 Comment(1)
Such a beautiful hack :-) saves my evening!Medawar
W
4

Personally I use buildbot which runs commands for me, I had to create a VBS script which performs the upload for me.

I installed WinSCP to do the ftp work and then just scripted the upload:

Set WshShell = CreateObject("WScript.Shell")
sCmd1 = """C:\Program Files\WinSCP\winscp.com"" <myusername> /command ""option batch on"" ""option confirm off"" ""put " & DefaultPath & strResult & "\" & DefaultFileName & " /Usr/<myuser>/" & updateType & "/" & strResult & "/"" ""exit"""

To pre-compile the website from a command line I do the following, however I do this on the web server rather than before uploading it:

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_compiler -p "D:\<mycompany>\Backedup\Web Sites\<mysite\Root" -v /
Washington answered 22/4, 2009 at 11:23 Comment(3)
My question is about precompiling an ASP.NET web site before putting it on a web server, not moving files around a network.Mutable
I edited my answer to help, its not the correct answer but might provide a hint.Washington
Thanks, I intend to revisit using aspnet_compiler directly some time this week. I'll update my post with my experiences.Mutable
E
4

I have been using msbuild for exactly what you describe. Have you tried setting the property like this?

 /property:"OutDir=c:\temp\taweb\;WebProjectOutputDir=c:\temp\taweb\"

If it still does not work, let me know and I can send you my bat file, that kicks of the msbuild script, that does the svn get, builds the assembly.info file, deploy the web site, and finally runs a http get on the site home page, just to make sure it built and deployed correctly.

Hope it helps Rihan

Enchorial answered 29/4, 2009 at 11:29 Comment(0)
M
4

I was struggling with the same error (MSB4057: The target "_CopyWebApplication" does not exist in the project.

Yes. I was using a Web Application Project (not a web site).

I was baffled because I had one project that worked, and one that did not. So I sat down with ExamDiffPro and went to work comparing the project files. What I found was the section of build target includes at the bottom of the project files were different.

One project (that was working) was created using a newer version of Visual Studio. The other one (that was NOT working) was created years ago and has been upgraded over the years, to the current version I am working with. The upgraded project, evidently didn't get upgraded with the new build targets as newer versions of Visual Studio became available.

At the bottom of the project file which was working, I found the following:

<Import Project="$(MSBuildBinPath)\Microsoft.VisualBasic.targets" />

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

The second import line was NOT in the project which was causing problems.

I simply copied the import from the working file, and pasted it in the relative location in the non-working file, and viola!

While this is not a direct solution to the initial problem defined in this thread, I hope this helps some of the other people that stumbled across this thread, looking for similar issues, only with actual Web Application Projects.

Methylene answered 20/9, 2011 at 0:59 Comment(0)
D
3

Here's a little PowerShell script that builds a solution, then publishes a web project from it.

The MSBuild is for .Net 3.5, so change to "\v4.0\" if needed.

$build = "$env:windir\Microsoft.NET\Framework\v3.5\MSBuild.exe"

$SolutionPath = "C:\Projects\AdminWebSite"
$SolutionFile = "AdminWebSite.sln"
$WebProjectFile = "Admin.Web\Admin.Web.csproj"
$OutputPath = "C:\PublishedSites\Hosts\adminweb"

& $build "$SolutionPath\$SolutionFile" /t:rebuild
& $build "$SolutionPath\$WebProjectFile" "/t:ResolveReferences;_CopyWebApplication;publish" /p:OutDir="$OutputPath\bin\" /p:WebProjectOutputDir="$OutputPath"

The "ResolveReferences" must come before "_CopyWebApplication" otherwise dependencies go missing.

The example paths given here build to:

  • C:\Project\AdminWebSite\AdminWebSite.sln
  • C:\Project\AdminWebSite\Admin.Web\Admin.Web.csproj
Dyewood answered 7/4, 2011 at 11:42 Comment(2)
Appears this is for ASP.NET Web Applications, not ASP.NET Web Sites. The distinction is that Web Site projects do not have a project file, only a solution file. That's why this question is tricky. Thanks anyway though.Mutable
IIRC, Visual Studio creates a temporary "fake" Solution file when it builds/publishes a csproj.Dyewood
T
2

The error you received (C:\dev\T&A\TAWeb.sln : error MSB4057: The target "_CopyWebApplication" does not exist in the project) is due to two issues. First: the target _CopyWebApplication must be called on a WebApplication project file, not a solution. Second: a website does not have a project file, WebApplications have project files.

The _CopyWebApplication target only applies to publishing a WebApplication project. Use the AspNetCompiler MSBuild task must be used to publish a website.

When you publish a website in visual studio 2008, the second line of output is "Pre-compiling Web Site". The options visual studio displays for publishing a website map directly to options for the AspNetCompiler and Aspnet_compiler.exe.

Though I'm not the first here to say "use AspNet_compiler", I thought describing the reasons why may be helpful. On a side note, I think the AspNet_compiler can be used to publish a webapp but I need to do some more testing.

Tarnetgaronne answered 27/10, 2010 at 17:26 Comment(0)
S
1

I use bat file using msbuild.exe (.net 3.5, vs 2008) for "publish" my website (vbproj) in a folder.

%msBuildDir%\msbuild "D:\Project1\Client\WebPresentation\ConsultaOperaciones.vbproj" /t:ResolveReferences;Rebuild /p:BuildingProject=true;OutDir=D:\Instalaciones\ultima\PublicacionWeb\OutDir\;WebProjectOutputDir=D:\Instalaciones\ultima\PublicacionWeb\WebProjectDir\

Shirlyshiroma answered 20/7, 2011 at 18:16 Comment(0)
V
-1

There's nothing magical about the "Publish..." feature that you couldn't recreate on your own, especially since you are targeting a network file share.

At it's core, all it's doing is copying your files from one place to another. Using NAnt, you can pull this off with a copy task or you can use an exec task to call xcopy If you're using a build tool other than NAnt, I'm sure there is support for similar tasks.

If you want to leave your raw code and debugging information behind, you can exclude files that end with .cs or .pdb. Both NAnt copy and xcopy provide easy ways to do this.

Viscus answered 22/4, 2009 at 11:27 Comment(2)
The Publish feature doesn't just copy files around, it pre-compiles the ASP.NET Web Site, putting all the code in .DLLs, and putting placeholders all over the resulting tree where .aspx pages (containing markup) would have been. That's the bit I'm trying to automate, not the "moving files around" bit. The point here is that I don't want the web server to be compiling source code.Mutable
Yep, my misunderstanding. I was thinking web application, not web site.Viscus

© 2022 - 2024 — McMap. All rights reserved.