一、前言

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

二、XML定义

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

项目名称 (ProjectName)

  1. {
  2. "name": "MyProjectName"
  3. }

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

程序集版本 (Version)

  1. {
  2. "version": "1.0.0-alpha-*"
  3. }
  1. <PropertyGroup>
  2. <VersionPrefix>1.0.0</VersionPrefix>
  3. <VersionSuffix>alpha</VersionSuffix>
  4. </PropertyGroup>

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

  1. <PropertyGroup>
  2. <Version>1.0.0-alpha</Version>
  3. </PropertyGroup>

程序集描述

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

frameworks (单目标框架)

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

frameworks (多目标框架)

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

dependencies (框架依赖)

  1. "dependencies": {
  2. "Microsoft.AspNetCore": "1.1.0"
  3. }
  1. <ItemGroup>
  2. <PackageReference Include="Microsoft.AspNetCore" Version="1.1.0" />
  3. </ItemGroup>

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

  1. {
  2. "framework": {
  3. "net451": {
  4. "dependencies": {
  5. "System.Collections.Immutable": "1.3.1"
  6. }
  7. },
  8. "netstandard1.5": {
  9. "dependencies": {
  10. "Newtonsoft.Json": "9.0.1"
  11. }
  12. }
  13. }
  14. }
  1. <ItemGroup Condition="'$(TargetFramework)'=='net451'">
  2. <PackageReference Include="System.Collections.Immutable" Version="1.3.1" />
  3. </ItemGroup>
  4. <ItemGroup Condition="'$(TargetFramework)'=='netstandard1.5'">
  5. <PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
  6. </ItemGroup>

imports (兼容导入)

  1. {
  2. "dependencies": {
  3. "xxx": "1.0-pre001"
  4. },
  5. "frameworks": {
  6. "netcoreapp1.0": {
  7. "imports": [
  8. "dnxcore50",
  9. "dotnet"
  10. ]
  11. }
  12. }
  13. }
  1. <PropertyGroup>
  2. <PackageTargetFallback>dnxcore50;dotnet</PackageTargetFallback>
  3. </PropertyGroup>
  4. <ItemGroup>
  5. <PackageReference Include="xxx" Version="1.0-pre001" />
  6. </ItemGroup>

依赖类型 (dependency type)

type: build

  1. {
  2. "dependencies": {
  3. "Microsoft.EntityFrameworkCore.Design": {
  4. "version": "1.1.0",
  5. "type": "build"
  6. }
  7. }
  8. }
  1. <ItemGroup>
  2. <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="1.1.0" PrivateAssets="All" />
  3. </ItemGroup>

type: platform

  1. {
  2. "dependencies": {
  3. "Microsoft.NETCore.App": {
  4. "version": "1.1.0",
  5. "type": "platform"
  6. }
  7. }
  8. }

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

  1. <TargetFramework>netcoreapp1.1</TargetFramework>

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

独立发布定义 (runtimes)

  1. {
  2. "runtimes": {
  3. "win7-x64": {},
  4. "osx.10.11-x64": {},
  5. "ubuntu.16.04-x64": {}
  6. }
  7. }
  1. <PropertyGroup>
  2. <RuntimeIdentifiers>win7-x64;osx.10-11-x64;ubuntu.16.04-x64</RuntimeIdentifiers>
  3. </PropertyGroup>

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

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

DOTNET CLI工具 (tools)

  1. {
  2. "tools": {
  3. "Microsoft.EntityFrameworkCore.Tools.DotNet": "1.0.0-*"
  4. }
  5. }
  1. <ItemGroup>
  2. <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.0.0" />
  3. </ItemGroup>

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

编译可执行 (emitEntryPoint)

  1. {
  2. "buildOptions": {
  3. "emitEntryPoint": true
  4. }
  5. }
  1. <PropertyGroup>
  2. <OutputType>Exe</OutputType>
  3. </PropertyGroup>
  1. {
  2. "buildOptions": {
  3. "emitEntryPoint": false
  4. }
  5. }
  1. <PropertyGroup>
  2. <OutputType>Library</OutputType>
  3. </PropertyGroup>

程序集强命名签名 (keyFile)

  1. {
  2. "buildOptions": {
  3. "keyFile": "MyKey.snk"
  4. }
  5. }
  1. <PropertyGroup>
  2. <AssemblyOriginatorKeyFile>MyKey.snk</AssemblyOriginatorKeyFile>
  3. <SignAssembly>true</SignAssembly>
  4. <PublicSign Condition="'$(OS)' != 'Windows_NT'">true</PublicSign>
  5. </PropertyGroup>

