1. 程式人生 > 實用技巧 >dart裡實現類似Java裡的--classpath的功能

dart裡實現類似Java裡的--classpath的功能

dart裡目前不清楚可不可以像Java一樣指定一個classpath option(看了下dart --help好像--packages很像,但是可能是我path格式有問題,反正沒成功),但是可以在dart專案根目錄裡新增 .dart_tool 目錄,然後在裡面新增 package_config.json 檔案,

然後檔案裡寫這樣的配置即可實現將其他地方的庫作為此專案的依賴庫:


{
"configVersion": 2,
"packages": [
{
"name": "dart_console",
"rootUri": "file:///home/silentdoer/.pub-cache/hosted/mirrors.tuna.tsinghua.edu.cn%2547dart-pub/dart_console-0.6.2",
"packageUri": "lib/",
"languageVersion": "2.7"
},
{
"name": "ffi",
"rootUri": "file:///home/silentdoer/.pub-cache/hosted/mirrors.tuna.tsinghua.edu.cn%2547dart-pub/ffi-0.1.3",
"packageUri": "lib/",
"languageVersion": "2.6"
},
{
"name": "win32",
"rootUri": "file:///home/silentdoer/.pub-cache/hosted/mirrors.tuna.tsinghua.edu.cn%2547dart-pub/win32-1.6.10",
"packageUri": "lib/",
"languageVersion": "2.7"
}
]
}

即,這裡用到了三個依賴庫(注意,包括遞迴依賴);

不過它還可以進一步精簡:

{
  "configVersion": 2,
  "packages": [
    {
      "name": "dart_console",
      "rootUri": "file:///home/silentdoer/.pub-cache/hosted/mirrors.tuna.tsinghua.edu.cn%2547dart-pub/dart_console-0.6.2",
      
"packageUri": "lib/" }, { "name": "ffi", "rootUri": "file:///home/silentdoer/.pub-cache/hosted/mirrors.tuna.tsinghua.edu.cn%2547dart-pub/ffi-0.1.3", "packageUri": "lib/" }, { "name": "win32", "rootUri": "file:///home/silentdoer/.pub-cache/hosted/mirrors.tuna.tsinghua.edu.cn%2547dart-pub/win32-1.6.10", "packageUri": "lib/" } ] }

而,這裡比如dart_console的目錄結構(其實就是一個git專案)是這樣的:

analysis_options.yaml example LICENSE README.md
CHANGELOG.md lib pubspec.yaml test
它們在dart_console-0.6.2目錄裡;(有空可以測試下是否只需要裡面的lib目錄即可?)