CI Pub Package package publisher

Include the version of your package in your source code.

build_version runs either as a standalone CLI or as a build_runner builder.

Feature Standalone CLI (dart run build_version@) With build_runner
Best when build_version is your only code generator (or other generators also run standalone). Your package already runs build_runner for other builders.
dev_dependencies None required. build_version and build_runner (plus build_verify for tests).
Custom output path --output <path> (-o) CLI option. build.yaml (builders: build_version: options: output: <path>).
Generated header Includes dart run build_version@<version> rerun comment. Single-line // Generated code. Do not modify. header.
CI verification dart run build_version@<version> --check package:build_verify (expectBuildClean())

Standalone CLI

Run build_version directly with Dart 3.12+ without adding build_runner to your dev_dependencies.

  1. Make sure pubspec.yaml has a version field:

    name: my_pkg
    version: 1.2.3
    
  2. Generate lib/src/version.dart:

    $ dart run build_version@
    

    lib/src/version.dart will be generated with content:

    // GENERATED CODE - DO NOT MODIFY BY HAND
    // Generated by package:build_version 2.2.0.
    // To reproduce this exact version: dart run build_version@2.2.0
    // To update with the latest release: dart run build_version@
    const packageVersion = '1.2.3';
    
  3. Verify in CI that lib/src/version.dart matches pubspec.yaml without modifying files on disk:

    $ dart run build_version@ --check
    

To write or check a custom output path relative to the package root, pass --output (-o):

$ dart run build_version@ --output lib/src/custom/path/to/version.dart
$ dart run build_version@ --check --output lib/src/custom/path/to/version.dart

You can also add build_version to dev_dependencies and run dart run build_version.

With build_runner

  1. Add build_version and build_runner to pubspec.yaml. Also make sure there is a version field.

    name: my_pkg
    version: 1.2.3
    dev_dependencies:
      build_runner: ^2.10.0
      build_version: ^2.2.0
    
  2. Run a build.

    $ dart run build_runner build
    

To change the path of the generated file when using build_runner, create a build.yaml in the root of your package and set the output option of this builder:

targets:
  $default:
    builders:
      build_version:
        options:
          output: lib/src/custom/path/to/version.dart

Libraries

builder
Configuration for using package:build-compatible build systems.