【轉】.NET Core基於. csproj 配置文件發布項目


一、前言

  .NET工具鏈在最新的Preview3版本中,引入了新的MSBuild項目系統,項目文件又回歸了.csproj的XML文件來管理,項目文件、包引用、程序集引用、.NET Core工具集、發布內容定義等內容。本文主要將主要討論,如何在新的項目系統中(.csproj)發布可執行文件。我們都知道在之前的版本中,項目文件是通過project.json文件來管理項目和包引用的,那么通過刪除 dependencies->Microsoft.NETCore.App-> "type": "platform" 子節點,並定義runtimes節點,來發布可執行文件 。

  所為可執行文件就是在目標機器上,不需要安裝.NET Core SDK或任何Runtime,就可以執行的文件。比如在Windows上可以生成 coreapp.exe的可執行文件,而在Linux中可以使用 ./coreapp 來執行。

  原理上這種可執行文件,就是通過一個C++應用程序為載體(宿主),加載CoreCLR,通過CoreCLR再加載任意的程序集,對這里有興趣的朋友也可以到Github上去看一下CoreCLR中ClrHost的部分。

二、生成可執行

  在新的.csproj項目文件中,我們要想發布一個可執行文件,就在手動創建名為<RuntimeIdentifiers>的節點,在這個節點下面,添加RuntimeIdentifiers也就是以前的RID定義,RID是描述系統平台的統一命名標示。例如我想要發布的可執行文件的目標系統平台為Win10Mac os 10.11.* 定義如下:

 <PropertyGroup>
      <RuntimeIdentifiers>win10-x64;osx.10.11-x64</RuntimeIdentifiers>
 </PropertyGroup>

  通過如下命令發布各平台的目標可執行文件:

dotnet build -r win10-x64
dotnet build -r osx.10.11-x64

  上面的命令可以生成帶有符號文件和調試信息的DEBUG版本,你的應用程序將生成在.\bin\Debug\netcoreapp1.0\< runtime_identifier>目錄下,如果想生成生產環境的最終版本請通過如下命令獲取:

dotnet publish -c release -r win10-x64
dotnet publish -c release -r osx.10.11-x64

  通過上述命令生成的Release版本目標執行文件將生成在 .\bin\release\netcoreapp1.0\<runtime_identifier>目錄下,並且每一個目標平台目錄下都有生成的可執行文件、發布項目的程序集、.NET Core依賴或必要的文件來保證生成程序的獨立可執行。

  我們來看一個新的csproj文件的完整定義:

復制代碼
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" />
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp1.0</TargetFramework>
    <VersionPrefix>1.0.0</VersionPrefix>
    <DebugType>Portable</DebugType>
    <RuntimeIdentifiers>win10-x64;osx.10.11-x64</RuntimeIdentifiers>
  </PropertyGroup>
  <ItemGroup>
    <Compile Include="**\*.cs" />
    <EmbeddedResource Include="**\*.resx" />
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.NETCore.App">
      <Version>1.0.1</Version>
    </PackageReference>
    <PackageReference Include="Newtonsoft.Json">
      <Version>9.0.1</Version>
    </PackageReference>
    <PackageReference Include="Microsoft.NET.Sdk">
      <Version>1.0.0-alpha-20161102-2</Version>
      <PrivateAssets>All</PrivateAssets>
    </PackageReference>
  </ItemGroup>

  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
復制代碼

三、RID

  RID是Runtime Identifier的縮寫,它用於定義目標操作系統標示。RID會不斷的更新,我們可以在CoreFx項目中找到RID定義,常用的RID有如下:

  Windows RIDs

  • Windows 7 / Windows Server 2008 R2
    • win7-x64
    • win7-x86
  • Windows 8 / Windows Server 2012
    • win8-x64
    • win8-x86
    • win8-arm
  • Windows 8.1 / Windows Server 2012 R2
    • win81-x64
    • win81-x86
    • win81-arm
  • Windows 10 / Windows Server 2016
    • win10-x64
    • win10-x86
    • win10-arm
    • win10-arm64

  Linux RIDs

  • Red Hat Enterprise Linux
    • rhel.7.0-x64
    • rhel.7.1-x64
    • rhel.7.2-x64
  • Ubuntu
    • ubuntu.14.04-x64
    • ubuntu.14.10-x64
    • ubuntu.15.04-x64
    • ubuntu.15.10-x64
    • ubuntu.16.04-x64
    • ubuntu.16.10-x64
  • CentOS
    • centos.7-x64
  • Debian
    • debian.8-x64
  • Fedora
    • fedora.23-x64
    • fedora.24-x64
  • OpenSUSE
    • opensuse.13.2-x64
    • opensuse.42.1-x64
  • Oracle Linux
    • ol.7-x64
    • ol.7.0-x64
    • ol.7.1-x64
    • ol.7.2-x64
  • Currently supported Ubuntu derivatives
    • linuxmint.17-x64
    • linuxmint.17.1-x64
    • linuxmint.17.2-x64
    • linuxmint.17.3-x64
    • linuxmint.18-x64

  OS X RIDs

  • osx.10.10-x64
  • osx.10.11-x64
  • osx.10.12-x64
四、系統依賴

  發布出來的目標平台可執行文件,也是需要依賴系統特性的,接下來我們來看下系統的需要組件有哪些:

Windows Ubuntu CentOS

OS X

 
  • Visual C++ Redistributable 
  • for Visual Studio 2015
 
  • libunwind8
  • libunwind8-dev
  • gettext
  • libicu-dev
  • liblttng-ust-dev
  • libcurl4-openssl-dev
  • libssl-dev
  • uuid-dev
  • unzip
 
  • deltarpm
  • epel-release
  • unzip
  • libunwind
  • gettext
  • libcurl-devel
  • openssl-devel
  • zlib
  • libicu-devel

 

 

 

  • libssl version 1.0.1

 



 

原文鏈接:http://www.cnblogs.com/maxzhang1985/p/6136886.html#_labelTop


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM