build_version
2.2.1, released 5 weeks ago by kevmoo.com
Generate a Dart file with the package version at build time.
What it does
`build_version` extracts the `version` field from a pubspec and writes it to a Dart file. It can run as a standalone CLI or as a build_runner builder, supporting custom output paths and CI verification.
AI-written summary of version 2.2.1, based on the package's README and metadata. Report an inaccuracy
Use it when
- You need a compile‑time constant for the package version
- You already use build_runner for other generators
- You want CI to verify the generated file matches pubspec
Skip it when
- You only need the version at runtime and can read pubspec directly
- You prefer a pure Dart solution without code generation
Strengths
- Zero runtime overhead
- Supports both CLI and build_runner workflows
- Customizable output path
- CI friendly `--check` flag
Weaknesses
- Requires Dart 3.12+
- Adds a build step to the project
- Only handles the version field, not other metadata
Setup
- Kind
- Pure Dart
- Effort
- Low
- Code generation
- Yes (build_runner)
- Direct dependencies
- 4
Add `build_version` to dev_dependencies and run `dart run build_runner build` or use the CLI directly. No special permissions needed.
```dart
// lib/src/version.dart (generated)
const packageVersion = '1.2.3';
```Alternatives
Packages that do the same job and could replace build_version.
- code_builder
A fluent, builder-based library for generating valid Dart code programmatically.
93Compare - source_gen
Source code generation builders and utilities for the Dart build system.
84Compare - gql_code_builder
Dart code builder generating typed classes from GraphQL documents and SDL
78Compare - merging_builder
Merges multiple input files into a single output file during the build process.
70Compare
Works with
Packages commonly used together with build_version: ones it builds on, ones built on it, and its own family.
- pubspec_parsebuild_version depends on itParse pubspec.yaml files with a type‑safe API and detailed error reporting.
- argsbuild_version depends on itLibrary for parsing raw command-line arguments into options and values using GNU and POSIX styles.
- buildbuild_version depends on itPackage for authoring build_runner compatible code generators.
- build_configbuild_version depends on itFormat definition and support for parsing build.yaml configuration.
- build_runnerUsed togetherA build system for Dart code generation and modular compilation.
- build_verifySame familyUtility to verify generated Dart code is up-to-date during build
- build_cliSame familyParse command line arguments directly into an annotation class using build_runner or a standalone CLI.