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

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

问题描述

Visual Studio 2010中有一个发布命令,允许您将Web应用程序项目发布到文件系统位置。我想这样做我的TeamCity构建服务器,所以我需要解决亚军或MSBuild的去做。我尝试使用发布的目标,但我认为这可能是对ClickOnce的:

 的MSBuild Project.csproj /吨:发布/ P:配置=部署

我基本上想要做完全是一个网络部署项目做什么,但没有加载项。我需要它来编译WAP,删除不必要的执行任何文件,执行任何 web.config文件转换,并且输出复制到指定的位置。

我的解决方案的基础上,杰夫Siver的答案

 <目标名称=部署>
    &所述; MSBuild的项目=$(SolutionFile)
             属性=配置= $(配置); DeployOnBuild = TRUE; DeployTarget =包
             ContinueOnError =FALSE/>
    < Exec命令=&放大器; QUOT; $(ProjectPath)\\ OBJ \\ $(配置)\\包\\ $(项目名).deploy.cmd和放大器; QUOT; / Y / M:$(DeployServer)-enableRule:DoNotDeleteRule
          ContinueOnError =FALSE/>
< /目标>


解决方案

我没有得到一个自定义的MSBuild脚本,它主要的工作。下面是有关TeamCity的生成配置设置:


神器路径:%system.teamcity.build.workingDir%\\ MyProject的\\ OBJ \\调试\\包\\ PackageTmp
亚军类型:的MSBuild(转轮的MSBuild文件)
构建文件路径:MyProject的\\ MyProject.csproj
工作目录:同检出目录
MSBuild的版本:Microsoft .NET框架4.0
MSBuild的ToolsVersion:4.0
运行平台:86
目标:包
命令行参数MSBuild.exe:/ P:配置=调试

这将编译,打包(与web.config中变换),并保存输出文物。唯一缺少的是复制输出到指定的位置,但可能与工件的依赖或一个MSBuild脚本做到无论在另一个TeamCity的生成配置。

更新

下面是一个MSBuild脚本,将编译,打包(与web.config中变换),输出复制到我的临时服务器

 <?XML版本=1.0编码=UTF-8&GT?;
<项目DefaultTargets =生成的xmlns =htt​​p://schemas.microsoft.com/developer/msbuild/2003>
    <&的PropertyGroup GT;
        <结构条件='$(配置)'==''>发布暨LT; /结构>
        <&解决方案名称GT; MySolution< /解决方案名称>
        < SolutionFile> $(解决方案名称)的.sln< / SolutionFile>
        <项目名>&MyProject的LT; /项目名>
        <&project文件GT; $(项目名)\\ $(项目名)的.csproj< / project文件>
    < /&的PropertyGroup GT;    <目标名称=生成DependsOnTargets =BuildPackage; CopyOutput/>    <目标名称=BuildPackage>
        <项目的MSBuild =$(SolutionFile)ContinueOnError =false的目标=重建属性=配置= $(配置)/>
        <项目的MSBuild =$(project文件)ContinueOnError =false的目标=包属性=配置= $(配置)/>
    < /目标>    <目标名称=CopyOutput>
        <&的ItemGroup GT;
            < PackagedFiles包括=$(项目名)\\ OBJ \\ $(配置)\\包\\ PackageTmp \\ ** \\ *。*/>
        < /&的ItemGroup GT;
        <复制来源档案=@(PackagedFiles) DestinationFiles=\"@(PackagedFiles->'\\\\build02\\wwwroot\\$(ProjectName)\\$(Configuration)\\%(RecursiveDir)%(Filename)%(Extension)')\"/>
    < /目标>
< /项目>

您也可以从标签的PropertyGroup删除解决方案名称和项目名的属性,并将其传递到MSBuild的。

  MSBuild的build.xml文件/ P:配置=部署,解决方案名称= MySolution;项目名= MyProject的

更新2

由于这个问题仍然得到一个良好的交通问题,我认为这是值得更新我用我的当前脚本的答案使用 Web部署(也称为MSDeploy)

 <项目的xmlns =htt​​p://schemas.microsoft.com/developer/msbuild/2003DefaultTargets =生成ToolsVersion =4.0>
  <&的PropertyGroup GT;
    <结构条件='$(配置)'==''>发布暨LT; /结构>
    < project文件条件='$(project文件)'==''> $(项目名)\\ $(项目名)的.csproj< / project文件>
    < D​​eployServiceUrl条件='$(DeployServiceUrl)'==''>的http://临时服务器/ MSDeployAgentService< / DeployServiceUrl>
  < /&的PropertyGroup GT;  <目标名称=VerifyProperties>
    <! - 验证我们对所有必需的属性值 - GT&;
    <错误条件='$(项目名)'==''文本=项目名是必需的。 />
  < /目标>  <目标名称=生成DependsOnTargets =VerifyProperties>
    <! - 部署使用Windows身份验证 - >
    <项目的MSBuild =$(project文件)
             属性=配置= $(配置);
                             MvcBuildViews = FALSE;
                             DeployOnBuild = TRUE;
                             DeployTarget = MSDeployPublish;
                             CreatePackageOnPublish = TRUE;
                             AllowUntrustedCertificate = TRUE;
                             MSDeployPublishMethod = RemoteAgent;
                             MsDeployServiceUrl = $(DeployServiceUrl);
                             SkipExtraFilesOnServer = TRUE;
                             用户名=;
                             密码=;
             ContinueOnError =FALSE/>
  < /目标>
< /项目>

在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%\MyProject\obj\Debug\Package\PackageTmp 
Type of runner: MSBuild (Runner for MSBuild files) 
Build file path: MyProject\MyProject.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)\Package\PackageTmp\**\*.*"/>
        </ItemGroup>
        <Copy SourceFiles="@(PackagedFiles)" DestinationFiles="@(PackagedFiles->'\\build02\wwwroot\$(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的发布网站?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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