目录

Contents

Pub 工具

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

The pub tool has commands for managing packages and for deploying packages and command-line apps. For general information about using the pub package manager, see How to use packages.

pub 相关命令链接:

Quick links to the pub commands:

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

Pub’s commands fall into the following categories:

管理 Package 的依赖关系

Managing package dependencies

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

Pub provides a number of commands for managing the packages your code depends on.

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

In this group, the most commonly used commands are pub get and pub upgrade, which retrieve or upgrade dependencies used by a package. Every time you modify a pubspec file, run pub get to make sure the dependencies are up to date. Some IDEs perform this step automatically on the creation of a project, or any modification of the pubspec.

pub cache
Manages pub’s local package cache. Use this command to add packages to your cache, or to perform a clean reinstall of all packages in your 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 are listed as dependencies used by the current package. Used for testing the lower range of your package’s dependencies.

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

pub get
Retrieves the packages that are listed as the dependencies for the current package. If a pubspec.lock file already exists, fetches the version of 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 listed as dependencies used by the current package. If a pubspec.lock file exists, ignores the versions listed in the lock file and fetches the newest versions that honor the constraints in the pubspec. Creates or updates the lock file, as needed.

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

运行命令行应用

Running command-line apps

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

Two commands let you run Dart scripts from the command line:

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

    The pub run command invokes a Dart script in your package, or in one of its dependencies.

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

    The pub global command lets you work with globally available packages.

部署 Package 和应用

Deploying packages and apps

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

With pub you can publish packages and command-line apps.

Packages

Package

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

To share your Dart packages with the world, you can use the pub publish command to upload the package to the pub.dev site. The pub uploader command enables specific users to modify and upload new versions of your package.

命令行应用

Command-line apps

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 run pub global activate to make it directly available from the command line.

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

全局选项

Global options

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

Several command-line options work with all of the pub commands. These include:

--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