Pub 工具

pub 工具包含管理 Package 、部署 Package 和部署命令行应用的命令。有关如何使用 Pub Package 管理器的信息,请查阅如何使用 Package

[Flutter]

Flutter note: If you’re using the Flutter SDK, don’t use the pub command directly. Instead use the flutter pub command, as described in Using packages on the Flutter website.

pub 相关命令链接:

有疑问?请查阅 Pub 疑难协助.

Pub 的命令可以分为以下几类:

管理 Package 的依赖关系

Pub 提供了许多命令,这些命令用于管理那些你代码所依赖 Package

在这些命令中,最常用的是 pub get 命令和 pub upgrade 命令,前者用于检索 Package 的依赖项,后者用于更新 Package 的依赖项。每一次你修改了 pubspec 文件后都需要执行 pub get 命令来确保你所用的依赖项是最新的。一些 IDE 会在创建项目或修改了 pubspec 文件后自动执行此操作。

  • pub cache
  • Manages pub’s local package cache. Use this command to add packagesto your cache, or to perform a clean reinstall of all packages inyour cache.

pub cache 命令:用于管理 Pub 的本地 Package 缓存。使用该命令你可以将一个 Package 添加至缓存,或者清除所有缓存的 Package 并重新安装。

  • pub deps
  • Lists all dependencies used by the current package.

pub deps 命令:用于显示当前 Package 使用的所有依赖项。

  • pub downgrade
  • Retrieves the lowest versions of all the packages that arelisted as dependencies used by the current package. Used for testingthe lower range of your package’s dependencies.

pub downgrade 命令:用于检索当前 Package 所依赖的其它 Package 的最低版本。用于测试这些较低版本依赖项的 Package 在当前 Package 上的兼容性。

  • pub get
  • Retrieves the packages that are listed as the dependencies forthe current package.If a pubspec.lock file already exists, fetches the versionof each dependency (if possible) as listed in the lock file.Creates or updates the lock file, as needed.

pub get 命令:用于检索当前 Package 所依赖的其它 Package。如果 pubspec.lock 文件已经存在,则根据该文件中保存的依赖项版本获取对应的依赖项。如有必要,将会创建或更新该文件。

  • pub upgrade
  • Retrieves the latest version of each package listedas dependencies used by the current package. If a pubspec.lockfile exists, ignores the versions listed in the lock file and fetchesthe newest versions that honor the constraints in the pubspec.Creates or updates the lock file, as needed.

pub upgrade 命令:用于检索当前 Package 所依赖的其它 Package 的最新版本。如果 pubspec.lock 文件已经存在,则忽略其保存的版本并以 pubspec 文件中指定的最新版本为主。如有必要,将会创建或更新该文件。

运行命令行应用

你可以使用下面两个命令从命令行运行 Dart 脚本:

  • pub run 命令可以调用当前 Package 或当前 Package 所依赖的其它 Package 中的 Dart 脚本。

  • pub global 命令可以让你使用那些可以全局可用的 Package。

部署 Package 和应用

使用 pub 命令你还可以发布 Package 和命令行应用。

Note: Pub used to support building apps through commands like pub build and pub serve. That functionality is now in other tools such as the build system. For details, see Obsolete Pub Features.

Packages

Package

你可以使用 pub publish 命令将 Package 上传至 Pub 网站以分享给全世界的开发者使用。pub uploader 命令则可以允许指定用户修改 Package 和上传新版本的 Package。

命令行应用

For any package that contains scripts (anything under the bin/directory), consider adding the executables tag to the pubspec file.When a script is listed under executables, users can runpub global activateto make it directly available from the command line.

任何包含脚本(即在 bin/ 目录下有任意文件)的 Package,可以在 pubspec 文件中添加上 executables 标签。当一个脚本标识为 executables 时,用户可以直接从命令行使用 pub global activate 命令运行它。

全局选项

有几个命令行选线可以用于所有 pub 命令。它们包括:

  • —help or -h
  • Print usage information.

—help-h:显示使用说明。

  • —version
  • Print version of pub.

—version:显示当前 pub 命令的版本。

  • —trace
  • Print debugging information when an error occurs.

—trace:当出现错误时输出调试信息。

  • —verbosity=<level>
  • The specified level determines the amount of information that is displayed:

—verbosity=<level>:指定输出信息的级别:

    • all
    • Show all output, including internal tracing messages.
  • all:显示所有出输出,包括内部追踪信息。

    • io
    • Show I/O operations.
  • io:显示 I/O 操作。

    • normal
    • Show errors, warnings, and user messages.
  • normal:显示错误、警告以及用户信息。

    • solver
    • Show steps during version resolution.
  • solver:显示版本解析的步骤。
  • -verbose or -v
  • Equivalent to —verbosity=all.

-verbose-v:等同于 —verbosity=all