本篇博客來了解一下dotnet這個神奇的命令。我會依次對dotnet,dotnet new,dotnet restore,dotnet build,dotnet test,dotnet run,dotnet pack,dotnet publish這些個命令的用法作個簡單的介紹以及演示。web
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
dotnet new命令用來建立一個.net core項目,該命令包含兩個選項,分別是-t(或–type)和-l(或-lang),用來指定項目類型和編程語言。json
-l, –lang [C#|F#]
-l的默認值是c#,也能夠設置爲f#。VB的話應該很快就能支持了。c#
-t, –type [console|web|lib|xunittest]
-t的默認值是concole,控制檯項目。其它幾個參數分別表明web項目,類庫項目和測試項目。例如:dotnet new命令會建立一個控制檯項目,dotnet new -t web會建立一個web項目(asp.NET mvc)。bash
dotnet restore [–source] [–packages] [–disable-parallel] [–fallbacksource] [–configfile] [–verbosity] [] mvc
dotnet restore命令經過NuGet來下載定義在project.json文件中的依賴,而後放到用戶目錄下的.nuget/packages文件夾中。默認狀況下,下載依賴組建的過程是並行進行的。
–packages選項能夠指定存放已下載組件的位置,默認是用戶目錄下的.nuget/packages文件夾。
–disable-parallel選項用來禁用並行下載。
–configfile選項用來指定使用哪一個NuGet.config文件。
dotnet restore命令運行完畢後,會生成project.lock.json文件。app
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
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」指明瞭須要的單元測試運行器。單元測試
dotnet run [–framework] [–configuration] [–project] [–help] [–]
dotnet run命令是一個比較便捷的運行代碼的命令。它會編譯代碼,輸出信息,並運行代碼。
dotnet pack [–output] [–no-build] [–build-base-path] [–configuration] [–version-suffix] []
dotnet pack命令編譯代碼並生成一個NuGet包,具體來講就是在bin\Debug目錄下生成一個.nupkg文件和一個.symbols.nupkg文件。
dotnet publish [–framework] [–runtime] [–build-base-path] [–output] [–version-suffix] [–configuration] [] dotnet publish命令會編譯代碼,而後讀取project.json文件中定義的全部依賴組件,最後將這些東西輸出到一個文件夾中。生成的文件默認會輸出到\bin\Debug\netcoreapp1.0\publish中,你能夠經過-o或–output選項來修改輸出位置。當你須要發佈你的代碼時,該命令的輸出文件將是你所須要的所有文件。