一、前言

在上一篇文章.Net Core迁移到MSBuild的多平台编译问题中,简单的讲了下新的项目配置文件中的节点配置,这篇我将用一些例子来详细讲下从project.json迁移到msbuild过程的节点配置。做过完整迁移新项目配置文件的朋友,肯定会觉得新的项目配置文件Msbuild的配置太扯了,虽然能直接编辑项目文件,但整个配置文件中没有了像project.json中的智能提示,当你在打开文件后你就会发现以前很轻松能写出来的json配置,现在已经什么都写不出来了,而且也没有文档可以参考,一般的情况下,往往开发人员就会关掉项目文件,打开NuGet管理器来进行包引用,但是这真的够用吗?不是所有的配置都能用可视化的方法来完成。

回到目录

二、XML定义

新的.csproj是基于xml格式的,下面介绍下project.json与.csproj文件的差异定义的例子:

项目名称 (ProjectName)

{"name": "MyProjectName"}

在csproj的配置中并没有对应的定义,它只会有项目文件名相同如:MyProjectName.csproj

程序集版本 (Version)

{"version": "1.0.0-alpha-*"}
<PropertyGroup><VersionPrefix>1.0.0</VersionPrefix><VersionSuffix>alpha</VersionSuffix></PropertyGroup>

当然也可以只使用Version来定义:

<PropertyGroup><Version>1.0.0-alpha</Version></PropertyGroup>

程序集描述

{"authors": [ "Anne", "Bob" ],"company": "Contoso","language": "en-US","title": "My library","description": "This is my library.\r\nAnd it's really great!","copyright": "Nugetizer 3000","userSecretsId": "xyz123"}
<PropertyGroup><Authors>Anne;Bob<Authors><Company>Contoso<Company><NeutralLanguage>en-US</NeutralLanguage><AssemblyTitle>My library</AssemblyTitle><Description>This is my library.
And it's really great!</Description><Copyright>Nugetizer 3000</Copyright><UserSecretsId>xyz123</UserSecretsId></PropertyGroup>

frameworks (单目标框架)

  "frameworks": {"netcoreapp1.0": {}}
<PropertyGroup><TargetFramework>netcoreapp1.0</TargetFramework></PropertyGroup>

frameworks (多目标框架)

 "frameworks": {"netcoreapp1.0": {},"net451": {}}
<PropertyGroup><TargetFrameworks>netcoreapp1.0;net451</TargetFrameworks></PropertyGroup>

dependencies (框架依赖)

"dependencies": {"Microsoft.AspNetCore": "1.1.0"}
<ItemGroup><PackageReferenceInclude="Microsoft.AspNetCore"Version="1.1.0"/></ItemGroup>

不同目标框架的依赖 (Per-framework dependencies)

{"framework": {"net451": {"dependencies": {"System.Collections.Immutable": "1.3.1"}},"netstandard1.5": {"dependencies": {"Newtonsoft.Json": "9.0.1"}}}
}
<ItemGroupCondition="'$(TargetFramework)'=='net451'"><PackageReferenceInclude="System.Collections.Immutable"Version="1.3.1"/></ItemGroup><ItemGroupCondition="'$(TargetFramework)'=='netstandard1.5'"><PackageReferenceInclude="Newtonsoft.Json"Version="9.0.1"/></ItemGroup>

imports (兼容导入)

 {"dependencies": {"xxx": "1.0-pre001"},"frameworks": {"netcoreapp1.0": {"imports": ["dnxcore50","dotnet"]}}}
<PropertyGroup><PackageTargetFallback>dnxcore50;dotnet</PackageTargetFallback></PropertyGroup><ItemGroup><PackageReferenceInclude="xxx"Version="1.0-pre001"/></ItemGroup>

依赖类型 (dependency type)

type: build

{"dependencies": {"Microsoft.EntityFrameworkCore.Design": {"version": "1.1.0","type": "build"}}}
<ItemGroup><PackageReferenceInclude="Microsoft.EntityFrameworkCore.Design"Version="1.1.0"PrivateAssets="All"/></ItemGroup>

type: platform

{"dependencies": {"Microsoft.NETCore.App": {"version": "1.1.0","type": "platform"}}}

在*.csproj项目配置文件中没有对应的配置节点,只有目标框架定义:

<TargetFramework>netcoreapp1.1</TargetFramework>

之前想要编译出独立发布的可执行文件,就需要把 "type": "platform"节点删除掉。

