使用msbuild从解决方案中发布一个web项目

本文关键字:布一个 web 项目 msbuild 解决方案 使用 | 更新日期: 2023-09-27 18:16:26

我正试图将我的解决方案中的一个web项目部署到服务器。我在TeamCity上使用msbuild,如下所示:

msbuild MySolution.sln /t:WebSite:Rebuild /p:DeployOnBuild=True /p:PublishProfile=Prod ...

然而,当我运行它时,msbuild仍然试图构建我的WebService项目,即使我的WebSite项目不依赖于它(但它确实依赖于解决方案中的Services项目)。如何只发布一个项目,也就是WebSite ?

我还尝试使用

构建项目文件
msbuild WebSite/WebSite.csproj /p:DeployOnBuild=True ...

但是它抱怨它不能恢复包:

[07:47:17]WebSite'WebSite.csproj.teamcity: Build target: Build
[07:47:17][WebSite'WebSite.csproj.teamcity] RestorePackages
[07:47:17][RestorePackages] Exec
[07:47:17][Exec] C:'TeamCity'buildAgent'work'cab8a3d752df3a51'.nuget'NuGet.targets(90, 15): error MSB4064: The "LogStandardErrorAsError" parameter is not supported by the "Exec" task. Verify the parameter exists on the task, and it is a settable public instance property.
[07:47:17][Exec] C:'TeamCity'buildAgent'work'cab8a3d752df3a51'.nuget'NuGet.targets(89, 9): error MSB4063: The "Exec" task could not be initialized with its input parameters. 
[07:47:17][WebSite'WebSite.csproj.teamcity] Project WebSite'WebSite.csproj.teamcity failed.

当我禁用NuGet包还原,CoreCompile (Csc)失败的错误,我从来没有听说过,不应该发生:

[07:54:43]WebSite'WebSite.csproj.teamcity: Build target: Build (13s)
[07:54:55][WebSite'WebSite.csproj.teamcity] CoreCompile
[07:54:55][CoreCompile] Csc
[07:54:56][Csc] Areas'Api'Services'TripService.cs(19, 104): error CS0241: Default parameter specifiers are not permitted
[07:54:56][Csc] Helpers'StatisticsUtility.cs(11, 35): error CS1031: Type expected
[07:54:56][Csc] Helpers'StatisticsUtility.cs(11, 53): error CS1002: ; expected
[07:54:56][Csc] Helpers'StatisticsUtility.cs(16, 28): error CS1519: Invalid token '(' in class, struct, or interface member declaration
[07:54:56][Csc] Helpers'StatisticsUtility.cs(16, 37): error CS1519: Invalid token ',' in class, struct, or interface member declaration
[07:54:56][Csc] Helpers'StatisticsUtility.cs(17, 27): error CS1519: Invalid token '(' in class, struct, or interface member declaration
[07:54:56][Csc] Helpers'StatisticsUtility.cs(17, 32): error CS1519: Invalid token ')' in class, struct, or interface member declaration
[07:54:56][Csc] Helpers'StatisticsUtility.cs(23, 17): error CS1519: Invalid token 'for' in class, struct, or interface member declaration
[07:54:56][Csc] Helpers'StatisticsUtility.cs(23, 26): error CS1519: Invalid token '<=' in class, struct, or interface member declaration
[07:54:56][Csc] Helpers'StatisticsUtility.cs(23, 45): error CS1519: Invalid token '-' in class, struct, or interface member declaration
[07:54:56][Csc] Helpers'StatisticsUtility.cs(23, 51): error CS1519: Invalid token '++' in class, struct, or interface member declaration
[07:54:56][Csc] Helpers'StatisticsUtility.cs(24, 34): error CS0270: Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)
[07:54:56][Csc] Helpers'StatisticsUtility.cs(24, 37): error CS1519: Invalid token '==' in class, struct, or interface member declaration
[07:54:56][Csc] Helpers'StatisticsUtility.cs(24, 51): error CS1519: Invalid token ')' in class, struct, or interface member declaration
[07:54:56][Csc] Helpers'StatisticsUtility.cs(24, 63): error CS1519: Invalid token '++' in class, struct, or interface member declaration
[07:54:56][Csc] Helpers'StatisticsUtility.cs(25, 41): error CS1519: Invalid token '>' in class, struct, or interface member declaration
[07:54:56][Csc] Helpers'StatisticsUtility.cs(25, 53): error CS1519: Invalid token ')' in class, struct, or interface member declaration
[07:54:56][Csc] Helpers'StatisticsUtility.cs(27, 36): error CS1519: Invalid token '=' in class, struct, or interface member declaration
[07:54:56][Csc] Helpers'StatisticsUtility.cs(27, 48): error CS1519: Invalid token ';' in class, struct, or interface member declaration
[07:54:56][Csc] Helpers'StatisticsUtility.cs(28, 36): error CS1519: Invalid token '=' in class, struct, or interface member declaration
[07:54:56][Csc] Helpers'StatisticsUtility.cs(29, 37): error CS1519: Invalid token '=' in class, struct, or interface member declaration
[07:54:56][Csc] Helpers'StatisticsUtility.cs(29, 48): error CS0270: Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)
[07:54:56][Csc] Helpers'StatisticsUtility.cs(29, 50): error CS1519: Invalid token ';' in class, struct, or interface member declaration
[07:54:56][Csc] Helpers'StatisticsUtility.cs(30, 33): error CS1519: Invalid token '=' in class, struct, or interface member declaration
[07:54:56][Csc] Helpers'StatisticsUtility.cs(30, 44): error CS0270: Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)
[07:54:56][Csc] Helpers'StatisticsUtility.cs(30, 50): error CS1519: Invalid token ';' in class, struct, or interface member declaration
[07:54:56][Csc] Helpers'StatisticsUtility.cs(32, 21): error CS0116: A namespace does not directly contain members such as fields or methods
[07:54:56][Csc] Helpers'StatisticsUtility.cs(35, 50): error CS1518: Expected class, delegate, enum, interface, or struct
[07:54:56][Csc] Helpers'StatisticsUtility.cs(38, 21): error CS0116: A namespace does not directly contain members such as fields or methods
[07:54:56][Csc] Helpers'StatisticsUtility.cs(40, 50): error CS1518: Expected class, delegate, enum, interface, or struct
[07:54:56][Csc] Helpers'StatisticsUtility.cs(42, 21): error CS1022: Type or namespace definition, or end-of-file expected
[07:54:56][Csc] Helpers'UrlHelperExtensions.cs(8, 59): error CS1031: Type expected
[07:54:56][Csc] Helpers'UrlHelperExtensions.cs(8, 80): error CS1002: ; expected
[07:54:56][Csc] Helpers'UrlHelperExtensions.cs(10, 55): error CS1519: Invalid token '(' in class, struct, or interface member declaration
[07:54:56][Csc] Helpers'UrlHelperExtensions.cs(10, 60): error CS1520: Class, struct, or interface method must have a return type
[07:54:56][Csc] Helpers'UrlHelperExtensions.cs(10, 82): error CS1002: ; expected
[07:54:56][Csc] Helpers'UrlHelperExtensions.cs(13, 23): error CS1518: Expected class, delegate, enum, interface, or struct
[07:54:56][Csc] Helpers'UrlHelperExtensions.cs(15, 60): error CS1518: Expected class, delegate, enum, interface, or struct
[07:54:56][Csc] Helpers'UrlHelperExtensions.cs(18, 23): error CS1518: Expected class, delegate, enum, interface, or struct
[07:54:56][Csc] Helpers'UrlHelperExtensions.cs(20, 25): error CS1518: Expected class, delegate, enum, interface, or struct
[07:54:56][Csc] Helpers'UrlHelperExtensions.cs(23, 28): error CS1518: Expected class, delegate, enum, interface, or struct
[07:54:56][Csc] Helpers'UrlHelperExtensions.cs(26, 28): error CS1518: Expected class, delegate, enum, interface, or struct
[07:54:56][Csc] Helpers'UrlHelperExtensions.cs(29, 24): error CS1518: Expected class, delegate, enum, interface, or struct
[07:54:56][Csc] Helpers'UrlHelperExtensions.cs(29, 84): error CS1518: Expected class, delegate, enum, interface, or struct
[07:54:56][Csc] Helpers'UrlHelperExtensions.cs(32, 28): error CS1518: Expected class, delegate, enum, interface, or struct
[07:54:56][Csc] Helpers'UrlHelperExtensions.cs(35, 9): error CS1022: Type or namespace definition, or end-of-file expected
[07:54:56][Csc] Helpers'UrlHelperExtensions.cs(23, 26): error CS0101: The namespace '<global namespace>' already contains a definition for '?'
[07:54:56][Csc] Helpers'UrlHelperExtensions.cs(26, 26): error CS0101: The namespace '<global namespace>' already contains a definition for '?'
[07:54:56][Csc] Helpers'UrlHelperExtensions.cs(29, 22): error CS0101: The namespace '<global namespace>' already contains a definition for '?'
[07:54:56][Csc] Helpers'UrlHelperExtensions.cs(29, 83): error CS0101: The namespace '<global namespace>' already contains a definition for '?'
[07:54:56][Csc] Helpers'UrlHelperExtensions.cs(32, 26): error CS0101: The namespace '<global namespace>' already contains a definition for '?'
[07:54:56][Csc] Controllers'SessionController.cs(13, 51): error CS0241: Default parameter specifiers are not permitted
[07:54:56][Csc] Helpers'JsonNetResult.cs(13, 44): error CS1031: Type expected
[07:54:56][Csc] Helpers'JsonNetResult.cs(13, 72): error CS1041: Identifier expected, 'object' is a keyword
[07:54:56][Csc] Helpers'JsonNetResult.cs(13, 91): error CS1002: ; expected
[07:54:56][Csc] Helpers'JsonNetResult.cs(16, 38): error CS1519: Invalid token '=' in class, struct, or interface member declaration
[07:54:56][Csc] Helpers'JsonNetResult.cs(16, 59): error CS1519: Invalid token ';' in class, struct, or interface member declaration
[07:54:56][Csc] Helpers'JsonNetResult.cs(17, 64): error CS1519: Invalid token '=' in class, struct, or interface member declaration
[07:54:56][Csc] Helpers'JsonNetResult.cs(17, 90): error CS1519: Invalid token ';' in class, struct, or interface member declaration
[07:54:56][Csc] Helpers'JsonNetResult.cs(18, 32): error CS1519: Invalid token '=' in class, struct, or interface member declaration
[07:54:56][Csc] Helpers'JsonNetResult.cs(18, 46): error CS1519: Invalid token ';' in class, struct, or interface member declaration
[07:54:56][Csc] Helpers'JsonNetResult.cs(19, 33): error CS1519: Invalid token ';' in class, struct, or interface member declaration
[07:54:56][Csc] Helpers'JsonNetResult.cs(22, 23): error CS1518: Expected class, delegate, enum, interface, or struct
[07:54:56][Csc] Helpers'JsonNetResult.cs(25, 37): error CS1518: Expected class, delegate, enum, interface, or struct
[07:54:56][Csc] Helpers'JsonNetResult.cs(32, 23): error CS1518: Expected class, delegate, enum, interface, or struct
[07:54:56][Csc] Helpers'JsonNetResult.cs(35, 37): error CS1518: Expected class, delegate, enum, interface, or struct
[07:54:56][Csc] Helpers'JsonNetResult.cs(40, 9): error CS1022: Type or namespace definition, or end-of-file expected
[07:54:56][Csc] Mailers'ITripMailer.cs(13, 132): error CS0241: Default parameter specifiers are not permitted
[07:54:56][Csc] Mailers'TripMailer.cs(54, 85): error CS0241: Default parameter specifiers are not permitted
[07:54:56][Csc] Services'Impl'AuthorizationService.cs(12, 70): error CS0241: Default parameter specifiers are not permitted
[07:54:56][Csc] Services'Impl'AuthorizationService.cs(43, 77): error CS0241: Default parameter specifiers are not permitted
[07:54:56][WebSite'WebSite.csproj.teamcity] Project WebSite'WebSite.csproj.teamcity failed.

使用msbuild从解决方案中发布一个web项目

几个月前我在http://sedodream.com/2013/03/06/HowToPublishOneWebProjectFromASolution.aspx上写过这篇文章。我在这里也复制了细节,见下面。


今天在twitter上@nunofcosta问了我一个粗略的问题:"我如何从一个包含许多的解决方案中发布一个web项目?"

他遇到的问题是他正在从命令行构建并将以下属性传递给msbuild.exe。

/p:DeployOnBuild=true
/p:PublishProfile='siteone - Web Deploy'
/p:Password=%password%

您可以在http://sedodream.com/2013/01/06/CommandLineWebProjectPublishing.aspx上阅读更多关于如何自动发布的信息。

将这些属性传递给msbuild.exe时,它们被称为全局属性。这些属性很难重写,并且会传递给所构建的每个项目。正因为如此,如果你有多个web项目的解决方案,当每个web项目被构建时,它被传递在相同的属性集。因此,当构建每个项目时,该项目的发布过程将启动,它将期望找到一个名为siteone - Web Deploy的文件。在文件夹*Properties'PublishProfiles*中的pubxml。如果文件不存在,操作可能会失败。

注意:如果您对使用此技术进行编排发布感兴趣,请在此之前查看我在https://stackoverflow.com/a/14231729/105999上的评论。

那么我们如何解决这个问题呢?

让我们看一个示例(参见下面的链接)。我有一个解决方案,PublishOnlyOne,与以下项目。

  1. ProjA
  2. ProjB

ProjA有一个名为' siteone - Web Deploy '的发布配置文件,ProjB没有。当尝试发布这个时,您可以尝试下面的命令行。

msbuild.exe PublishOnlyOne.sln /p:DeployOnBuild=true /p:PublishProfile=’siteone – Web Deploy’ /p:Password=%password%

看到publish-sln。

如果你这样做,当它的时间为ProjB构建它将失败,因为没有siteone - Web部署配置文件的项目。因此,我们不能通过deploybuild。下面是我们需要做的。

  1. 编辑ProjA。csproj定义另一个属性,它将有条件地设置DeployOnBuild
  2. 从命令行传入该属性

我编辑了ProjA,并在.csproj文件的Import语句之前添加了以下属性组。

<PropertyGroup>
<DeployOnBuild Condition=" '$(DeployProjA)'!='' ">$(DeployProjA)</DeployOnBuild>
</PropertyGroup>

在这里您可以看到deployyonbuild被设置为DeployProjA的任何值,只要它不是空的。现在修改后的命令是:

msbuild.exe PublishOnlyOne.sln /p:DeployProjA=true /p:PublishProfile=’siteone – Web Deploy’ /p:Password=%password%

这里不是传递deployyonbuild,而是传递DeployProjA,它将设置deployyonbuild。因为deploybuild没有传递给ProjB,所以它不会尝试发布。

您可以在https://github.com/sayedihashimi/sayed-samples/tree/master/PublishOnlyOne找到完整的示例。

有一个更简单的解决方案。MSBuild支持在构建解决方案时针对单个项目。您可以将项目名称放入Target参数中。注意,这是您在解决方案中指定的项目的可视名称(不一定与.csproj文件的名称相同)。

注意:这里需要的唯一"技巧"是将项目名称中的点(.)替换为下划线(_)

示例MSBuild命令行,如果你的项目名称是" your . project . name ":

msbuild.exe YourSolutionName.sln /T:"Your_Poject_Name" /P:DeployOnBuild=true /P:PublishProfile=YourPublishProfile.pubxml

您还可以为该项目指定一个构建目标,但是该目标应该存在于解决方案中的所有项目:

msbuild.exe YourSolutionName.sln /T:"Your_Poject_Name:Rebuild" /P:DeployOnBuild=true /P:PublishProfile=YourPublishProfile.pubxml

  1. 自Visual Studio 2008以来,这部分记录在MSDN中:https://msdn.microsoft.com/en-us/library/ms164311 (v = vs.140) . aspx
  2. 特别感谢Vasil Trifonov指出了替换技巧:http://www.codeproject.com/Articles/654910/How-to-build-a-specific-project-from-a-solution-wi

在需要手动自定义发布的场景中,另一个更好的选择是使用IsPublishable MSBuild属性(在这里找到https://github.com/dotnet/docs/issues/13365):

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp2.2</TargetFramework>
    <IsPublishable>false</IsPublishable>
  </PropertyGroup>
...
</Project>

MSBuild条件可用于根据不同的流设置IsPublishable值,例如:

    <IsPublishable Condition="'$(TargetFramework.TrimEnd(`0123456789`))' == 'net'>false</IsPublishable>