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

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

问题描述

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

我有一个使用msbuild为.NET 4.x编译的.NET 4.7库项目(通过为.NET 4.7创建类库(.NET Framework)"项目,可以很容易地重现此问题).

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

  dotnet msbuild/property:TargetFrameworkVersion=net5.0/property:TargetFrameworkIdentifier=.NETCoreApp适用于.NET的Microsoft(R)Build Engine版本16.8.0 + 126527ff1版权所有(C)Microsoft Corporation.版权所有.C:\ Program Files \ dotnet \ sdk \ 5.0.100 \ Roslyn \ Microsoft.CSharp.Core.targets(8,31):错误MSB4086:尝试对"$(_ TargetFrameworkVersionWithoutV)"进行数字比较评估为"的而不是数字,条件是('$(TargetFrameworkIdentifier)'!='.NETCoreApp'或'$(_ TargetFrameworkVersionWithoutV)'<'3.0')AND [H:\ ClassLibrary1 \ ClassLibrary1.csproj]C:\ Program Files \ dotnet \ sdk \ 5.0.100 \ Roslyn \ Microsoft.CSharp.Core.targets(8,31):error MSB4086:('$(TargetFrameworkIdentifier)'!='.NETStandard'或'$(_ TargetFrameworkVersionWithoutV)'<'2.1')'.[H:\ ClassLibrary1 \ ClassLibrary1.csproj] 

这似乎想要 $(_ TargetFrameworkVersionWithoutV),所以我尝试了

  dotnet msbuild/property:TargetFrameworkVersion=net5.0/property:TargetFrameworkIdentifier=.NETCoreApp/property:_TargetFrameworkVersionWithoutV=5.0适用于.NET的Microsoft(R)Build Engine版本16.8.0 + 126527ff1版权所有(C)Microsoft Corporation.版权所有.C:\ Program Files \ dotnet \ sdk \ 5.0.100 \ Microsoft.Common.CurrentVersion.targets(84,5):错误MSB4184:表达式"[[Microsoft.Build.Utilities.ToolLocationHelper] :: GetPathToStandardLibraries(.NETCoreApp,net5.0,'','','','')''无法评估.输入的字符串格式不正确.[H:\ ClassLibrary1 \ ClassLibrary1.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)Build Engine版本16.8.0 + 126527ff1版权所有(C)Microsoft Corporation.版权所有.C:\ Program Files \ dotnet \ sdk \ 5.0.100 \ Microsoft.Common.CurrentVersion.targets(1180,5):错误MSB3644:找不到.NETCoreApp,Version = v5.0的参考程序集.若要解决此问题,请为此框架版本安装Developer Pack(SDK/Targeting Pack)或重新定位您的应用程序.您可以从以下网址下载.NET Framework开发人员包:https://aka.ms/msbuild/developerpacks [H:\ ClassLibrary1 \ ClassLibrary1.csproj] 

解决方案

NET 5 Net Core 而不是 Net Framework 的新发行版本.strong>.

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

因此,您不能将new-SDK样式项目的属性用于非SDK样式的项目.请参见本文档.

,新SDK样式项目使用 TargetFramework ,而非SDK样式项目使用 TargetFrameworkVersion .并且 .NET 5 应该与 TargetFramework 一起使用,但是您的非SDK项目无法识别它.

解决方案

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

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

 < TargetFramework> net47</TargetFramework> 

然后将您旧项目的内容迁移到新项目中,这意味着该项目是具有网络框架的 new-sdk 样式项目.

请参见从.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 Files\dotnet\sdk\5.0.100\Roslyn\Microsoft.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:\ClassLibrary1\ClassLibrary1.csproj]
C:\Program Files\dotnet\sdk\5.0.100\Roslyn\Microsoft.CSharp.Core.targets(8,31): error MSB4086:                                          ('$(TargetFrameworkIdentifier)' != '.NETStandard' OR '$(_TargetFrameworkVersionWithoutV)' < '2.1')". [H:\ClassLibrary1\ClassLibrary1.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 Files\dotnet\sdk\5.0.100\Microsoft.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:\ClassLibrary1\ClassLibrary1.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 Files\dotnet\sdk\5.0.100\Microsoft.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:\ClassLibrary1\ClassLibrary1.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天全站免登陆