独立发布定义 (runtimes)

{"runtimes": {"win7-x64": {},"osx.10.11-x64": {},"ubuntu.16.04-x64": {}}}
<PropertyGroup><RuntimeIdentifiers>win7-x64;osx.10-11-x64;ubuntu.16.04-x64</RuntimeIdentifiers></PropertyGroup>

现在想生成独立发布版本,只需要在项目配置中定义RuntimeIdentifiers节点,并运行如下命令:

dotnet publish --framework netcoreapp1.0 --runtime osx.10.11-x64

DOTNET CLI工具 (tools)

{"tools": {"Microsoft.EntityFrameworkCore.Tools.DotNet": "1.0.0-*"}}
<ItemGroup><DotNetCliToolReferenceInclude="Microsoft.EntityFrameworkCore.Tools.DotNet"Version="1.0.0"/></ItemGroup>

提示:tools下的引用,不再支持“imports”节点定义(不能兼容非dotnet core版本的tools)。

编译可执行 (emitEntryPoint)

{"buildOptions": {"emitEntryPoint": true}}
<PropertyGroup><OutputType>Exe</OutputType></PropertyGroup>
{"buildOptions": {"emitEntryPoint": false}}
<PropertyGroup><OutputType>Library</OutputType></PropertyGroup>

程序集强命名签名 (keyFile)

{"buildOptions": {"keyFile": "MyKey.snk"}}
<PropertyGroup><AssemblyOriginatorKeyFile>MyKey.snk</AssemblyOriginatorKeyFile><SignAssembly>true</SignAssembly><PublicSignCondition="'$(OS)' != 'Windows_NT'">true</PublicSign></PropertyGroup>

其它编译设置

{"buildOptions": {"warningsAsErrors": true,"nowarn": ["CS0168", "CS0219"],"xmlDoc": true,"preserveCompilationContext": true,"outputName": "Different.AssemblyName","debugType": "portable","allowUnsafe": true,"define": ["TEST", "OTHERCONDITION"]}}
<PropertyGroup><TreatWarningsAsErrors>true</TreatWarningsAsErrors><NoWarn>$(NoWarn);CS0168;CS0219</NoWarn><GenerateDocumentationFile>true</GenerateDocumentationFile><PreserveCompliationContext>true</PreserveCompliationContext><AssemblyName>Different.AssemblyName</AssemblyName><DebugType>portable</DebugType><AllowUnsafeBlocks>true</AllowUnsafeBlocks><DefineConstants>$(DefineConstants);TEST;OTHERCONDITION</DefineConstants></PropertyGroup>

打包设置 (packOptions)

{"packOptions": {"summary": "A bundle of cats","tags": ["hyperscale", "cats"],"owners": [ "Nate", "Jenna" ],"releaseNotes": "Version 1.0","iconUrl": "https://icons.com/awesomeness.png","projectUrl": "https://github.com/natemcmaster","licenseUrl": "https://www.apache.org/licenses/LICENSE-2.0","requireLicenseAcceptance": false,"repository": {"type": "git","url": "https://github.com/natemcmaster/natemcmaster.github.io"}}}
<PropertyGroup><Description>A bundle of cats</Description><PackageTags>hyperscale;cats</PackageTags><PackageReleaseNotes>Version 1.0</PackageReleaseNotes><PackageIconUrl>https://icons.com/awesomeness.png</PackageIconUrl><PackageProjectUrl>https://github.com/natemcmaster</PackageProjectUrl><PackageLicenseUrl>https://www.apache.org/licenses/LICENSE-2.0</PackageLicenseUrl><PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance><RepositoryType>git</RepositoryType><RepositoryUrl>https://github.com/natemcmaster/natemcmaster.github.io</RepositoryUrl><!-- regrettably, 'owners' does not translate to MSBuild. --></PropertyGroup>

MsBuild脚本

{"scripts": {"precompile": "generateCode.cmd","postpublish": [ "obfuscate.cmd", "removeTempFiles.cmd" ]}}
<TargetName="MyPreCompileTarget"BeforeTargets="Build"><ExecCommand="generateCode.cmd"/></Target><TargetName="MyPostCompileTarget"AfterTargets="Publish"><ExecCommand="obfuscate.cmd"/><ExecCommand="removeTempFiles.cmd"/></Target>

运行时设置 (runtimeOptions)

