如何使用 msbuild 发布 Web? [英] How to Publish Web with msbuild?

查看:24
本文介绍了如何使用 msbuild 发布 Web?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Visual Studio 2010 有一个发布命令,允许您将 Web 应用程序项目发布到文件系统位置.我想在我的 TeamCity 构建服务器上执行此操作,因此我需要使用解决方案运行程序或 msbuild 执行此操作.我尝试使用 Publish 目标,但我认为这可能适用于 ClickOnce:

msbuild Project.csproj/t:Publish/p:Configuration=Deploy

我基本上想做一个 Web 部署项目所做的事情,但没有加载项.我需要它来编译 WAP,删除任何不需要执行的文件,执行任何 web.配置转换,并将输出复制到指定位置.

我的解决方案,基于 Jeff Siver 的回答

<MSBuild 项目="$(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"/></目标>

解决方案

我基本上不用自定义 msbuild 脚本就可以工作.以下是相关的 TeamCity 构建配置设置:

<前>工件路径:%system.teamcity.build.workingDir%MyProjectobjDebugPackagePackageTmp运行器类型:MSBuild(MSBuild 文件的运行器)构建文件路径:MyProjectMyProject.csproj工作目录:与结帐目录相同MSBuild 版本:Microsoft .NET Framework 4.0MSBuild 工具版本:4.0运行平台:x86目标:包MSBuild.exe 的命令行参数:/p:Configuration=Debug

这将编译、打包(使用 web.config 转换)并将输出保存为工件.唯一缺少的是将输出复制到指定位置,但这可以在具有工件依赖项或 msbuild 脚本的另一个 TeamCity 构建配置中完成.

更新

这是一个 msbuild 脚本,它将编译、打包(使用 web.config 转换)并将输出复制到我的临时服务器

'\build02wwwroot$(ProjectName)$(Configuration)\%(RecursiveDir)%(Filename)%(Extension)')"/></目标></项目>

您还可以从 PropertyGroup 标记中删除 SolutionName 和 ProjectName 属性并将它们传递给 msbuild.

msbuild build.xml/p:Configuration=Deploy;SolutionName=MySolution;ProjectName=MyProject

更新 2

由于这个问题仍然有很多流量,我认为值得用我当前使用 Web 部署(也称为 MSDeploy).

<属性组><Configuration Condition=" '$(Configuration)' == '' ">Release</Configuration><ProjectFile Condition=" '$(ProjectFile)' == '' ">$(ProjectName)$(ProjectName).csproj</ProjectFile><DeployServiceUrl Condition="'$(DeployServiceUrl)' == '' ">http://staging-server/MSDeployAgentService</DeployServiceUrl></PropertyGroup><目标名称="VerifyProperties"><!-- 验证我们是否具有所有必需属性的值--><Error Condition=" '$(ProjectName)' == '' " Text="ProjectName 是必需的."/></目标><Target Name="Build" DependsOnTargets="VerifyProperties"><!-- 使用 Windows 身份验证进行部署 --><MSBuild 项目="$(ProjectFile)"属性="配置=$(配置);MvcBuildViews=False;DeployOnBuild=true;DeployTarget=MSDeployPublish;CreatePackageOnPublish=True;AllowUntrustedCertificate=True;MSDeployPublishMethod=RemoteAgent;MsDeployServiceUrl=$(DeployServiceUrl);SkipExtraFilesOnServer=True;用户名=;密码=;"ContinueOnError="false"/></目标></项目>

在 TeamCity 中,我有名为 env.Configurationenv.ProjectNameenv.DeployServiceUrl 的参数.MSBuild 运行器具有构建文件路径,并且参数会自动传递(您不必在命令行参数中指定它们).

您也可以从命令行运行它:

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="&quot;$(ProjectPath)obj$(Configuration)Package$(ProjectName).deploy.cmd&quot; /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

这篇关于如何使用 msbuild 发布 Web?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