The RewritePath transform allows changing the name and path of files without affecting their content.

Syntax Reference

type: RewritePath
regex: <string>
rewriteTo: <SpEL expression>
matchOrFail: <boolean>

For each input file, RewritePath will attempt to match its path using the regular expression defined by the regex property. It the regex matches, the path of the file is changed to the evaluation result of rewriteTo, which is an expression that has access to the overall engine model as well as variables defined by capturing groups of the regex (both named capturing groups (?<example>[a-z]*), and regular index-based capturing groups are supported. g0 contains the whole match, g1 the first capturing group, etc.)

If the regex doesn't match, then the behaviour depends on the matchOrFail property: - if set to false (the default), then the file is left untouched, - if set to true then an error occurs. This is to prevent misconfiguration if you expect that all files coming in will match the regex (read below about typical interaction between RewritePath and Chain + Include)

The default value for regex is the following regular expression, which provides convenient access to some named capturing groups:

^(?<folder>.*/)?(?<filename>([^/]+?|)(?=(?<ext>\.[^/.]*)?)$)

This captures the following (assume some/deep/nested/file.xml as an example) - folder: the full folder path the file is in (some/deep/nested/ in the example), - filename: the full name of the file (last segment), including extension if present (file.xml), - ext: the last dot and extension in the filename, if present (.xml).

The default value for rewriteTo is the expression #folder + #filename, which actually doesn't rewrite paths.

Examples

The following moves all files from src/main/java to sub-module/src/main/java:

type: RewritePath
regex: src/main/java/(.*)
rewriteTo: "'sub-module/src/main/java' + #g1"   # 'sub-module/' + #g0 works too

The following flattens all files found inside the sub-path directory (and its subdirectories) and puts them into the flattened folder:

type: RewritePath
regex: sub-path/(.*/)*(?<filename>[^/]+)
rewriteTo: "'flattened' + #filename"   # 'flattened' + #g2 would work too

The following turns all paths into lowercase:

type: RewritePath
rewriteTo: "#g0.toLowerCase()" 

Interaction with Chain and Include

It is very common to define pipelines that perform a Chain of transformations on some subset of files, typically selected via Include/Exclude:

- include: "**/*.java"
- chain:
    - # do something here
    - # and then here

If one of the transformations in the chain is a RewritePath operation, then chances are that you want the rewrite to happen to all files matched by the Include. For those typical configurations, the matchOrFail guard can be set to true to make sure that the regex you provide indeed matches all files coming in.

See Also

  • UniquePath can be used to make sure rewritten paths don't clash with other files, or to decide which one to pick if they do.
check-circle-line exclamation-circle-line close-line
Scroll to top icon