{"runtimeOptions": {"configProperties": {"System.GC.Server": true,"System.GC.Concurrent": true,"System.GC.RetainVM": true,"System.Threading.ThreadPool.MinThreads": 10,"System.Threading.ThreadPool.MaxThreads": 100}}}
<PropertyGroup><ServerGarbageCollection>true</ServerGarbageCollection><ConcurrentGarbageCollection>true</ConcurrentGarbageCollection><RetainVMGarbageCollection>true</RetainVMGarbageCollection><!-- I'm not suggesting these settings...just showing usage ;) --><ThreadPoolMinThreads>10</ThreadPoolMinThreads><ThreadPoolMaxThreads>100</ThreadPoolMaxThreads></ProeprtyGroup>

当然如果你创建的是一个web项目的话,及Microsoft.NET.Sdk.Web。那么 ServerGarbageCollection设置将默认为true。

项目文件管理

{"buildOptions": {"compile": {"copyToOutput": "notes.txt","include": "../Shared/*.cs","exclude": "../Shared/Not/*.cs"},"embed": {"include": "../Shared/*.resx"}},"packOptions": {"include": "Views/","mappings": {"some/path/in/project.txt": "in/package.txt"}},"publishOptions": {"include": ["files/","publishnotes.txt"]}}
<ItemGroup><CompileInclude="..\Shared\*.cs"Exclude="..\Shared\Not\*.cs"/><EmbeddedResourceInclude="..\Shared\*.resx"/><ContentInclude="Views\**\*"PackagePath="%(Identity)"/><NoneInclude="some/path/in/project.txt"Pack="true"PackagePath="in/package.txt"/><NoneInclude="notes.txt"CopyToOutputDirectory="Always"/><!-- CopyToOutputDirectory = { Always, PreserveNewest, Never } --><ContentInclude="files\**\*"CopyToPublishDirectory="PreserveNewest"/><NoneInclude="publishnotes.txt"CopyToPublishDirectory="Always"/><!-- CopyToPublishDirectory = { Always, PreserveNewest, Never } --><!-- you can set both copy output and publish directories--><NoneInclude="testasset.txt"CopyToOutputDirectory="Always"CopyToPublishDirectory="Always"/><!-- alternatively, use nested XML attributes. They're functionally the same--><NoneInclude="testasset2.txt"><CopyToOutputDirectory>Always</CopyToOutputDirectory><CopyToPublishDirectory>Always</CopyToPublishDirectory></None></ItemGroup>

单元测试

xunit

{"testRunner": "xunit","dependencies": {"dotnet-test-xunit": "<any>"}}
<ItemGroup><PackageReferenceInclude="Microsoft.NET.Test.Sdk"Version="15.0.0"/><PackageReferenceInclude="xunit"Version="2.2.0"/><PackageReferenceInclude="xunit.runner.visualstudio"Version="2.2.0"/></ItemGroup>

mstest

{"testRunner": "mstest","dependencies": {"dotnet-test-mstest": "<any>"}}
<ItemGroup><PackageReferenceInclude="Microsoft.NET.Test.Sdk"Version="15.0.0"/><PackageReferenceInclude="MSTest.TestAdapter"Version="1.1.12"/><PackageReferenceInclude="MSTest.TestFramework"Version="1.1.11"/></ItemGroup>

回到目录

三、结语

说实话MSBuild的项目配置系统还是比较灵活的,以后整个dotnet体系的构建过程也都得到了统一。在dotnet cli中也集成了msbuild,即dotnet build。

原文地址:http://www.cnblogs.com/maxzhang1985/p/6550001.html


.NET社区新闻,深度好文,微信中搜索dotNET跨平台或扫描二维码关注

赞赏

