如何使用 MSBuild 为 .NET 5 构建现有的 .NET 4 项目? [英] How to use MSBuild to build an existing .NET 4 project for .NET 5?

查看:39
本文介绍了如何使用 MSBuild 为 .NET 5 构建现有的 .NET 4 项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以针对 .NET 5 SDK msbuild 为 .NET 4.x 创建的项目,而无需在 VS 中升级项目?

我有一个 .NET 4.7 库项目,我使用 msbuild 为 .NET 4.x 编译(通过为 .NET 4.7 创建类库(.NET 框架)"项目可以轻松重现这个问题).

我想为 .NET 5 构建它,所以我尝试了

dotnet msbuild/property:TargetFrameworkVersion=net5.0/property:TargetFrameworkIdentifier=.NETCoreApp适用于 .NET 的 Microsoft (R) 构建引擎版本 16.8.0+126527ff1版权所有 (C) 微软公司.版权所有.C:Program Filesdotnetsdk5.0.100RoslynMicrosoft.CSharp.Core.targets(8,31):错误 MSB4086:尝试对$(_TargetFrameworkVersionWithoutV)"进行数值比较;评估为"而不是数字,在条件 "('$(TargetFrameworkIdentifier)' != '.NETCoreApp' OR '$(_TargetFrameworkVersionWithoutV)' < '3.0') AND [H:ClassLibrary1ClassLibrary1.csproj]C:Program Filesdotnetsdk5.0.100RoslynMicrosoft.CSharp.Core.targets(8,31): 错误 MSB4086: ('$(TargetFrameworkIdentifier)' != '.NETStandard' OR '$(_TargetFrameworkVersionWithoutV))' < '2.1')​​'.[H:ClassLibrary1ClassLibrary1.csproj]

这似乎想要 $(_TargetFrameworkVersionWithoutV) 所以我尝试了

dotnet msbuild/property:TargetFrameworkVersion=net5.0/property:TargetFrameworkIdentifier=.NETCoreApp/property:_TargetFrameworkVersionWithoutV=5.0适用于 .NET 的 Microsoft (R) 构建引擎版本 16.8.0+126527ff1版权所有 (C) 微软公司.版权所有.C:Program Filesdotnetsdk5.0.100Microsoft.Common.CurrentVersion.targets(84,5): error MSB4184: The expression "[Microsoft.Build.Utilities.ToolLocationHelper]::GetPathToStandardLibraries(.NETCoreApp,net5.0, '', '', '', '')"无法评价.输入字符串的格式不正确.[H:ClassLibrary1ClassLibrary1.csproj]

PS:我也尝试将纯 .NET 5 库项目与 .NET 4 库项目进行比较,并以此为基础尝试了这个

dotnet msbuild/property:TargetFramework=net5.0/property:TargetFrameworkIdentifier=.NETCoreApp/property:_TargetFrameworkVersionWithoutV=5.0/property:TargetFrameworkVersion=5.0适用于 .NET 的 Microsoft (R) 构建引擎版本 16.8.0+126527ff1版权所有 (C) 微软公司.版权所有.C:Program Filesdotnetsdk5.0.100Microsoft.Common.CurrentVersion.targets(1180,5):错误 MSB3644:未找到 .NETCoreApp,Version=v5.0 的参考程序集.要解决此问题,请为此框架版本安装开发人员包(SDK/目标包)或重新定位您的应用程序.您可以在 https://aka.ms/msbuild/developerpacks [H:ClassLibrary1ClassLibrary1.csproj] 下载 .NET Framework 开发包

解决方案

NET 5Net Core 而不是 Net Framework 的新发布版本强>.

NET 5 适用于新的 sdk 样式项目,而您身边的 net framework 4.7 项目是非 sdk 样式的项目.

所以您不能将 new-sdk 样式项目的属性用于非 sdk 样式项目.请参阅本文档.

另外,新sdk风格的项目使用TargetFramework,而非sdk风格的项目使用TargetFrameworkVersion.并且 .NET 5 应该与 TargetFramework 一起使用,但您的非 sdk 项目无法识别它.

解决方案

因此您应该将您的网络框架项目从 non-sdk 更改为 new-sdk.

您可以创建一个新的net core项目,然后将其xxx.csproj文件更改为:

net47

然后将旧项目的内容迁移到新项目中,这意味着该项目是一个 new-sdk 风格的带有 net framework 的项目.

请参阅从 .NET Framework 移植到 .NET Core 的概述这个文件.

然后,你可以使用

dotnet msbuild/property:TargetFramework=net5.0

Is it possible to msbuild a project that was created for .NET 4.x against the .NET 5 SDK without upgrading the project in VS?

I have a .NET 4.7 library project that I compile for .NET 4.x using msbuild (and this issue is easily reproducible by creating a "Class Library (.NET Framework)" project for .NET 4.7).

I want to build it for .NET 5, so I tried

dotnet msbuild /property:TargetFrameworkVersion=net5.0 /property:TargetFrameworkIdentifier=.NETCoreApp



Microsoft (R) Build Engine version 16.8.0+126527ff1 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

C:Program Filesdotnetsdk5.0.100RoslynMicrosoft.CSharp.Core.targets(8,31): error MSB4086: A numeric comparison was attempted on "$(_TargetFrameworkVersionWithoutV)" that evaluates to "" instead of a number, in condition "('$(TargetFrameworkIdentifier)' != '.NETCoreApp' OR '$(_TargetFrameworkVersionWithoutV)' < '3.0') AND [H:ClassLibrary1ClassLibrary1.csproj]
C:Program Filesdotnetsdk5.0.100RoslynMicrosoft.CSharp.Core.targets(8,31): error MSB4086:                                          ('$(TargetFrameworkIdentifier)' != '.NETStandard' OR '$(_TargetFrameworkVersionWithoutV)' < '2.1')". [H:ClassLibrary1ClassLibrary1.csproj]

That seems to want $(_TargetFrameworkVersionWithoutV) so I tried

dotnet msbuild /property:TargetFrameworkVersion=net5.0 /property:TargetFrameworkIdentifier=.NETCoreApp  /property:_TargetFrameworkVersionWithoutV=5.0
Microsoft (R) Build Engine version 16.8.0+126527ff1 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

C:Program Filesdotnetsdk5.0.100Microsoft.Common.CurrentVersion.targets(84,5): error MSB4184: The expression "[Microsoft.Build.Utilities.ToolLocationHelper]::GetPathToStandardLibraries(.NETCoreApp, net5.0, '', '', '', '')" cannot be evaluated. Input string was not in a correct format. [H:ClassLibrary1ClassLibrary1.csproj]

PS: I did also try comparing a pure .NET 5 library project against a .NET 4 one and based on that tried this

dotnet msbuild /property:TargetFramework=net5.0 /property:TargetFrameworkIdentifier=.NETCoreApp  /property:_TargetFrameworkVersionWithoutV=5.0 /property:TargetFrameworkVersion=5.0
Microsoft (R) Build Engine version 16.8.0+126527ff1 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

C:Program Filesdotnetsdk5.0.100Microsoft.Common.CurrentVersion.targets(1180,5): error MSB3644: The reference assemblies for .NETCoreApp,Version=v5.0 were not found. To resolve this, install the Developer Pack (SDK/Targeting Pack) for this framework version or retarget your application. You can download .NET Framework Developer Packs at https://aka.ms/msbuild/developerpacks [H:ClassLibrary1ClassLibrary1.csproj]

解决方案

NET 5 is the new release version of Net Core rather than Net Framework.

See this document:

And NET 5 is for new-sdk style project while net framework 4.7 project in your side is non-sdk style project.

So you can not use a new-sdk style project's property for a non-sdk style project. See this document.

Also, new-sdk style projects use TargetFramework while non-sdk style project uses TargetFrameworkVersion. And .NET 5 should be used with TargetFramework but your non-sdk project does not identify it.

Solution

So you should change your net framework project from non-sdk into new-sdk.

You could create a new net core project and then change its xxx.csproj file into:

<TargetFramework>net47</TargetFramework>

And then migrate your old project's content into the new one which means that this project is a new-sdk style project with net framework.

See Overview of porting from .NET Framework to .NET Core and also this document.

Then, you can use

dotnet msbuild /property:TargetFramework=net5.0

这篇关于如何使用 MSBuild 为 .NET 5 构建现有的 .NET 4 项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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