Class ReplacerMojo
- java.lang.Object
-
- org.apache.maven.plugin.AbstractMojo
-
- com.google.code.maven_replacer_plugin.ReplacerMojo
-
- All Implemented Interfaces:
org.apache.maven.plugin.ContextEnabled
,org.apache.maven.plugin.Mojo
@Mojo(name="replace", defaultPhase=COMPILE, threadSafe=true) public class ReplacerMojo extends org.apache.maven.plugin.AbstractMojo
Goal replaces token with value inside file
-
-
Field Summary
Fields Modifier and Type Field Description private java.lang.String
basedir
Optional base directory for each file to replace.private boolean
commentsEnabled
Comments enabled in the tokenValueMapFile.private java.util.List<java.lang.String>
delimiters
Add a list of delimiters which are added on either side of tokens to match against.private java.lang.String
encoding
File encoding used when reading and writing files.private java.util.List<java.lang.String>
excludes
List of files to exclude for multiple (or single) replacement.private java.lang.String
file
File to check and replace tokens.private java.util.List<java.lang.String>
files
list filesprivate FileSelector
fileSelector
private java.lang.String
filesToExclude
List of comma separated files to exclude (must have some includes) for multiple (or single) replacement.private java.lang.String
filesToInclude
Comma separated list of includes.private FileUtils
fileUtils
private boolean
ignoreErrors
Ignore any errors produced by this plugin such as files not being found and continue with the build.private boolean
ignoreMissingFile
Ignore missing target file.private java.util.List<java.lang.String>
includes
List of files to include for multiple (or single) replacement.private java.lang.String
inputFilePattern
Regular expression is run on an input file's name to create the output file with.private static java.lang.String
INVALID_IGNORE_MISSING_FILE_MESSAGE
private java.lang.Integer
maxReplacements
Set a maximum number of files which can be replaced per execution.private java.lang.String
outputBasedir
Base directory (appended) to use for outputDir.private java.lang.String
outputDir
Output to another dir.private java.lang.String
outputFile
Output to another file.private OutputFilenameBuilder
outputFilenameBuilder
private java.lang.String
outputFilePattern
Regular expression groups from inputFilePattern are used in this pattern to create an output file per input file.private java.util.List<java.lang.String>
outputFiles
list out put fileprivate PatternFlagsFactory
patternFlagsFactory
private boolean
preserveDir
Parent directory is preserved when replacing files found from includes and being written to an outputDir.private ReplacementProcessor
processor
private boolean
quiet
Stops printing a summary of files that have had replacements performed upon them when true.private boolean
regex
Indicates if the token should be located with regular expressions.private static java.lang.String
REGEX_PATTERN_WITH_DELIMITERS_MESSAGE
private java.util.List<java.lang.String>
regexFlags
List of standard Java regular expression Pattern flags (see Java Doc).private java.util.List<Replacement>
replacements
List of replacements with token/value pairs.private ReplacerFactory
replacerFactory
private boolean
skip
Skip running this plugin.private SummaryBuilder
summaryBuilder
private java.lang.String
token
Token to replace.private java.lang.String
tokenFile
Token file containing a token to be replaced in the target file/s.private java.lang.String
tokenValueMap
Map of tokens and respective values to replace with.private TokenValueMapFactory
tokenValueMapFactory
private boolean
unescape
Unescape tokens and values to Java format.private java.lang.String
value
Value to replace token with.private java.lang.String
valueFile
A file containing a value to replace the given token with.private java.lang.String
variableTokenValueMap
Variable tokenValueMap.private java.lang.String
xpath
X-Path expression for locating node's whose content you wish to replace.
-
Constructor Summary
Constructors Constructor Description ReplacerMojo()
ReplacerMojo(FileUtils fileUtils, ReplacementProcessor processor, ReplacerFactory replacerFactory, TokenValueMapFactory tokenValueMapFactory, FileSelector fileSelector, PatternFlagsFactory patternFlagsFactory, OutputFilenameBuilder outputFilenameBuilder, SummaryBuilder summaryBuilder)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private void
addIncludesFilesAndExcludedFiles()
private void
addToList(java.util.List<java.lang.String> toAdds, java.util.List<java.lang.String> destination)
private java.util.List<DelimiterBuilder>
buildDelimiters()
private java.util.List<Replacement>
buildReplacements()
private boolean
checkFileExists()
void
execute()
java.lang.String
getBasedir()
private java.lang.String
getBaseDirPrefixedFilename(java.lang.String file)
private java.util.List<Replacement>
getDelimiterReplacements(java.util.List<Replacement> replacements)
java.util.List<java.lang.String>
getDelimiters()
java.util.List<java.lang.String>
getExcludes()
java.lang.String
getFile()
java.util.List<java.lang.String>
getFiles()
java.lang.String
getFilesToExclude()
java.lang.String
getFilesToInclude()
java.util.List<java.lang.String>
getIncludes()
java.lang.String
getInputFilePattern()
java.lang.String
getOutputBasedir()
java.lang.String
getOutputDir()
java.lang.String
getOutputFile()
java.lang.String
getOutputFilePattern()
java.util.List<java.lang.String>
getOutputFiles()
java.lang.String
getVariableTokenValueMap()
boolean
isCommentsEnabled()
boolean
isIgnoreErrors()
boolean
isPreserveDir()
boolean
isSkip()
boolean
isUnescape()
private <T> java.util.List<T>
limit(java.util.List<T> all)
private void
replaceContents(ReplacementProcessor processor, java.util.List<Replacement> replacements, java.lang.String inputFile)
private void
replaceContents(ReplacementProcessor processor, java.util.List<Replacement> replacements, java.util.List<java.lang.String> inputFiles)
void
setBasedir(java.lang.String baseDir)
void
setCommentsEnabled(boolean commentsEnabled)
void
setDelimiters(java.util.List<java.lang.String> delimiters)
void
setEncoding(java.lang.String encoding)
void
setExcludes(java.util.List<java.lang.String> excludes)
void
setFile(java.lang.String file)
void
setFiles(java.util.List<java.lang.String> files)
void
setFilesToExclude(java.lang.String filesToExclude)
void
setFilesToInclude(java.lang.String filesToInclude)
void
setIgnoreErrors(boolean ignoreErrors)
void
setIgnoreMissingFile(boolean ignoreMissingFile)
void
setIncludes(java.util.List<java.lang.String> includes)
void
setInputFilePattern(java.lang.String inputFilePattern)
void
setMaxReplacements(java.lang.Integer maxReplacements)
void
setOutputBasedir(java.lang.String outputBasedir)
void
setOutputDir(java.lang.String outputDir)
void
setOutputFile(java.lang.String outputFile)
void
setOutputFilePattern(java.lang.String outputFilePattern)
void
setOutputFiles(java.util.List<java.lang.String> outputFiles)
void
setPreserveDir(boolean preserveDir)
void
setQuiet(boolean quiet)
void
setRegex(boolean regex)
void
setRegexFlags(java.util.List<java.lang.String> regexFlags)
void
setReplacements(java.util.List<Replacement> replacements)
void
setSkip(boolean skip)
void
setToken(java.lang.String token)
void
setTokenFile(java.lang.String tokenFile)
void
setTokenValueMap(java.lang.String tokenValueMap)
void
setUnescape(boolean unescape)
void
setValue(java.lang.String value)
void
setValueFile(java.lang.String valueFile)
void
setVariableTokenValueMap(java.lang.String variableTokenValueMap)
void
setXpath(java.lang.String xpath)
-
-
-
Field Detail
-
INVALID_IGNORE_MISSING_FILE_MESSAGE
private static final java.lang.String INVALID_IGNORE_MISSING_FILE_MESSAGE
- See Also:
- Constant Field Values
-
REGEX_PATTERN_WITH_DELIMITERS_MESSAGE
private static final java.lang.String REGEX_PATTERN_WITH_DELIMITERS_MESSAGE
- See Also:
- Constant Field Values
-
fileUtils
private final FileUtils fileUtils
-
replacerFactory
private final ReplacerFactory replacerFactory
-
tokenValueMapFactory
private final TokenValueMapFactory tokenValueMapFactory
-
fileSelector
private final FileSelector fileSelector
-
patternFlagsFactory
private final PatternFlagsFactory patternFlagsFactory
-
outputFilenameBuilder
private final OutputFilenameBuilder outputFilenameBuilder
-
summaryBuilder
private final SummaryBuilder summaryBuilder
-
processor
private final ReplacementProcessor processor
-
file
@Parameter private java.lang.String file
File to check and replace tokens. Path to single file to replace tokens in. The file must be text (ascii). Based on current execution path.
-
includes
@Parameter private java.util.List<java.lang.String> includes
List of files to include for multiple (or single) replacement. In Ant format (*\/directory/**.properties) Cannot use with outputFile.
-
excludes
@Parameter private java.util.List<java.lang.String> excludes
List of files to exclude for multiple (or single) replacement. In Ant format (*\/directory/**.properties) Cannot use with outputFile.
-
filesToInclude
@Parameter private java.lang.String filesToInclude
Comma separated list of includes. This is split up and used the same way a array of includes would be. In Ant format (*\/directory/**.properties). Files not found are ignored by default.
-
filesToExclude
@Parameter private java.lang.String filesToExclude
List of comma separated files to exclude (must have some includes) for multiple (or single) replacement. This is split up and used the same way a array of excludes would be. In Ant format (**\/directory/do-not-replace.properties). The files replaced will be derived from the list of includes and excludes.
-
token
@Parameter private java.lang.String token
Token to replace. The text to replace within the given file. This may or may not be a regular expression (see regex notes above).
-
tokenFile
@Parameter private java.lang.String tokenFile
Token file containing a token to be replaced in the target file/s. May be multiple words or lines. This is useful if you do not wish to expose the token within your pom or the token is long.
-
ignoreMissingFile
@Parameter private boolean ignoreMissingFile
Ignore missing target file. Use only with file configuration (not includes etc). Set to true to not fail build if the file is not found. First checks if file exists and exits without attempting to replace anything.
-
value
@Parameter private java.lang.String value
Value to replace token with. The text to be written over any found tokens. If no value is given, the tokens found are replaced with an empty string (effectively removing any tokens found). You can also reference grouped regex matches made in the token here by $1, $2, etc.
-
valueFile
@Parameter private java.lang.String valueFile
A file containing a value to replace the given token with. May be multiple words or lines. This is useful if you do not wish to expose the value within your pom or the value is long.
-
regex
@Parameter private boolean regex
Indicates if the token should be located with regular expressions. This should be set to false if the token contains regex characters which may miss the desired tokens or even replace the wrong tokens.
-
outputFile
@Parameter private java.lang.String outputFile
Output to another file. The input file is read and the final output (after replacing tokens) is written to this file. The path and file are created if it does not exist. If it does exist, the contents are overwritten. You should not use outputFile when using a list of includes.
-
outputDir
@Parameter private java.lang.String outputDir
Output to another dir. Destination directory relative to the execution directory for all replaced files to be written to. Use with outputDir to have files written to a specific base location.
-
tokenValueMap
@Parameter private java.lang.String tokenValueMap
Map of tokens and respective values to replace with. A file containing tokens and respective values to replace with. This file may contain multiple entries to support a single file containing different tokens to have replaced. Each token/value pair should be in the format: "token=value" (without quotations). If your token contains ='s you must escape the = character to \=. e.g. tok\=en=value
-
basedir
@Parameter(defaultValue=".") private java.lang.String basedir
Optional base directory for each file to replace. Path to base relative files for replacements from. This feature is useful for multi-module projects. Default "." which is the default Maven basedir.
-
regexFlags
@Parameter private java.util.List<java.lang.String> regexFlags
List of standard Java regular expression Pattern flags (see Java Doc). Must contain one or more of: * CANON_EQ * CASE_INSENSITIVE * COMMENTS * DOTALL * LITERAL * MULTILINE * UNICODE_CASE * UNIX_LINES
-
replacements
@Parameter private java.util.List<Replacement> replacements
List of replacements with token/value pairs. Each replacement element to contain sub-elements as token/value pairs. Each token within the given file will be replaced by it's respective value.
-
commentsEnabled
@Parameter(defaultValue="true") private boolean commentsEnabled
Comments enabled in the tokenValueMapFile. Comment lines start with '#'. If your token starts with an '#' then you must supply the commentsEnabled parameter and with a value of false. Default is true.
-
skip
@Parameter(defaultValue="false") private boolean skip
Skip running this plugin. Default is false.
-
outputBasedir
@Parameter private java.lang.String outputBasedir
Base directory (appended) to use for outputDir. Having this existing but blank will cause the outputDir to be based on the execution directory.
-
preserveDir
@Parameter(defaultValue="true") private boolean preserveDir
Parent directory is preserved when replacing files found from includes and being written to an outputDir. Default is true.
-
quiet
@Parameter(defaultValue="false") private boolean quiet
Stops printing a summary of files that have had replacements performed upon them when true. Default is false.
-
unescape
@Parameter(defaultValue="false") private boolean unescape
Unescape tokens and values to Java format. e.g. token\n is unescaped to token(carriage return). Default is false.
-
delimiters
@Parameter private java.util.List<java.lang.String> delimiters
Add a list of delimiters which are added on either side of tokens to match against. You may also use the '' character to place the token in the desired location for matching. e.g. @ would match @token@. e.g. ${} would match ${token}.
-
variableTokenValueMap
@Parameter private java.lang.String variableTokenValueMap
Variable tokenValueMap. Same as the tokenValueMap but can be an include configuration rather than an outside property file. Similar to tokenValueMap but incline configuration inside the pom. This parameter may contain multiple entries to support a single file containing different tokens to have replaced. Format is comma separated. e.g. token=value,token2=value2 Comments are not supported.
-
ignoreErrors
@Parameter(defaultValue="false") private boolean ignoreErrors
Ignore any errors produced by this plugin such as files not being found and continue with the build. First checks if file exists and exits without attempting to replace anything. Only usable with file parameter. Default is false.
-
xpath
@Parameter private java.lang.String xpath
X-Path expression for locating node's whose content you wish to replace. This is useful if you have the same token appearing in many nodes but wish to only replace the contents of one or more of them.
-
encoding
@Parameter(defaultValue="${project.build.sourceEncoding}") private java.lang.String encoding
File encoding used when reading and writing files. Default system encoding used when not specified.
-
inputFilePattern
@Parameter private java.lang.String inputFilePattern
Regular expression is run on an input file's name to create the output file with. Must be used in conjunction with outputFilePattern.
-
outputFilePattern
@Parameter private java.lang.String outputFilePattern
Regular expression groups from inputFilePattern are used in this pattern to create an output file per input file. Must be used in conjunction with inputFilePattern. The parameter outputFile is ignored when outputFilePattern is used.
-
maxReplacements
@Parameter private java.lang.Integer maxReplacements
Set a maximum number of files which can be replaced per execution.
-
files
@Parameter private java.util.List<java.lang.String> files
list files
-
outputFiles
@Parameter private java.util.List<java.lang.String> outputFiles
list out put file
-
-
Constructor Detail
-
ReplacerMojo
public ReplacerMojo()
-
ReplacerMojo
public ReplacerMojo(FileUtils fileUtils, ReplacementProcessor processor, ReplacerFactory replacerFactory, TokenValueMapFactory tokenValueMapFactory, FileSelector fileSelector, PatternFlagsFactory patternFlagsFactory, OutputFilenameBuilder outputFilenameBuilder, SummaryBuilder summaryBuilder)
-
-
Method Detail
-
execute
public void execute() throws org.apache.maven.plugin.MojoExecutionException
- Throws:
org.apache.maven.plugin.MojoExecutionException
-
limit
private <T> java.util.List<T> limit(java.util.List<T> all)
-
checkFileExists
private boolean checkFileExists() throws org.apache.maven.plugin.MojoExecutionException
- Throws:
org.apache.maven.plugin.MojoExecutionException
-
getBaseDirPrefixedFilename
private java.lang.String getBaseDirPrefixedFilename(java.lang.String file)
-
addIncludesFilesAndExcludedFiles
private void addIncludesFilesAndExcludedFiles()
-
addToList
private void addToList(java.util.List<java.lang.String> toAdds, java.util.List<java.lang.String> destination)
-
replaceContents
private void replaceContents(ReplacementProcessor processor, java.util.List<Replacement> replacements, java.lang.String inputFile) throws java.io.IOException
- Throws:
java.io.IOException
-
replaceContents
private void replaceContents(ReplacementProcessor processor, java.util.List<Replacement> replacements, java.util.List<java.lang.String> inputFiles) throws java.io.IOException
- Throws:
java.io.IOException
-
buildReplacements
private java.util.List<Replacement> buildReplacements() throws java.io.IOException
- Throws:
java.io.IOException
-
getDelimiterReplacements
private java.util.List<Replacement> getDelimiterReplacements(java.util.List<Replacement> replacements)
-
buildDelimiters
private java.util.List<DelimiterBuilder> buildDelimiters()
-
setRegex
public void setRegex(boolean regex)
-
setFile
public void setFile(java.lang.String file)
-
getFile
public java.lang.String getFile()
-
setToken
public void setToken(java.lang.String token)
-
setValue
public void setValue(java.lang.String value)
-
setTokenFile
public void setTokenFile(java.lang.String tokenFile)
-
setValueFile
public void setValueFile(java.lang.String valueFile)
-
setIgnoreMissingFile
public void setIgnoreMissingFile(boolean ignoreMissingFile)
-
setOutputFile
public void setOutputFile(java.lang.String outputFile)
-
setTokenValueMap
public void setTokenValueMap(java.lang.String tokenValueMap)
-
setFilesToInclude
public void setFilesToInclude(java.lang.String filesToInclude)
-
setFilesToExclude
public void setFilesToExclude(java.lang.String filesToExclude)
-
setBasedir
public void setBasedir(java.lang.String baseDir)
-
setReplacements
public void setReplacements(java.util.List<Replacement> replacements)
-
setRegexFlags
public void setRegexFlags(java.util.List<java.lang.String> regexFlags)
-
setIncludes
public void setIncludes(java.util.List<java.lang.String> includes)
-
getIncludes
public java.util.List<java.lang.String> getIncludes()
-
setExcludes
public void setExcludes(java.util.List<java.lang.String> excludes)
-
getExcludes
public java.util.List<java.lang.String> getExcludes()
-
getFilesToInclude
public java.lang.String getFilesToInclude()
-
getFilesToExclude
public java.lang.String getFilesToExclude()
-
setOutputDir
public void setOutputDir(java.lang.String outputDir)
-
isCommentsEnabled
public boolean isCommentsEnabled()
-
setCommentsEnabled
public void setCommentsEnabled(boolean commentsEnabled)
-
setOutputBasedir
public void setOutputBasedir(java.lang.String outputBasedir)
-
isPreserveDir
public boolean isPreserveDir()
-
setPreserveDir
public void setPreserveDir(boolean preserveDir)
-
getBasedir
public java.lang.String getBasedir()
-
getOutputDir
public java.lang.String getOutputDir()
-
getOutputBasedir
public java.lang.String getOutputBasedir()
-
getOutputFile
public java.lang.String getOutputFile()
-
setQuiet
public void setQuiet(boolean quiet)
-
setDelimiters
public void setDelimiters(java.util.List<java.lang.String> delimiters)
-
getDelimiters
public java.util.List<java.lang.String> getDelimiters()
-
setUnescape
public void setUnescape(boolean unescape)
-
isUnescape
public boolean isUnescape()
-
setVariableTokenValueMap
public void setVariableTokenValueMap(java.lang.String variableTokenValueMap)
-
getVariableTokenValueMap
public java.lang.String getVariableTokenValueMap()
-
setIgnoreErrors
public void setIgnoreErrors(boolean ignoreErrors)
-
isIgnoreErrors
public boolean isIgnoreErrors()
-
setXpath
public void setXpath(java.lang.String xpath)
-
setEncoding
public void setEncoding(java.lang.String encoding)
-
setInputFilePattern
public void setInputFilePattern(java.lang.String inputFilePattern)
-
setOutputFilePattern
public void setOutputFilePattern(java.lang.String outputFilePattern)
-
getInputFilePattern
public java.lang.String getInputFilePattern()
-
getOutputFilePattern
public java.lang.String getOutputFilePattern()
-
setSkip
public void setSkip(boolean skip)
-
isSkip
public boolean isSkip()
-
setMaxReplacements
public void setMaxReplacements(java.lang.Integer maxReplacements)
-
getFiles
public java.util.List<java.lang.String> getFiles()
-
setFiles
public void setFiles(java.util.List<java.lang.String> files)
-
getOutputFiles
public java.util.List<java.lang.String> getOutputFiles()
-
setOutputFiles
public void setOutputFiles(java.util.List<java.lang.String> outputFiles)
-
-