How to use Ant regular expressions to rename files on S3 uploads

Ant regular expressions offer powerful options to adjust the resulting object keys on S3 uploads, but they are not exactly intuitive to use. This article offers some examples for common use cases to get you started.

Step-by-step guide

Using Ant regular expressions on S3 uploads in itself is straight forward:

  1. Check the Use Ant regular expressions to rename files checkbox.

  2. Fill the appearing From Pattern and To Pattern fields.

    Screenshot of active Ant regular expressions usage

Common use cases

The tricky part can be defining the right patterns for the desired result, hence some common examples:

Rename a single file

This is the most basic operation, but comes with a caveat regarding the need to escape the '.' for literal matching in the from pattern:

From Pattern: some/local/path/filename\.jar

To Pattern: changedFilename.jar

Drop leading local path elements

(example from screenshot above)

When you select local files, their full path relative to the Bamboo working directory will become part of the resulting object key on upload, e.g. "some/local/path/file.txt" would result in "[optionalObjectKeyPrefix/]some/local/path/file.txt". In order to drop parts of the local path, you would use subexpression capturing groups via parentheses to capture only the part you are interested in:

From Pattern: some/local/path/(.*)

To Pattern: \1

Drop intermediate path elements

Assuming you have a nested directory structure as the source, but want to remove parts of that nesting (e.g. "a/b/c/someArtifact.jar" and "a/b/d/anotherArtifact.jar" and you want to drop the c and d subdirectories):

From Pattern: ^(path/to/).*?/(.*)

Note the 'non greedy' ? operator to prevent matching more than one directory level between the two!

To Pattern: \1\2

In case you need to use end of line matching in your from patterns, note that you need to escape a dollar-sign ($) with another dollar-sign in Ant!

For more complex examples, see the Ant regexp-mapper documentation.