Replicate VS2008 “Publish Web Site” from command l

2019-01-10 03:56发布

问题:

I am trying to replicate the exact functionality of this dialogue in Visual Studio 2008 in a build script: alt text http://i41.tinypic.com/1osl1f.png

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

回答1:

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.



回答2:

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>


回答3:

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    


回答4:

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.



回答5:

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 /


回答6:

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



回答7:

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.



回答8:

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


回答9:

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.



回答10:

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\



回答11:

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.