format ';' STRING (';' ANY )*
Macro
Summary
Format a string using printf-style format specifiers with provided arguments, similar to Java’s String.format().
Syntax
${format;<format-string>[;<argument>...]}
Parameters
- format-string: A format string with printf-style format specifiers (e.g.,
%s,%d,%f,%tF) - argument…: Zero or more arguments to substitute into the format string
Behavior
The macro:
- Processes the format string using Java’s
Formattersyntax - Uses
Locale.ROOTfor build reproducibility - Automatically converts string arguments to appropriate types based on format specifiers
- Expands arguments that look like macros (starting with
${) - Supports conditional formatting with
%{<condition>;<true>;<false>}
Conditional Syntax
%{<condition>;<true-value>;<false-value>}
Where <condition> is evaluated as truthy (non-empty and not “false”).
Examples
# Basic string formatting
${format;Bundle-Version %s;1.0.0}
# Returns: Bundle-Version 1.0.0
# Multiple arguments
${format;%s-%s-%s;com;example;bundle}
# Returns: com-example-bundle
# Number formatting
${format;Port number: %d;8080}
# Returns: Port number: 8080
# Decimal formatting
${format;Progress: %.2f%%;75.5}
# Returns: Progress: 75.50%
# Date/time formatting
${format;%tF;${tstamp}}
# Returns: 2024-01-15 (ISO date format)
${format;%tY-%tm-%td;${tstamp}}
# Returns: 2024-01-15 (custom date format)
# Conditional formatting
${format;%{${debug}};DEBUG-MODE;RELEASE-MODE}
# Returns: DEBUG-MODE if ${debug} is set, otherwise RELEASE-MODE
# Width and padding
${format;ID: %05d;42}
# Returns: ID: 00042
# Combining with other macros
${format;Built on %tF at %tT;${now};${now}}
# Returns: Built on 2024-01-15 at 14:30:45
Format Specifiers
Common format specifiers include:
%s- String%d- Decimal integer%f- Floating point%x- Hexadecimal%o- Octal%b- Boolean%tF- ISO date (yyyy-MM-dd)%tT- Time (HH:mm:ss)%tY,%tm,%td- Year, month, day components
See Java’s Formatter documentation for complete format specifier syntax.
Use Cases
- Version Strings: Create formatted version identifiers
- Manifest Headers: Generate complex manifest header values
- Date/Time Stamps: Format timestamps in various formats
- Numeric IDs: Format numbers with padding or specific precision
- Conditional Text: Generate different text based on build properties
- Reproducible Builds: Format values consistently using Locale.ROOT
Notes
- Uses
Locale.ROOTfor consistent, reproducible formatting across environments - Follows Java’s
String.format()andFormattersyntax - Arguments are automatically converted to appropriate types
- Arguments that start with
${are expanded as macros first - Conditional syntax
%{...}is bnd-specific (not standard Java) - For date/time formatting, combine with tstamp or now
Related Macros
- tstamp - Get timestamp in milliseconds
- now - Get current date/time
- long2date - Convert timestamp to formatted date
- if - Conditional macro (simpler alternative for boolean conditions)
See test cases in MacroTestsForDocsExamples.java
