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/"
    }
  ]
}

还能再简化:

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

而且,rootUri可以用相对路径,注意,它相对的是package_config.json文件的位置

{
    "configVersion": 2,
    "packages": [
      {
        "name": "dart_console",
        "rootUri": "../../packages/dart_console-0.6.2",
        "packageUri": "lib/"
      },
      {
        "name": "ffi",
        "rootUri": "/home/silentdoer/.pub-cache/hosted/mirrors.tuna.tsinghua.edu.cn%2547dart-pub/ffi-0.1.3",
        "packageUri": "lib/"
      },
      {
        "name": "win32",
        "rootUri": "/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目录即可,即上面的目录除了lib目录都可以删掉)

这里还可以简写成这样:

{
    "configVersion": 2,
    "packages": [
      {
        "name": "dart_console",
        "rootUri": "../../packages/dart_console-0.6.2/lib/"
      },
      {
        "name": "ffi",
        "rootUri": "../../packages/ffi-0.1.3/lib"
      },
      {
        "name": "win32",
        "rootUri": "../../packages/win32-1.6.10/lib"
      }
    ]
  }