其它编译设置

  1. {
  2. "buildOptions": {
  3. "warningsAsErrors": true,
  4. "nowarn": ["CS0168", "CS0219"],
  5. "xmlDoc": true,
  6. "preserveCompilationContext": true,
  7. "outputName": "Different.AssemblyName",
  8. "debugType": "portable",
  9. "allowUnsafe": true,
  10. "define": ["TEST", "OTHERCONDITION"]
  11. }
  12. }
  1. <PropertyGroup>
  2. <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
  3. <NoWarn>$(NoWarn);CS0168;CS0219</NoWarn>
  4. <GenerateDocumentationFile>true</GenerateDocumentationFile>
  5. <PreserveCompliationContext>true</PreserveCompliationContext>
  6. <AssemblyName>Different.AssemblyName</AssemblyName>
  7. <DebugType>portable</DebugType>
  8. <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
  9. <DefineConstants>$(DefineConstants);TEST;OTHERCONDITION</DefineConstants>
  10. </PropertyGroup>

打包设置 (packOptions)

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

MsBuild脚本

  1. {
  2. "scripts": {
  3. "precompile": "generateCode.cmd",
  4. "postpublish": [ "obfuscate.cmd", "removeTempFiles.cmd" ]
  5. }
  6. }
  1. <Target Name="MyPreCompileTarget" BeforeTargets="Build">
  2. <Exec Command="generateCode.cmd" />
  3. </Target>
  4. <Target Name="MyPostCompileTarget" AfterTargets="Publish">
  5. <Exec Command="obfuscate.cmd" />
  6. <Exec Command="removeTempFiles.cmd" />
  7. </Target>

运行时设置 (runtimeOptions)

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

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

项目文件管理

  1. {
  2. "buildOptions": {
  3. "compile": {
  4. "copyToOutput": "notes.txt",
  5. "include": "../Shared/*.cs",
  6. "exclude": "../Shared/Not/*.cs"
  7. },
  8. "embed": {
  9. "include": "../Shared/*.resx"
  10. }
  11. },
  12. "packOptions": {
  13. "include": "Views/",
  14. "mappings": {
  15. "some/path/in/project.txt": "in/package.txt"
  16. }
  17. },
  18. "publishOptions": {
  19. "include": [
  20. "files/",
  21. "publishnotes.txt"
  22. ]
  23. }
  24. }
  1. <ItemGroup>
  2. <Compile Include="..\Shared\*.cs" Exclude="..\Shared\Not\*.cs" />
  3. <EmbeddedResource Include="..\Shared\*.resx" />
  4. <Content Include="Views\**\*" PackagePath="%(Identity)" />
  5. <None Include="some/path/in/project.txt" Pack="true" PackagePath="in/package.txt" />
  6. <None Include="notes.txt" CopyToOutputDirectory="Always" />
  7. <!-- CopyToOutputDirectory = { Always, PreserveNewest, Never } -->
  8. <Content Include="files\**\*" CopyToPublishDirectory="PreserveNewest" />
  9. <None Include="publishnotes.txt" CopyToPublishDirectory="Always" />
  10. <!-- CopyToPublishDirectory = { Always, PreserveNewest, Never } -->
  11. <!-- you can set both copy output and publish directories-->
  12. <None Include="testasset.txt" CopyToOutputDirectory="Always" CopyToPublishDirectory="Always" />
  13. <!-- alternatively, use nested XML attributes. They're functionally the same-->
  14. <None Include="testasset2.txt">
  15. <CopyToOutputDirectory>Always</CopyToOutputDirectory>
  16. <CopyToPublishDirectory>Always</CopyToPublishDirectory>
  17. </None>
  18. </ItemGroup>

单元测试

xunit

  1. {
  2. "testRunner": "xunit",
  3. "dependencies": {
  4. "dotnet-test-xunit": "<any>"
  5. }
  6. }
  1. <ItemGroup>
  2. <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
  3. <PackageReference Include="xunit" Version="2.2.0" />
  4. <PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
  5. </ItemGroup>

mstest

  1. {
  2. "testRunner": "mstest",
  3. "dependencies": {
  4. "dotnet-test-mstest": "<any>"
  5. }
  6. }
  1. <ItemGroup>
  2. <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
  3. <PackageReference Include="MSTest.TestAdapter" Version="1.1.12" />
  4. <PackageReference Include="MSTest.TestFramework" Version="1.1.11" />
  5. </ItemGroup>

三、结语

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

.NET Core 开源学习群:214741894

GitHub:https://github.com/maxzhang1985/YOYOFx 如果觉还可以请Star下, 欢迎一起交流。

