目录

Contents

pub get 命令

Get 命令是 Pub 工具 中的一个命令。

Get is one of the commands of the pub tool.

$ pub get [--offline]

该命令获取所有在当前工作目录下的 pubspec.yaml 文件中列出的依赖项,以及这些依赖项的 间接依赖项。例如:

This command gets all the dependencies listed in the pubspec.yaml file in the current working directory, as well as their transitive dependencies. For example:

$ pub get
Got dependencies!

如果 Pub 的系统缓存 中还没有该依赖项,则 pub get 命令会在必要时下载该依赖项并更新缓存。该命令会创建一个 .packages 文件并将之映射到 Pub 的系统缓存中。

If the system cache doesn’t already contain the dependencies, pub get updates the cache, downloading dependencies if necessary. To map packages back to the system cache, this command creates a .packages file.

一旦依赖获取完毕,就可以在 Dart 代码中引用。例如,假如 Package 依赖了名为 test 的 Package,则可以使用该 Package 下的资源:

Once the dependencies are acquired, they may be referenced in Dart code. For example, if a package depends on test:

import 'package:test/test.dart';

pub get 命令获取新依赖项后会写入一个 lockfile 文件以确保下次执行该命令时会使用相同的依赖项版本。应用型的 Package 应该总是签入该 lockfile 文件以控制来源;从而确保在将 Package 部署到生产环境时所有的依赖项对于所有开发者而言都是相同的版本。库类型的 Package 则不需要签入 lockfile 文件,因为它们可能需要使用到不同的依赖项版本。

When pub get gets new dependencies, it writes a lockfile to ensure that future gets will use the same versions of those dependencies. Application packages should check in the lockfile to source control; this ensures the application will use the exact same versions of all dependencies for all developers and when deployed to production. Library packages should not check in the lockfile, though, since they’re expected to work with a range of dependency versions.

如果 lockfile 已经存在,pub get 命令会尽可能地使用锁定的依赖项版本。如果某个依赖项没有被锁定,则 Pub 会获取所有 限定的版本 中最新的那个依赖项版本。这是 pub get 命令与 pub upgrade 命令最大的不同点,后者总是会去尝试使用依赖项的最新版本。

If a lockfile already exists, pub get uses the versions of dependencies locked in it if possible. If a dependency isn’t locked, pub gets the latest version of that dependency that satisfies all the version constraints. This is the primary difference between pub get and pub upgrade, which always tries to get the latest versions of all dependencies.

Package 解析

Package resolution

默认情况下,Pub 会创建一个 .packages 文件用于映射 Package 名到位置 URI。在创建 .packages 文件之前,Pub 常常还会创建一个 packages 目录。

By default, pub creates a .packages file that maps from package names to location URIs. Before the .packages file, pub used to create packages directories.

更多信息请查阅 Package 文件规范建议

For more information, see the package specification file proposal.

获取一个新的依赖项

Getting a new dependency

如果在执行 pub get 命令前将某个依赖添加至 pubspec 文件中,则在执行该命令后会将该新的依赖项以及其间接依赖的其它依赖项下载并将其更新映射到 .packages 文件中。但是,Pub 不会更改哪些已经存在的依赖项除非有必要获取它们的新版本。

If a dependency is added to the pubspec and then pub get is run, it gets the new dependency and any of its transitive dependencies and updates the mapping in the .packages file. However, pub won’t change the versions of any already-acquired dependencies unless that’s necessary to get the new dependency.

移除一个依赖项

Removing a dependency

如果在 pub get 命令前从 pubspec 文件移除了某个依赖项,则在执行该命令后会将该依赖项从 .packages 文件中移除,且代码使用到该依赖项的相关导入将变得不可用。所有该依赖项依赖的间接依赖项也同时会被移除,只要这些间接依赖项没有没其它的依赖项所依赖。移除某个依赖项不会对已经获得的依赖项版本产生任何影响。

If a dependency is removed from the pubspec and then pub get is run, it removes the dependency from the .packages file, making the dependency unavailable for importing. Any transitive dependencies of the removed dependency are also removed, as long as no remaining immediate dependencies also depend on them. Removing a dependency never changes the versions of any already-acquired dependencies.

系统 Package 缓存

The system package cache

依赖项通过网络从类似 Git 仓库和 Pub 网站 下载并存储在一个 系统级的缓存 中。这意味着如果多个 Package 使用了相同依赖项的相同版本,它就不再需要通过网络下载,而仅仅只需从本地缓存获取即可。

Dependencies downloaded over the internet, such as those from Git and the pub.dev site, are stored in a system-wide cache. This means that if multiple packages use the same version of the same dependency, it only needs to be downloaded and stored locally once.

默认情况下,Pub 缓存存储在你的用户目录(Mac 和 Linux)或 %APPDATA%\Pub\Cache 目录(Windows,不同版本的 Windows 操作系统可能会不一样)下的 .pub-cache 子目录中。你可以在运行 Pub 相关命令前通过 PUB_CACHE 系统环境变量配置你想要的缓存存储目录。

By default, the system package cache is located in the .pub-cache subdirectory of your home directory (on Mac and Linux), or in %APPDATA%\Pub\Cache (on Windows; the location might vary depending on the Windows version). You can configure the location of the cache by setting the PUB_CACHE environment variable before running pub.

离线检索

Getting while offline

在没有网络的情况下你也依然可以运行 pub get 命令。因为 Pub 会将 Package 下载到一个统一的缓存区并将其与系统上其它的 Package 进行共享,如果你所需的 Package 是一个使用频率很高的 Package,那么很有可能它已经被其它 Package 在使用时下载到统一缓存区中了,此时你可以直接依赖使用它。

If you don’t have network access, you can still run pub get. Because pub downloads packages to a central cache shared by all packages on your system, it can often find previously downloaded packages without needing to use the network.

但是,默认情况下,pub get 命令会总是尝试获取线上的依赖版本,因此 Pub 可以确定依赖项是否有更新的版本。如果你不想 Pub 去线上检查,可以使用 --offline 命令参数让该命令在离线模式下执行。在离线模式下,Pub 只会从本地缓存区查找已经下载到的可用 Package。

However, by default, pub get tries to go online if you have any hosted dependencies, so that pub can detect newer versions of dependencies. If you don’t want pub to do that, pass it the --offline flag. In offline mode, pub looks only in your local package cache, trying to find a set of versions that work with your package from what’s already available.

切记 Pub 会生成一个 lockfile 文件。如果缓存中某些依赖项目有且只有一个版本且非常旧,离线模式下执行 pub get 命令则依然会使用那些旧的版本。下次当你有可用的网络时,可以使用 pub upgrade 命令将其更新到最新版本。

Keep in mind that pub generates a lockfile. If the only version of some dependency in your cache happens to be old, offline pub get locks your app to that old version. The next time you are online, you will likely want to run pub upgrade to upgrade to a later version.

选项

Options

pub get 命令支持我们上面所说的 --offline 命令行参数。

The pub get command supports the --offline command-line argument, as discussed above.

你可以查阅 全局选项 获取 Pub 命令所支持的命令选项。

For options that apply to all pub commands, see Global options.