问题
I have a dotnet SDK .sln
(and a build.proj
) with <TargetFrameworks>netcoreapp2.1;net461</TargetFrameworks>
. It builds on Windows using Visual Studio and dotnet build
, but I'd also like it to build as many other places as possible. What do I need to put in my README.md and/or what can I put in the project files to make it build on Rider and/or on from bash on a Mac?
回答1:
(Currently) The simplest way to build for a .NET Framework TFM when running on either macOS or Linux using the .NET Core CLI, is to utilize the .NET Framework Targeting Pack Nuget Packages from Microsoft (currently in preview):
These packages enable building .NET Framework projects on any machine with at least MSBuild or the .NET Core SDK installed.
The following scenarios and benefits are enabled for .NET Framework projects:
- Build without requiring admin operations to install pre-requisites such as Visual Studio or .NET Framework targeting packs.
- Build libraries on any operating system supported by the .NET Core SDK.
- Build Mono-based projects.
You may either include the Microsoft.NETFramework.ReferenceAssemblies metapackage; or use just the specific package, which is in your case Microsoft.NETFramework.ReferenceAssemblies.net461.
Add the package to the *.csproj
or your Directory.Build.props
:
<Project>
<ItemGroup>
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.0-preview.2" PrivateAssets="All" />
</ItemGroup>
</Project>
Note: The PrivateAssets
attribute controls which dependency assets will be consumed but won't flow to the parent project. See the docs.
回答2:
In order to build via bash on a vanilla Mac, the minimal steps seem to be:
- Install Mono 6.0 (5.2 is recommended for VS Mac interop, I dont care about that, and Mono 6.0's interop with Dotnet core is better)
- Install dotnet SDK 2.2 (doesn't have to be exactly that, but it works for me)
- Put this in a Directory.build.props file (open to improvements if anyone has any)
<Project> <PropertyGroup> <IsOSX Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::OSX)))'== 'true'">true</IsOSX> </PropertyGroup> <PropertyGroup Condition=" '$(IsOSX)' == 'true' "> <FrameworkPathOverride>/Library/Frameworks/Mono.framework/Versions/Current/Commands/../lib/mono/4.6.1-api</FrameworkPathOverride> </PropertyGroup> </Project>
- Bash:
dotnet build SolutionFileName.sln
should now work - Install Rider 2019.1 or later
- Rider: should Just Work (it should autodetect msbuild 16.0 in the build tools section)
来源:https://stackoverflow.com/questions/57382030/simplest-way-to-build-dotnet-sdk-project-requiring-net461-on-macos