.Net Core迁移到MSBuild平台(二)的更多相关文章

  1. Net Core迁移到MSBuild

    Net Core迁移到MSBuild平台(二)   阅读目录 一.前言 二.XML定义 三.结语 回到目录 一.前言 在上一篇文章.Net Core迁移到MSBuild的多平台编译问题中,简单的讲了下 ...

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

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

  3. .net core迁移实践:项目文件csproj的转换

    随着net core的不断更新和生产可用,越来越多的人把现有的应用迁移和部署到net core平台.本文将分享迁移过程中的一个环节,给大家做一下参考. 背景说明 先来介绍一下什么是SDK样式的文件结构 ...

  4. 内存中 OLTP - 常见的工作负荷模式和迁移注意事项(二)

    ----------------------------我是分割线------------------------------- 本文翻译自微软白皮书<In-Memory OLTP – Comm ...

  5. EntityFramework Core 运行dotnet ef命令迁移背后本质是什么?(EF Core迁移原理)

    前言 终于踏出第一步探索EF Core原理和本质,过程虽然比较漫长且枯燥乏味还得反复论证,其中滋味自知,EF Core的强大想必不用我再过多废话,有时候我们是否思考过背后到底做了些什么,到底怎么实现的 ...

  6. .net core 的图片处理及二维码的生成及解析

    写代码这事,掐指算来已经十有余年. 从html到css到javascript到vbscript到c#,从兴趣到职业,生活总是失落与惊喜并存. 绝大部分时候,出发并不是因为知道该到哪里去,只是知道不能再 ...

  7. NET Core迁移

      向ASP.NET Core迁移   有人说.NET在国内的氛围越来越不行了,看博客园文章的浏览量也起不来.是不是要转Java呢? 没有必要扯起语言的纷争,Java也好C#都只是语言是工具,各有各的 ...

  8. Cookies 初识 Dotnetspider EF 6.x、EF Core实现dynamic动态查询和EF Core注入多个上下文实例池你知道有什么问题? EntityFramework Core 运行dotnet ef命令迁移背后本质是什么?(EF Core迁移原理)

    Cookies   1.创建HttpCookies Cookie=new HttpCookies("CookieName");2.添加内容Cookie.Values.Add(&qu ...

  9. 大众点评网王宏:从.Net迁移向Java平台 - 51CTO.COM

    大众点评网王宏:从.Net迁移向Java平台 - 51CTO.COM 大众点评网王宏:从.Net迁移向Java平台

随机推荐

  1. doubango(3)--协议栈的启动过程

    协议栈启动的上层接口 对于Doubango中得sip协议栈,是通过SipStack类粘合上层代码与底层代码的,该类定义在SipStack.h中,实现在SipStack.cxx中.当构造好一个SipSt ...

  2. Word常用实用知识1

    Word常用实用知识1 纯手打,可能有错别字,使用的版本是office Word 2013 转载请注明出处,谢谢. 快速输入日期(含格式) [插入]--[日期]   快速输入日期和时间(快捷键) 快速 ...

  3. 定时任务管理中心(dubbo+spring)-我们到底能走多远系列47

    我们到底能走多远系列47 扯淡: 又是一年新年时,不知道上一年你付出了多少,收获了多少呢?也许你正想着老板会发多少奖金,也许你正想着明年去哪家公司投靠. 这个时间点好好整理一下,思考总结一下,的确是个 ...

  4. Div.2 C. Dasha and Password

    C. Dasha and Password time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  5. canvas绘图详解-06-绘制一个五角星-常用绘图原理

    先将如何画一个正规的五角星 在五角星的内外画两个圆,五角星有五个角,360/5=72度 所以得出这两个角的度数 然后算出这两个点坐标 角度转弧度 角度/180*Math.PI 所以外顶点坐标 x:   ...

  6. HDU 2080 夹角有多大II

    夹角有多大II Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  7. Salesforce删除数据时出现Insufficient privileges的可能原因

    遇到一个诡异的情况,用户通过界面删除一条自定义Object的数据的时候出现了Insufficient privileges.按理说,如果用户的Profile没有此Object的删除权限的话,应该连删除 ...

  8. ArcGIS制图表达Representation-制图表达原理

    ArcGIS制图表达技术-制图表达原理 by 李远祥 在讲述原理之前,需要对上一章内容进行一些必要的补充说明.既然制图表达有很多优势,是不是什么情况下都可以使用制图表达技术呢?如果有以下的一些特殊的要 ...

  9. 使用C#读写ini配置文件

    INI就是扩展名为"INI"的文件,其实他本身是个文本文件,可以用记事本打工,主要存放的是用户所做的选择或系统的各种参数. INI文件其实并不是普通的文本文件.它有自己的结构.由若 ...

  10. 分布式缓存技术memcached学习系列(四)—— 一致性hash算法原理

    分布式一致性hash算法简介 当你看到"分布式一致性hash算法"这个词时,第一时间可能会问,什么是分布式,什么是一致性,hash又是什么.在分析分布式一致性hash算法原理之前, ...