Loop transform iterates over elements in a list and applies the provided transform for every element in that list.
doAsMerge is used, a copy of the
Loop transform’s input is passed to each transform and the outputs from each transform are merged using a set union.
doAsChain is used, each transform is executed sequentially, receiving the previous transform’s output as its input. The first transform is to receive the
Loop transform’s input as its input.
type: Loop on: <SpEL expression> var: <string> index: <string> doAsChain: <transform> doAsMerge: <transform>
onmust be a SpEL expression that evaluates a list. This is the list of elements to be iterated over.
varis the name of the variable to be assigned to the current element on each iteration. (optional)
indexis the variable’s name to be assigned to the index of the current element on each iteration. (optional)
doAsMergeis the transform to be executed for every element in the list, on a copy of the
doAsChainis the transform to be executed for every element in the list, passing the output of the transform as input to the next transform.
index are optional.
Only one of the
doAsChain variables is to be used in a
Consider the following when choosing
doAsMerge executes the transform on the same input files for every iteration and merges the resulting outputs. It is best suited when a transform is executed multiple times on the same input and does not have conflicts.
doAsChain executes the transform on the initial input files once and then passes the resulting output to the second iteration and so on. It is best suited when a transform must detect any changes that occurred in the previous iteration.
Create a new directory for every module in
modules (a list of strings) based on the contents of the “template” directory.
type: Loop on: "#modules" var: m doAsMerge: type: RewritePath regex: "template/(.*)" rewriteTo: "#m + '/' + #g1"
Add every artifactId in
artifacts (a list of strings) as a Spring plug-in.
type: Loop on: "#artifacts" var: a doAsChain: type: OpenRewriteRecipe recipe: org.openrewrite.maven.AddPlugin options: groupId: "'org.springframework'" artifactId: "#a" version: "'5.7.1'"
You can use
Loop in combination with custom types, for example:
accelerator: types: - name: MavenPlugin struct: - name: groupId - name: artifactId - name: version options: - name: pluginsToAdd dataType: [MavenPlugin] # End users will be able to enter a collection of GAV tuples engine: include: [pom.xml] chain: - type: Loop on: pluginsToAdd # Iterate on the pluginsToAdd collection var: p # The variable "p" will contain each tuple in turn doAsChain: # Will apply the second execution to the result of the first, and so on... type: OpenRewriteRecipe recipe: org.openrewrite.maven.AddPlugin options: groupId: "#p['groupId']" artifactId: "#p['artifactId']" version: "#p['version']"
For more information, see Using Custom Types.