問題描述
Visual Studio 2010 有一個發(fā)布命令,允許您將 Web 應(yīng)用程序項(xiàng)目發(fā)布到文件系統(tǒng)位置.我想在我的 TeamCity 構(gòu)建服務(wù)器上執(zhí)行此操作,因此我需要使用解決方案運(yùn)行程序或 msbuild 執(zhí)行此操作.我嘗試使用 Publish 目標(biāo),但我認(rèn)為這可能適用于 ClickOnce:
msbuild Project.csproj/t:Publish/p:Configuration=Deploy
我基本上想做一個 Web 部署項(xiàng)目所做的事情,但沒有插件.我需要它來編譯 WAP,刪除任何不需要執(zhí)行的文件,執(zhí)行任何 web.xml 文件.配置轉(zhuǎn)換,并將輸出復(fù)制到指定位置.
我的解決方案,基于 Jeff Siver 的回答
<Target Name="Deploy"><MSBuild Projects="$(SolutionFile)"屬性="配置=$(配置);DeployOnBuild=true;DeployTarget=Package"ContinueOnError="假"/><Exec Command=""$(ProjectPath)obj$(Configuration)Package$(ProjectName).deploy.cmd"/y/m:$(DeployServer) -enableRule:DoNotDeleteRule"ContinueOnError="假"/></目標(biāo)>
我得到它主要是在沒有自定義 msbuild 腳本的情況下工作.以下是相關(guān)的 TeamCity 構(gòu)建配置設(shè)置:
<上一頁>工件路徑:%system.teamcity.build.workingDir%MyProjectobjDebugPackagePackageTmp運(yùn)行器類型:MSBuild(用于 MSBuild 文件的運(yùn)行器)構(gòu)建文件路徑:MyProjectMyProject.csproj工作目錄:與結(jié)帳目錄相同MSBuild 版本:Microsoft .NET Framework 4.0MSBuild 工具版本:4.0運(yùn)行平臺:x86目標(biāo):包MSBuild.exe 的命令行參數(shù):/p:Configuration=Debug這將編譯、打包(使用 web.config 轉(zhuǎn)換)并將輸出保存為工件.唯一缺少的是將輸出復(fù)制到指定位置,但這可以在另一個帶有工件依賴項(xiàng)的 TeamCity 構(gòu)建配置或使用 msbuild 腳本中完成.
更新
這是一個 msbuild 腳本,它將編譯、打包(使用 web.config 轉(zhuǎn)換)并將輸出復(fù)制到我的登臺服務(wù)器
<?xml version="1.0" encoding="utf-8" ?><Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"><屬性組><配置條件="'$(Configuration)' == ''">發(fā)布</配置><解決方案名稱>我的解決方案</解決方案名稱><SolutionFile>$(SolutionName).sln</SolutionFile><項(xiàng)目名稱>我的項(xiàng)目</項(xiàng)目名稱><ProjectFile>$(ProjectName)$(ProjectName).csproj</ProjectFile></屬性組><目標(biāo)名稱="Build" DependsOnTargets="BuildPackage;CopyOutput"/><目標(biāo)名稱="BuildPackage"><MSBuild Projects="$(SolutionFile)" ContinueOnError="false" Targets="Rebuild" Properties="Configuration=$(Configuration)"/><MSBuild Projects="$(ProjectFile)" ContinueOnError="false" Targets="Package" Properties="Configuration=$(Configuration)"/></目標(biāo)><目標(biāo)名稱="復(fù)制輸出"><項(xiàng)目組><PackagedFiles Include="$(ProjectName)obj$(Configuration)PackagePackageTmp***.*"/></項(xiàng)目組><Copy SourceFiles="@(PackagedFiles)" DestinationFiles="@(PackagedFiles->'\build02wwwroot$(ProjectName)$(Configuration)\%(RecursiveDir)%(Filename)%(Extension)')"/></目標(biāo)></項(xiàng)目>
您還可以從 PropertyGroup 標(biāo)記中刪除 SolutionName 和 ProjectName 屬性并將它們傳遞給 msbuild.
msbuild build.xml/p:Configuration=Deploy;SolutionName=MySolution;ProjectName=MyProject
更新 2
由于這個問題仍然有大量流量,我認(rèn)為值得用我當(dāng)前使用 Web 部署(也稱為 MSDeploy).
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build" ToolsVersion="4.0"><屬性組><配置條件="'$(Configuration)' == ''">發(fā)布</配置><ProjectFile Condition="'$(ProjectFile)' == ''">$(ProjectName)$(ProjectName).csproj</ProjectFile><DeployServiceUrl 條件="'$(DeployServiceUrl)' == ''">http://staging-server/MSDeployAgentService</DeployServiceUrl></屬性組><目標(biāo)名稱="驗(yàn)證屬性"><!-- 驗(yàn)證我們是否具有所有必需屬性的值--><錯誤條件="'$(ProjectName)' == '' " Text="ProjectName 是必需的."/></目標(biāo)><目標(biāo)名稱="構(gòu)建" DependsOnTargets="VerifyProperties"><!-- 使用windows身份驗(yàn)證部署--><MSBuild Projects="$(ProjectFile)"屬性=配置=$(配置);MvcBuildViews=假;部署構(gòu)建=真;部署目標(biāo)=MSDeployPublish;CreatePackageOnPublish=真;AllowUntrustedCertificate=True;MSDeployPublishMethod=遠(yuǎn)程代理;MsDeployServiceUrl=$(DeployServiceUrl);SkipExtraFilesOnServer=真;用戶名=;密碼=;"ContinueOnError="假"/></目標(biāo)></項(xiàng)目>
在 TeamCity 中,我有名為 env.Configuration
、env.ProjectName
和 env.DeployServiceUrl
的參數(shù).MSBuild 運(yùn)行器具有構(gòu)建文件路徑,并且參數(shù)自動傳遞(您不必在命令行參數(shù)中指定它們).
你也可以從命令行運(yùn)行它:
msbuild build.xml/p:Configuration=Staging;ProjectName=MyProject;DeployServiceUrl=http://staging-server/MSDeployAgentService
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>
I got it mostly working without a custom msbuild script. Here are the relevant TeamCity build configuration settings:
Artifact paths: %system.teamcity.build.workingDir%MyProjectobjDebugPackagePackageTmp Type of runner: MSBuild (Runner for MSBuild files) Build file path: MyProjectMyProject.csproj Working directory: same as checkout directory MSBuild version: Microsoft .NET Framework 4.0 MSBuild ToolsVersion: 4.0 Run platform: x86 Targets: Package Command line parameters to MSBuild.exe: /p:Configuration=Debug
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
<?xml version="1.0" encoding="utf-8" ?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Release</Configuration>
<SolutionName>MySolution</SolutionName>
<SolutionFile>$(SolutionName).sln</SolutionFile>
<ProjectName>MyProject</ProjectName>
<ProjectFile>$(ProjectName)$(ProjectName).csproj</ProjectFile>
</PropertyGroup>
<Target Name="Build" DependsOnTargets="BuildPackage;CopyOutput" />
<Target Name="BuildPackage">
<MSBuild Projects="$(SolutionFile)" ContinueOnError="false" Targets="Rebuild" Properties="Configuration=$(Configuration)" />
<MSBuild Projects="$(ProjectFile)" ContinueOnError="false" Targets="Package" Properties="Configuration=$(Configuration)" />
</Target>
<Target Name="CopyOutput">
<ItemGroup>
<PackagedFiles Include="$(ProjectName)obj$(Configuration)PackagePackageTmp***.*"/>
</ItemGroup>
<Copy SourceFiles="@(PackagedFiles)" DestinationFiles="@(PackagedFiles->'\build02wwwroot$(ProjectName)$(Configuration)\%(RecursiveDir)%(Filename)%(Extension)')"/>
</Target>
</Project>
You can also remove the SolutionName and ProjectName properties from the PropertyGroup tag and pass them to msbuild.
msbuild build.xml /p:Configuration=Deploy;SolutionName=MySolution;ProjectName=MyProject
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).
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build" ToolsVersion="4.0">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Release</Configuration>
<ProjectFile Condition=" '$(ProjectFile)' == '' ">$(ProjectName)$(ProjectName).csproj</ProjectFile>
<DeployServiceUrl Condition=" '$(DeployServiceUrl)' == '' ">http://staging-server/MSDeployAgentService</DeployServiceUrl>
</PropertyGroup>
<Target Name="VerifyProperties">
<!-- Verify that we have values for all required properties -->
<Error Condition=" '$(ProjectName)' == '' " Text="ProjectName is required." />
</Target>
<Target Name="Build" DependsOnTargets="VerifyProperties">
<!-- Deploy using windows authentication -->
<MSBuild Projects="$(ProjectFile)"
Properties="Configuration=$(Configuration);
MvcBuildViews=False;
DeployOnBuild=true;
DeployTarget=MSDeployPublish;
CreatePackageOnPublish=True;
AllowUntrustedCertificate=True;
MSDeployPublishMethod=RemoteAgent;
MsDeployServiceUrl=$(DeployServiceUrl);
SkipExtraFilesOnServer=True;
UserName=;
Password=;"
ContinueOnError="false" />
</Target>
</Project>
In TeamCity, I have parameters named env.Configuration
, env.ProjectName
and env.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:
msbuild build.xml /p:Configuration=Staging;ProjectName=MyProject;DeployServiceUrl=http://staging-server/MSDeployAgentService
這篇關(guān)于如何使用 msbuild 發(fā)布網(wǎng)頁?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!