如何使用msbuild发布网页?
Visual Studio 2010 有一个发布命令,允许您将 Web 应用程序项目发布到文件系统位置。我想在我的 TeamCity 构建服务器上执行此操作,因此我需要使用解决方案运行程序或 msbuild 来执行此操作。我尝试使用 Publish 目标,但我认为这可能适用于 ClickOnce:
msbuild Project.csproj /t:Publish /p:Configuration=Deploy
我基本上想要完全执行 Web 部署项目的操作,但没有加载项。我需要它来编译 WAP、删除执行所需的任何文件、执行任何 web.xml 文件。配置转换,并将输出复制到指定位置。
我的解决方案,基于 Jeff Siver 的回答
<Target Name="Deploy">
<MSBuild Projects="$(SolutionFile)"
Properties="Configuration=$(Configuration);DeployOnBuild=true;DeployTarget=Package"
ContinueOnError="false" />
<Exec Command=""$(ProjectPath)\obj\$(Configuration)\Package\$(ProjectName).deploy.cmd" /y /m:$(DeployServer) -enableRule:DoNotDeleteRule"
ContinueOnError="false" />
</Target>
Visual Studio 2010 has a Publish command that allows you to publish your Web Application Project to a file system location. I'd like to do this on my TeamCity build server, so I need to do it with the solution runner or msbuild. I tried using the Publish target, but I think that might be for ClickOnce:
msbuild Project.csproj /t:Publish /p:Configuration=Deploy
I basically want to do exactly what a web deployment project does, but without the add-in. I need it to compile the WAP, remove any files unnecessary for execution, perform any web.config transformations, and copy the output to a specified location.
My Solution, based on Jeff Siver's answer
<Target Name="Deploy">
<MSBuild Projects="$(SolutionFile)"
Properties="Configuration=$(Configuration);DeployOnBuild=true;DeployTarget=Package"
ContinueOnError="false" />
<Exec Command=""$(ProjectPath)\obj\$(Configuration)\Package\$(ProjectName).deploy.cmd" /y /m:$(DeployServer) -enableRule:DoNotDeleteRule"
ContinueOnError="false" />
</Target>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
我大部分时间都在没有自定义 msbuild 脚本的情况下工作。以下是相关的 TeamCity 构建配置设置:
这将编译、打包(使用 web.config 转换)并将输出保存为工件。唯一缺少的是将输出复制到指定位置,但这可以在具有工件依赖项的另一个 TeamCity 构建配置中或使用 msbuild 脚本来完成。
更新
这是一个 msbuild 脚本,它将编译、打包(使用 web.config 转换)并将输出复制到我的临时服务器
您还可以从 PropertyGroup 标记中删除 SolutionName 和 ProjectName 属性并将它们传递到 msbuild.
更新 2
由于这个问题仍然获得大量流量,我认为值得用我当前使用 Web 部署(也称为 MSDeploy)。
在 TeamCity 中,我有名为
env.Configuration
、env.ProjectName
和env.DeployServiceUrl
的参数。 MSBuild 运行程序具有构建文件路径,并且参数会自动传递(您不必在命令行参数中指定它们)。您还可以从命令行运行它:
I got it mostly working without a custom msbuild script. Here are the relevant TeamCity build configuration settings:
This will compile, package (with web.config transformation), and save the output as artifacts. The only thing missing is copying the output to a specified location, but that could be done either in another TeamCity build configuration with an artifact dependency or with an msbuild script.
Update
Here is an msbuild script that will compile, package (with web.config transformation), and copy the output to my staging server
You can also remove the SolutionName and ProjectName properties from the PropertyGroup tag and pass them to msbuild.
Update 2
Since this question still gets a good deal of traffic, I thought it was worth updating my answer with my current script that uses Web Deploy (also known as MSDeploy).
In TeamCity, I have parameters named
env.Configuration
,env.ProjectName
andenv.DeployServiceUrl
. The MSBuild runner has the build file path and the parameters are passed automagically (you don't have to specify them in Command line parameters).You can also run it from the command line:
使用 VS 2012 中引入的部署配置文件,您可以使用以下命令行进行发布:
有关参数的更多信息 查看此。
/p:VisualStudioVersion
参数的值取决于您的 Visual Studio 版本。维基百科有一个Visual Studio 版本及其版本表。Using the deployment profiles introduced in VS 2012, you can publish with the following command line:
For more information on the parameters see this.
The values for the
/p:VisualStudioVersion
parameter depend on your version of Visual Studio. Wikipedia has a table of Visual Studio releases and their versions.我想出了这样的解决方案,对我来说非常有用:
秘密武器是 _WPPCopyWebApplication 目标。
I came up with such solution, works great for me:
The secret sauce is _WPPCopyWebApplication target.
我不了解 TeamCity,所以希望这对您有用。
我发现执行此操作的最佳方法是使用 MSDeploy.exe。这是 Microsoft 运行的 WebDeploy 项目的一部分。您可以在此处下载这些内容。
使用 WebDeploy,您可以运行命令行,
这与 VS Publish 命令执行相同的操作,仅将必要的部分复制到部署文件夹。
I don't know TeamCity so I hope this can work for you.
The best way I've found to do this is with MSDeploy.exe. This is part of the WebDeploy project run by Microsoft. You can download the bits here.
With WebDeploy, you run the command line
This does the same thing as the VS Publish command, copying only the necessary bits to the deployment folder.
使用 VisualStudio 2012,有一种无需发布配置文件即可处理主题的方法。您可以使用参数传递输出文件夹。它适用于“publishUrl”参数中的绝对路径和相对路径。您可以使用 VS100COMNTOOLS,但需要重写 VisualStudioVersion 才能使用
%ProgramFiles%\MSBuild\Microsoft\VisualStudio\v11.0\WebApplications\Microsoft.WebApplication.targets
中的目标“WebPublish”。使用 VisualStudioVersion 10.0,此脚本将成功,但没有输出:)更新: 我已成功在仅安装了 Windows SDK 7.1 的构建服务器上使用此方法(计算机上没有 Visual Studio 2010 和 2012) 。但我必须按照以下步骤才能使其工作:
脚本:
With VisualStudio 2012 there is a way to handle subj without publish profiles. You can pass output folder using parameters. It works both with absolute and relative path in 'publishUrl' parameter. You can use VS100COMNTOOLS, however you need to override VisualStudioVersion to use target 'WebPublish' from
%ProgramFiles%\MSBuild\Microsoft\VisualStudio\v11.0\WebApplications\Microsoft.WebApplication.targets
. With VisualStudioVersion 10.0 this script will succeed with no outputs :)Update: I've managed to use this method on a build server with just Windows SDK 7.1 installed (no Visual Studio 2010 and 2012 on a machine). But I had to follow these steps to make it work:
Script:
发现了两种不同的解决方案,其工作方式略有不同:
1. 该解决方案的灵感来自于 alexanderb [link] 的答案。不幸的是它对我们不起作用 - 一些 dll 没有复制到 OutDir。我们发现用
Build
目标替换ResolveReferences
可以解决问题 - 现在所有必需的文件都被复制到 OutDir 位置。Disadvantage of this solution was the fact that OutDir contained not only files for publish.
2. 第一个解决方案效果很好,但没有达到我们的预期。我们希望拥有 Visual Studio IDE 中的发布功能 - 即只有应发布的文件才会被复制到输出目录中。正如已经提到的,第一个解决方案将更多文件复制到 OutDir 中 - 用于发布的网站然后存储在
_PublishedWebsites/{ProjectName}
子文件夹中。以下命令解决了这个问题 - 仅将要发布的文件复制到所需的文件夹。现在您有了可以直接发布的目录 - 与第一个解决方案相比,您将节省一些硬盘空间。AutoParameterizationWebConfigConnectionStrings=false
parameter will guarantee that connection strings will not be handled as special artifacts and will be correctly generated - for more information see link.found two different solutions which worked in slightly different way:
1. This solution is inspired by the answer from alexanderb [link]. Unfortunately it did not work for us - some dll's were not copied to the OutDir. We found out that replacing
ResolveReferences
withBuild
target solves the problem - now all necessary files are copied into the OutDir location.Disadvantage of this solution was the fact that OutDir contained not only files for publish.
2. The first solution works well but not as we expected. We wanted to have the publish functionality as it is in Visual Studio IDE - i.e. only the files which should be published will be copied into the Output directory. As it has been already mentioned first solution copies much more files into the OutDir - the website for publish is then stored in
_PublishedWebsites/{ProjectName}
subfolder. The following command solves this - only the files for publish will be copied to desired folder. So now you have directory which can be directly published - in comparison with the first solution you will save some space on hard drive.AutoParameterizationWebConfigConnectionStrings=false
parameter will guarantee that connection strings will not be handled as special artifacts and will be correctly generated - for more information see link.这是我的工作批处理
publish-my-website.bat
请注意,我在服务器上安装了 Visual Studio,以便能够运行
MsBuild.exe
,因为MsBuild.exe .Net Framework 文件夹中的
不起作用。this is my working batch
publish-my-website.bat
Note that I installed Visual Studio on server to be able to run
MsBuild.exe
because theMsBuild.exe
in .Net Framework folders don't work.您必须设置您的环境
并参考我的博客。(抱歉,帖子是韩文)
<前><代码>@ECHO 关闭
:: http://stackoverflow.com/questions/5598668/valid-parameters-for-msdeploy-via-msbuild
::-DeployOnBuild -True
:: -错误的
::
::-DeployTarget -MsDeployPublish
:: -包裹
::
::-Configuration - 有效解决方案配置的名称
::
::-CreatePackageOnPublish -True
:: -错误的
::
::-DeployIisAppPath -<网站名称>/<文件夹>
::
::-MsDeployServiceUrl - 要使用的 MSDeploy 安装位置
::
::-MsDeployPublishMethod -WMSVC(Web 管理服务)
:: -远程代理
::
::-AllowUntrustedCertificate(与自签名 SSL 证书一起使用)-True
:: -错误的
::
::-用户名
::-密码
设定本地
如果存在“%SystemRoot%\Microsoft.NET\Framework\v2.0.50727” SET FXPath="%SystemRoot%\Microsoft.NET\Framework\v2.0.50727”
IF EXIST "%SystemRoot%\Microsoft.NET\Framework\v3.5" SET FXPath="%SystemRoot%\Microsoft.NET\Framework\v3.5"
如果存在“%SystemRoot%\Microsoft.NET\Framework\v4.0.30319” SET FXPath="%SystemRoot%\Microsoft.NET\Framework\v4.0.30319”
SET targetFile=<网站完整路径,即。 .\trunk\WebServer\WebServer.csproj
SET配置=释放
SET msDeployServiceUrl=https://<域>:8172/MsDeploy.axd
SET msDeploySite="<网站名称>"
SET 用户名=“WebDeploy”
设置密码=%USERNAME%
SET平台=任何CPU
SET msbuild=%FXPath%\MSBuild.exe /MaxCpuCount:%NUMBER_OF_PROCESSORS% /clp:ShowCommandLine
%MSBuild% %targetFile% /p:configuration=%configuration%;平台=%platform% /p:DeployOnBuild=True /p:DeployTarget=MsDeployPublish /p:CreatePackageOnPublish=False /p:DeployIISAppPath=%msDeploySite% /p:MSDeployPublishMethod =WMSVC /p:MsDeployServiceUrl=%msDeployServiceUrl% /p:AllowUntrustedCertificate=True /p:用户名=%USERNAME% /p:密码=%password% /p:SkipExtraFilesOnServer=True /p:VisualStudioVersion=12.0
如果不是“%ERRORLEVEL%”==“0”则暂停
本地化
You must set your environments
and reference my blog.(sorry post was Korean)
http://blog.naver.com/PostSearchList.nhn?SearchText=webdeploy&blogId=xyz37&x=25&y=7
您可以通过下面的代码以所需的路径发布解决方案,这里 PublishInDFolder 是具有我们需要发布的路径的名称(我们需要在下图中创建它)
您可以像这样创建发布文件
在批处理文件(.bat)中添加以下两行代码
You can Publish the Solution with desired path by below code, Here PublishInDFolder is the name that has the path where we need to publish(we need to create this in below pic)
You can create publish file like this
Add below 2 lines of code in batch file(.bat)
这是我的批处理文件
This my batch file
为了生成发布输出,请再提供一个参数。
msbuild example.sln /p:publishprofile=profilename /p:deployonbuild=true /p:configuration=debug/或任何
For generating the publish output provide one more parameter.
msbuild example.sln /p:publishprofile=profilename /p:deployonbuild=true /p:configuration=debug/or any
您可以使用此命令通过发布配置文件来发布 Web 应用程序。
此示例发布配置文件可以创建一个版本 zip 文件,其版本号位于网络路径中的 AssemblyInfo.cs 文件中(创建 zip 文件并使用 PowerShell 命令删除其他已发布文件是可选的)。
you can use this command to publish web applications with Publish Profiles.
This sample Publish Profile can create a release zip file with a version number that's in AssemblyInfo.cs File in the network path (create zip file and remove other published files with PowerShell command is optional).
使用 PublishProfile:
不使用 PublishProfile:
With PublishProfile:
Without PublishProfile: