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

Publish one web project from solution with msbuild(使用 msbuild 从解决方案发布一个 Web 项目)
本文介绍了使用 msbuild 从解决方案发布一个 Web 项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

I'm trying to deploy one of the web projects in my solution to a server. I am using msbuild on TeamCity like so:

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

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

However, when I run it, msbuild still tries to build my WebService project, even though my WebSite project does not depend on it (but it does depend on a Services project also in the solution). How do only publish one project, aka just WebSite?

我也尝试过使用

msbuild WebSite/WebSite.csproj /p:DeployOnBuild=True ...

但它随后抱怨它无法恢复包:

but it then complains that it can't restore packages:

[07:47:17]WebSiteWebSite.csproj.teamcity: Build target: Build
[07:47:17][WebSiteWebSite.csproj.teamcity] RestorePackages
[07:47:17][RestorePackages] Exec
[07:47:17][Exec] C:TeamCityuildAgentworkcab8a3d752df3a51.nugetNuGet.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:TeamCityuildAgentworkcab8a3d752df3a51.nugetNuGet.targets(89, 9): error MSB4063: The "Exec" task could not be initialized with its input parameters. 
[07:47:17][WebSiteWebSite.csproj.teamcity] Project WebSiteWebSite.csproj.teamcity failed.

当我禁用 NuGet 包还原时,CoreCompile (Csc) 失败并出现我从未听说过且不应该发生的错误:

When I disable NuGet Package Restore, CoreCompile (Csc) fails with errors I've never heard of and shouldn't be happening:

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

推荐答案

我在 http://sedodream.com/2013/03/06/HowToPublishOneWebProjectFromASolution.aspx 几个月前.我在这里也复制了详细信息,请参见下文.

I blogged about this at http://sedodream.com/2013/03/06/HowToPublishOneWebProjectFromASolution.aspx a few months back. I've copied the details here as well, see below.

今天在推特上 @nunofcosta 粗略地问了我一个问题我如何从一个解决方案发布一个 Web 项目包含很多?"

Today on twitter @nunofcosta asked me roughly the question "How do I publish one web project from a solution that contains many?"

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

The issue that he is running into is that he is building from the command line and passing the following properties to 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 项目时,它会在同一组属性中传递.因此,在构建每个项目时,该项目的发布过程将开始,并且它会在文件夹 *PropertiesPublishProfiles* 中找到一个名为 siteone – Web Deploy.pubxml 的文件.如果文件不存在,则操作可能会失败.

When you pass these properties to msbuild.exe they are known as global properties. These properties are difficult to override and are passed to every project that is built. Because of this if you have a solution with multiple web projects, when each web project is built it is passed in the same set of properties. Because of this when each project is built the publish process for that project will start and it will expect to find a file named siteone – Web Deploy.pubxml in the folder *PropertiesPublishProfiles*. If the file doesn’t exist the operation may fail.

注意:如果您有兴趣使用此技术进行编排发布,请参阅我在 https://stackoverflow 上的评论.com/a/14231729/105999 在这样做之前.

Note: If you are interested in using this technique for an orchestrated publish see my comments at https://stackoverflow.com/a/14231729/105999 before doing so.

那么我们该如何解决呢?

So how can we resolve this?

让我们看一个示例(请参阅下面的链接).我有一个解决方案,PublishOnlyOne,包含以下项目.

Let’s take a look at a sample (see links below). I have a solution, PublishOnlyOne, with the following projects.

  1. 项目
  2. 项目B

ProjA 有一个名为siteone – Web Deploy"的发布配置文件,ProjB 没有.尝试发布此内容时,您可以尝试以下命令行.

ProjA has a publish profile named ‘siteone – Web Deploy’, ProjB does not. When trying to publish this you may try the following command line.

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

请参阅示例中的 publish-sln.cmd.

如果您这样做,那么当 ProjB 构建它的时候将失败,因为该项目没有 siteone – Web Deploy 配置文件.因此,我们无法通过 DeployOnBuild.相反,这是我们需要做的.

If you do this, when its time for ProjB to build it will fail because there’s no siteone – Web Deploy profile for that project. Because of this, we cannot pass DeployOnBuild. Instead here is what we need to do.

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

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

I edited ProjA and added the following property group before the Import statements in the .csproj file.

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

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

Here you can see that DeployOnBuild is set to whatever value DeployProjA is as long as it’s not empty. Now the revised command is:

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

这里我没有传递 DeployOnBuild,而是传递了 DeployProjA,然后它将设置 DeployOnBuild.由于 DeployOnBuild 没有传递给 ProjB,它不会尝试发布.

Here instead of passing DeployOnBuild, I pass in DeployProjA which will then set DeployOnBuild. Since DeployOnBuild wasn’t passed to ProjB it will not attempt to publish.

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

这篇关于使用 msbuild 从解决方案发布一个 Web 项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

MSBuild cannot find a reference(MSBuild 找不到参考)
The reference assemblies for framework .NETCore, Version=v5.0 were not found(未找到框架 .NETCore,Version=v5.0 的参考程序集)
quot;File has a different computed hash than specified in manifestquot; error when signing the EXE(“文件的计算哈希值与清单中指定的不同签署EXE时出错)
Good-practices: How to reuse .csproj and .sln files to create your MSBuild script for CI?(良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 MSBuild 脚本?)
Run an MSBuild target only if project is actually built(仅在实际构建项目时运行 MSBuild 目标)
MS-Build BeforeBuild not firing(MS-Build BeforeBuild 未触发)