Include the version of your package in your source code.
Standalone CLI
Run build_version directly with Dart 3.12+ without adding build_runner to
your dev_dependencies.
-
Make sure
pubspec.yamlhas aversionfield:name: my_pkg version: 1.2.3 -
Generate
lib/src/version.dart:$ dart run build_version@lib/src/version.dartwill 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'; -
Verify in CI that
lib/src/version.dartmatchespubspec.yamlwithout 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
-
Add
build_versionandbuild_runnertopubspec.yaml. Also make sure there is aversionfield.name: my_pkg version: 1.2.3 dev_dependencies: build_runner: ^2.10.0 build_version: ^2.2.0 -
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.