用户登录
用户注册

分享至

如何使用 msbuild 发布网页?

  • 作者: 段友457123
  • 来源: 51数据库
  • 2023-02-13

问题描述

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)"属性="配置=$(配置);DeployOnBuild=true;DeployTarget=Package"ContinueOnError="假"/><Exec Command="&quot;$(ProjectPath)obj$(Configuration)Package$(ProjectName).deploy.cmd&quot;/y/m:$(DeployServer) -enableRule:DoNotDeleteRule"ContinueOnError="假"/></目标>

解决方案

我得到它主要是在没有自定义 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 转换)并将输出保存为工件.唯一缺少的是将输出复制到指定位置,但这可以在另一个带有工件依赖项的 TeamCity 构建配置或使用 msbuild 脚本中完成.

更新

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

<?xml version="1.0" encoding="utf-8" ?><Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"><属性组><配置条件="'$(Configuration)' == ''">发布</配置><解决方案名称>我的解决方案</解决方案名称><SolutionFile>$(SolutionName).sln</SolutionFile><项目名称>我的项目</项目名称><ProjectFile>$(ProjectName)$(ProjectName).csproj</ProjectFile></属性组><目标名称="Build" DependsOnTargets="BuildPackage;CopyOutput"/><目标名称="BuildPackage"><MSBuild Projects="$(SolutionFile)" ContinueOnError="false" Targets="Rebuild" Properties="Configuration=$(Configuration)"/><MSBuild Projects="$(ProjectFile)" ContinueOnError="false" Targets="Package" Properties="Configuration=$(Configuration)"/></目标><目标名称="复制输出"><项目组><PackagedFiles Include="$(ProjectName)obj$(Configuration)PackagePackageTmp***.*"/></项目组><Copy SourceFiles="@(PackagedFiles)" DestinationFiles="@(PackagedFiles->'\build02wwwroot$(ProjectName)$(Configuration)\%(RecursiveDir)%(Filename)%(Extension)')"/></目标></项目>

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

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

更新 2

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

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build" ToolsVersion="4.0"><属性组><配置条件="'$(Configuration)' == ''">发布</配置><ProjectFile Condition="'$(ProjectFile)' == ''">$(ProjectName)$(ProjectName).csproj</ProjectFile><DeployServiceUrl 条件="'$(DeployServiceUrl)' == ''">http://staging-server/MSDeployAgentService</DeployServiceUrl></属性组><目标名称="验证属性"><!-- 验证我们是否具有所有必需属性的值--><错误条件="'$(ProjectName)' == '' " Text="ProjectName 是必需的."/></目标><目标名称="构建" DependsOnTargets="VerifyProperties"><!-- 使用windows身份验证部署--><MSBuild Projects="$(ProjectFile)"属性=配置=$(配置);MvcBuildViews=假;部署构建=真;部署目标=MSDeployPublish;CreatePackageOnPublish=真;AllowUntrustedCertificate=True;MSDeployPublishMethod=远程代理;MsDeployServiceUrl=$(DeployServiceUrl);SkipExtraFilesOnServer=真;用户名=;密码=;"ContinueOnError="假"/></目标></项目>

在 TeamCity 中,我有名为 env.Configuration、env.ProjectName 和 env.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

软件
前端设计
程序设计
Java相关