Append to text file
Recipe ID
org.openrewrite.text.AppendToTextFileArtifact
org.openrewrite:rewrite-coreAppends or replaces content of an existing plain text file, or creates a new one if it doesn't already exist. Please note that this recipes requires existing plain text files' format to be successfully parsable by OpenRewrite. If a file is left unchanged, it might be parsed as a Quark rather than plain text. In such case, use the plainTextMask option. See the Gradle or Maven plugin configuration page.
Usage
You’ll need the Moderne CLI configured before running the command below.
mod run . --recipe org.openrewrite.text.AppendToTextFile --recipe-option "relativeFileName=foo/bar/baz.txt" --recipe-option "content=Some text."If the recipe isn’t available locally, install it with:
mod config recipes jar install org.openrewrite:rewrite-core:8.88.0Options
| Name | Type | Description |
|---|---|---|
relativeFileNamerequired | String | File name, using a relative path. May also be a glob expression (e.g. **/*.txt) to match one or more existing files; when a glob is supplied no new file is created. If a non-plaintext file matches, that file is left unchanged.e.g. foo/bar/baz.txt |
contentrequired | String | Multiline text content to be appended to the file. e.g. Some text. |
preamble | String | If a new file is created, this content will be included at the beginning. e.g. # File generated by OpenRewrite # |
appendNewline | Boolean | Print a newline automatically after the content (and preamble). Default true. |
existingFileStrategy | Strategy | Determines behavior if a file exists at this location prior to Rewrite execution.
- Continue: append new content to existing file contents. If existing file is not plaintext, recipe does nothing.
- Replace: remove existing content from file.
- Leave: *(default)* do nothing. Existing file is fully preserved.
- Merge: append only lines from new content that are not already present in the file. Lines are compared line-by-line after trimming whitespace. Preserves existing line order.
Note: this only affects the first interaction with the specified file per Rewrite execution.
Subsequent instances of this recipe in the same Rewrite execution will always append. |
Data tables
Structured output this recipe can produce.
- Source files that had resultsSource files that were modified by the recipe run.
org.openrewrite.table.SourcesFileResults - Source files that had search resultsSearch results that were found during the recipe run.
org.openrewrite.table.SearchResults - Source files that errored on a recipeThe details of all errors produced by a recipe run.
org.openrewrite.table.SourcesFileErrors - Recipe performanceStatistics used in analyzing the performance of recipes.
org.openrewrite.table.RecipeRunStats