【先定一個小目標】dotnet core 命令詳解


本篇博客來了解一下dotnet這個神奇的命令。我會依次對dotnet,dotnet new,dotnet restore,dotnet build,dotnet test,dotnet run,dotnet pack,dotnet publish這些個命令的用法做個簡單的介紹以及演示。

1、基本的信息命令

dotnet命令主要是用來查看一些基本的信息,如平台、版本號等。經常會用到的參數有–version,–info,–help,下面依次運行下看看輸出

dotnet –version 
1.0.0-preview2-003121 
dotnet –info 
.NET Command Line Tools (1.0.0-preview2-003121)

Product Information: 
Version: 1.0.0-preview2-003121 
Commit SHA-1 hash: 1e9d529bc5

Runtime Environment: 
OS Name: Mac OS X 
OS Version: 10.11 
OS Platform: Darwin 
RID: osx.10.11-x64 
dotnet –help 
.NET Command Line Tools (1.0.0-preview2-003121) 
Usage: dotnet [host-options] [command] [arguments] [common-options]

Arguments: 
[command] The command to execute 
[arguments] Arguments to pass to the command 
[host-options] Options specific to dotnet (host) 
[common-options] Options common to all commands

Common options: 
-v|–verbose Enable verbose output 
-h|–help Show help

Host options (passed before the command): 
-v|–verbose Enable verbose output 
–version Display .NET CLI Version Number 
–info Display .NET CLI Info

Common Commands: 
new Initialize a basic .NET project 
restore Restore dependencies specified in the .NET project 
build Builds a .NET project 
publish Publishes a .NET project for deployment (including the runtime) 
run Compiles and immediately executes a .NET project 
test Runs unit tests using the test runner specified in the project 
pack Creates a NuGet package

2、dotnet new

dotnet new命令用來創建一個.net core項目,該命令包含兩個選項,分別是-t(或–type)和-l(或-lang),用來指定項目類型和編程語言。

-l, –lang [C#|F#] 
-l的默認值是c#,也可以設置為f#。VB的話應該很快就能支持了。

-t, –type [console|web|lib|xunittest] 
-t的默認值是concole,控制台項目。其它幾個參數分別代表web項目,類庫項目和測試項目。例如:dotnet new命令會創建一個控制台項目,dotnet new -t web會創建一個web項目(asp.NET mvc)。

3、dotnet restore

dotnet restore [–source] [–packages] [–disable-parallel] [–fallbacksource] [–configfile] [–verbosity] [] 

dotnet restore命令通過NuGet來下載定義在project.json文件中的依賴,然后放到用戶目錄下的.nuget/packages文件夾中。默認情況下,下載依賴組建的過程是並行進行的。 
–packages選項可以指定存放已下載組件的位置,默認是用戶目錄下的.nuget/packages文件夾。 
–disable-parallel選項用來禁用並行下載。 
–configfile選項用來指定使用哪個NuGet.config文件。 
dotnet restore命令運行完畢后,會生成project.lock.json文件。

4、dotnet build

dotnet build [–output] [–build-base-path] [–framework] [–configuration] [–runtime] [–version-suffix] 
[–build-profile] [–no-incremental] [–no-dependencies] [] 
dotnet build命令將項目中的所有源代碼文件和依賴的組件編譯為二進制的dll文件。該命令會讀取project.lock.json,如果項目中找不到該文件,則需要先運行dotnet restore命令。 
執行完dotnet build命令后,會在bin/Debug/netcoreapp1.0文件夾下生成.dll文件和.pbd文件等,例如:

bash-3.2$ ls -al bin/Debug/netcoreapp1.0/ 
total 80 
drwxr-xr-x 11 jingjing staff 374 9 6 22:08 . 
drwxr-xr-x 6 jingjing staff 204 9 6 21:52 .. 
-rw-r–r–@ 1 jingjing staff 6148 9 6 23:13 .DS_Store 
-rwxr–r– 1 jingjing staff 1636 9 5 22:38 app.deps.json 
-rwxr–r– 1 jingjing staff 4608 9 5 22:38 app.dll 
-rwxr–r– 1 jingjing staff 496 9 5 22:38 app.pdb 
-rwxr–r– 1 jingjing staff 107 9 5 22:38 app.runtimeconfig.dev.json 
-rwxr–r– 1 jingjing staff 118 9 5 22:38 app.runtimeconfig.json 
-rwxr–r– 1 jingjing staff 3584 9 5 22:05 library.dll 
-rwxr–r– 1 jingjing staff 452 9 5 22:05 library.pdb 
drwxr-xr-x 10 jingjing staff 340 9 6 22:08 publish

 

5、dotnet test

dotnet test [–configuration] [–output] [–build-base-path] [–framework] [–runtime] [–no-build] [–parentProcessId] 
[–port] [] 
dotnet test命令用來運行單元測試項目中的測試代碼,單元測試項目需要依賴一個單元測試框架(如nunit或xunit)以及對應的單元測試運行器,單元測試運行器在project.json文件中通過testRunner節點來指定。下面是一個使用xunit作為單元測試框架的項目的project.json文件

{
  "version": "1.0.0-*",
  "buildOptions": {
    "debugType": "portable"
  },
  "dependencies": {
    "System.Runtime.Serialization.Primitives": "4.1.1",
    "xunit": "2.1.0",
    "dotnet-test-xunit": "1.0.0-rc2-192208-24"
  },
  "testRunner": "xunit",
  "frameworks": {
    "netcoreapp1.0": {
      "dependencies": {
        "Microsoft.NETCore.App": {
          "type": "platform",
          "version": "1.0.0"
        }
      },
      "imports": [
        "dotnet5.4",
        "portable-net451+win8"
      ]
    }
  }
}

 

其中,”testRunner”: “xunit”指明了需要的單元測試運行器。

6、dotnet run

dotnet run [–framework] [–configuration] [–project] [–help] [–] 
dotnet run命令是一個比較便捷的運行代碼的命令。它會編譯代碼,輸出信息,並運行代碼。

7、dotnet pack

dotnet pack [–output] [–no-build] [–build-base-path] [–configuration] [–version-suffix] [] 
dotnet pack命令編譯代碼並生成一個NuGet包,具體來說就是在bin\Debug目錄下生成一個.nupkg文件和一個.symbols.nupkg文件。

8、dotnet publish

dotnet publish [–framework] [–runtime] [–build-base-path] [–output] [–version-suffix] [–configuration] [] 
dotnet publish命令會編譯代碼,然后讀取project.json文件中定義的所有依賴組件,最后將這些東西輸出到一個文件夾中。生成的文件默認會輸出到\bin\Debug\netcoreapp1.0\publish中,你可以通過-o或–output選項來修改輸出位置。當你需要發布你的代碼時,該命令的輸出文件將是你所需要的全部文件。


免責聲明!

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



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