如何使用msbuild发布Web应用?

233

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

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

我基本上想要做的是与Web部署项目完全相同,但没有插件。 我需要编译WAP,删除任何不必要的执行文件,执行任何web.config转换,并将输出复制到指定位置。

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

<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>

可能重复:https://dev59.com/OnM_5IYBdhLWcg3w8IA2 - Steven Evers
@SnOrfus 我目前正在使用VS 2008中的Web部署项目(正如我在回答那个问题时提到的),但我想尝试自动化VS 2010的发布功能。 - jrummell
2
只需要对您的脚本进行一个小修改:您正在使用$(ProjectPath)来部署脚本,但实际上您需要的是$(ProjectDir),否则您最终会得到.csproj\obj。 - Troy Hunt
@Troy Hunt - ProjectPath 实际上是我脚本中一个变量,它保存着项目文件夹的相对路径,但 ProjectDir 也应该可以使用。 - jrummell
2
从VS2012开始,这变得更容易了:https://dev59.com/m2Yr5IYBdhLWcg3wJ2-U#13947667 - RobSiklos
显示剩余2条评论
12个回答

1

为了生成发布输出,提供一个额外的参数。

msbuild example.sln /p:publishprofile=profilename /p:deployonbuild=true /p:configuration=debug或任何其他配置。


0

您可以使用此命令和发布配置文件一起发布 Web 应用程序。

msbuild SolutionName.sln /p:DeployOnBuild=true /p:PublishProfile=PublishProfileName

这个示例发布配置文件可以创建一个包含网络路径中 AssemblyInfo.cs 文件中版本号的发布 zip 文件(使用 PowerShell 命令创建 zip 文件并删除其他已发布文件是可选的)。

 <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
     <PropertyGroup>
    <WebPublishMethod>FileSystem</WebPublishMethod>
    <LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
    <LastUsedPlatform>Any CPU</LastUsedPlatform>
    <SiteUrlToLaunchAfterPublish />
    <LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
    <ExcludeApp_Data>False</ExcludeApp_Data>
    <Major>0</Major>
    <Minor>1</Minor>
    <Build>2</Build>
    <Publish>C:\</Publish>
    <publishUrl>$(Publish)</publishUrl>
    <DeleteExistingFiles>True</DeleteExistingFiles>
  </PropertyGroup>
  <Target Name="GetBuildUrl">
    <PropertyGroup>      <In>$([System.IO.File]::ReadAllText('$(MSBuildProjectDirectory)\Properties\AssemblyInfo.cs'))</In>
      <TargetPath>\\NetworkPath\ProjectName</TargetPath>
      <Pattern>^\s*\[assembly: AssemblyVersion\(\D*(\d+)\.(\d+)\.(\d+)\.(\d+)</Pattern>
      <Major>$([System.Text.RegularExpressions.Regex]::Match($(In), $(Pattern), System.Text.RegularExpressions.RegexOptions.Multiline).Groups[1].Value)</Major>
      <Minor>$([System.Text.RegularExpressions.Regex]::Match($(In), $(Pattern), System.Text.RegularExpressions.RegexOptions.Multiline).Groups[2].Value)</Minor>
      <Build>$([System.Text.RegularExpressions.Regex]::Match($(In), $(Pattern), System.Text.RegularExpressions.RegexOptions.Multiline).Groups[3].Value)</Build>
      <Sub>$([System.Text.RegularExpressions.Regex]::Match($(In), $(Pattern), System.Text.RegularExpressions.RegexOptions.Multiline).Groups[4].Value)</Sub>
      <Publish>$(TargetPath)\$(Major).$(Minor).$(Build).$(Sub)\</Publish>
      <publishUrl Condition=" '$(Publish)' != '' ">$(Publish)</publishUrl>
      <publishUrl Condition=" '$(Publish)' == '' and '$(LastUsedBuildConfiguration)'!='' ">$(LastUsedBuildConfiguration)</publishUrl>
    </PropertyGroup>
  </Target>
  <Target Name="BeforeBuild" DependsOnTargets="GetBuildUrl">
    <Message Importance="High" Text="|" />
    <Message Importance="High" Text=" ================================================================================================" />
    <Message Importance="High" Text="    BUILD INFO                                                                                    " />
    <Message Importance="High" Text="    Version [$(Major).$(Minor).$(Build)] found in [$(MSBuildProjectDirectory)\Properties\AssemblyInfo.cs] " />
    <Message Importance="High" Text="    Build will be saved to [$(publishUrl)]                                                        " />
    <Message Importance="High" Text=" =================================================================================================" />
    <Message Importance="High" Text="|" />
  </Target>
  <Target Name="Zip" BeforeTargets="AfterBuild">
    <Exec Command="PowerShell -command Compress-Archive -Path $(Publish) -DestinationPath $(Publish)Release.zip" />
    <Exec Command="PowerShell -command Remove-Item -Recurse -Force $(Publish) -Exclude Release.zip" />
  </Target>
</Project>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接