.Net Core迁移到MSBuild平台相关推荐

  1. .Net Core迁移到MSBuild的多平台编译问题

    一.前言 本篇主要讨论.NET Core应用程序项目结构的主题,重点探索.NET Core应用程序的多平台编译问题,这里指的多平台是指.NET Framework..NET Core App..NET ...

  2. .NET Core 迁移躺坑记续集--Win下莫名其妙的超时

    继上一集.NET Core 迁移躺坑记里说到遇到的各种问题并且弄了n个解决方案之后,特别是对于问题4的解决方案对于切换了HttpClientFactory 我用了你家netcore 2.1下专门解决之 ...

  3. 【华为云技术分享】如何将代码自动迁移到鲲鹏平台

    本文内容源视频地址: https://huaweicloud.bugu.mudu.tv/watch/ym1bzp7p 大家好,今天要讲的主题是关于软件迁移,这是一个久远的话题,因为但凡牵扯到切换平台. ...

  4. Mavenir研究表明,向融合5G Packet Core迁移可将总拥有成本降低多达36%

    早期采用的移动网络运营商能够节省资本支出和运营支出 德州理查森--(美国商业资讯)--Mavenir是一家网络软件提供商,致力于利用可以在任何云上运行并能够改变世界连接方式的云原生软件来构建面向未来的 ...

  5. 蝉知门户系统迁移到SAE平台-对蝉知2.5版本部分功能的限制

    蝉知2.5版本加入了部分新功能,使用起来更加方便.但在sae平台上受限于平台环境,其中的插件安装.模板安装功能由于没有写权限无法使用.需要在迁移至sae平台时做出限制,提示用户进行其他方式的安装. 1 ...

  6. 从小型机到x86:四川电信核心数据库迁移到虚拟化平台

    (近期,TechTarget中国记者孙瑞对四川电信将CRM核心数据库从小型机迁移到x86虚拟化平台的项目进行了采访.以下是对本事件的详细报道.) 从近两年开始,一场起源于国内某知名互联网企业的&quo ...

  7. 企业如何通过迁移到云平台来减少开支

    由于发生新冠疫情以及因此员工大规模实施远程工作,很多企业加快了其数字化转型计划.根据英国政府发布的一份调查报告,企业迅速投资于技术以重塑工作场所以及员工的工作和互动方式,并将这些技术(例如云计算和视频 ...

  8. .NET Core迁移前的准备工作

    前段时间迁移.NET Core做了大量的试水和评估,今天整理一下分享给大家.大致有以下几个部分: 1. .NET Core的由来 2. 为什么要迁移.NET Core 3. .NET Core3.X主 ...

  9. EntityFramework Core 迁移忽略主外键关系

    [导读]本文来源于一位公众号童鞋私信我的问题,在我稍加思索后给出了如下一种方案,在此之前我也思考过这个问题,借此机会我稍微看了下,目前能够想到的也只是本文所述方案. 为何要忽略主外键关系 我们不仅疑惑 ...

最新文章

  1. 学计算机有哪些大学专业,计算机专业:最好的7所大学!也是全中国“最难考”的大学!...
  2. c++中类的申明和定义
  3. python归一化sklearn_用sklearn进行对数据标准化、归一化以及将数据还原详解
  4. winform插入时间类型数据到oracle数据库,winform操作访问Oracle 10g数据库,并自动填充到DataGridView...
  5. LeetCode 217 存在重复元素
  6. MPLS 次末跳弹出配置_weblogic的安装与配置
  7. python类中的 init_Python类中__init__()的作用
  8. JAVA day13,14 API、Object类、日期时间类(long,Date,Calendar,DateFormat)、String类(字符串,可变长字符串)、正则表达式、包装类
  9. python tkinter 定时_如何使用tkinter创建计时器?
  10. oracle 052 题库变了,oracle ocp题库变化,052新加的考试题收集整理-30
  11. 贪吃蛇小游戏(C语言实现简易版)
  12. 使用学信网认证,免费获取JetBrains学习产品
  13. 基于JSP的班级聚会网站
  14. 有逆时针将视频画面旋转90度的方法吗?
  15. 全球最易受黑客攻击的国家:中国排第五
  16. jquery.min.js:2 Uncaught Error: Syntax error, unrecognized expression: unsupported pseudo: blank
  17. Dynamic Address Validation Array (DAVA): A Moving Target Defense Protocol for CANBus 论文笔记
  18. 使用BN时ValueError: expected 2D or 3D input (got 4D input)的可能原因
  19. 《CSDN 涨粉攻略》11个涨粉方法,你学会了几个?
  20. ubuntu:软件包

热门文章

  1. Shell配置_配置IP
  2. LNMP服务器安装配置(Rhel+Nginx+PHP+MySQL)
  3. Spring 事物传播特性
  4. 坑爹!千万不要在生产环境使用控制台日志
  5. Blazor 应用如何使用 Azure Active Directory 认证登录
  6. 如何在 NET 程序万种死法中有效的生成 Dump (下)
  7. 从CLR GC到CoreCLR GC看.NET Core为云而生
  8. Kubernetes探针踩坑记
  9. .NET Core微服务开发选项
  10. 【朝夕Net社区技术专刊】Core3.1 WebApi集群实战专题-Corre3.1WebApi配置集成日志/配置Swagger...