This topic tells you about the Application Accelerator RewritePath
transform in Tanzu Application Platform (commonly known as TAP).
The RewritePath
transform allows you to change the name and path of files without affecting their content.
RewritePath(regex: REGEX, rewriteTo: SPEL-EXPRESSION, matchOrFail:BOOLEAN)
Where:
REGEX
is a regular expression (regex). For each input file, RewritePath
attempts to match its path
by using this regex. If the regex matches, RewritePath
changes the path
of the file to the evaluation result of rewriteTo
.
SPEL-EXPRESSION
is a SpEL expression that has access to the overall engine model and to variables defined by capturing groups of the regular expression. Both named capturing groups (?<example>[a-z]*)
and regular index-based capturing groups are supported. g0
contains the whole match, g1
contains the first capturing group, and so on.
BOOLEAN
is the behavior you want if the regex doesn’t match:
false
, which is the default, the file is left untouched.true
, an error occurs. This prevents misconfiguration if you expect all files coming in to match the regex. For more information about typical interactions between RewritePath
and Chain + Include
, see the following section, Interaction with Chain and Include.The default values for RewritePath
are as follows:
regex
: The default is the following regular expression, which provides convenient access to some named capturing groups:
^(?<folder>.*/)?(?<filename>([^/]+?|)(?=(?<ext>\.[^/.]*)?)$)
Using some/deep/nested/file.xml
as an example, the default regular expression captures:
some/deep/nested/
.file.xml
..xml
.rewriteTo
: The default is the expression #folder + #filename
, which doesn’t rewrite paths.
matchOrFail
: The default is false
, which leaves the file untouched if the regex doesn’t match.
See the following examples using the RewritePath
transform.
The following moves all files from src/main/java
to sub-module/src/main/java
:
RewritePath(regex: "src/main/java/(.*)", rewriteTo: "sub-module/src/main/java" + #g1)
The following flattens all files found inside the sub-path
directory and its subdirectories, and puts them into the flattened
folder:
RewritePath(regex: "sub-path/(.*/)*(?<filename>[^/]+)", rewriteTo: "flattened" + #filename)
The following turns all paths into lowercase:
RewritePath(rewriteTo: #g0.toLowerCase())
It’s common to define pipelines that perform a Chain
of transformations on a subset of files, typically selected by Include/Exclude
:
Include({"**/*.java"})
T1()
T2()
T3()
If one of the transformations in the chain is a RewritePath
operation, chances are you want the rewrite to apply to all files matched by the Include
. For those typical configurations, you can set the matchOrFail
guard to true
to ensure the regex
you provide indeed matches all files coming in.