Class ReplacerMojo

  • All Implemented Interfaces:
    org.apache.maven.plugin.ContextEnabled, org.apache.maven.plugin.Mojo

    public class ReplacerMojo
    extends org.apache.maven.plugin.AbstractMojo
    Goal replaces token with value inside file
    • 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
      • file

        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

        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

        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

        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

        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

        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

        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

        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

        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

        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

        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

        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

        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

        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

        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

        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

        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

        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

        private boolean skip
        Skip running this plugin. Default is false.
      • outputBasedir

        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

        private boolean preserveDir
        Parent directory is preserved when replacing files found from includes and being written to an outputDir. Default is true.
      • quiet

        private boolean quiet
        Stops printing a summary of files that have had replacements performed upon them when true. Default is false.
      • unescape

        private boolean unescape
        Unescape tokens and values to Java format. e.g. token\n is unescaped to token(carriage return). Default is false.
      • delimiters

        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

        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

        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

        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

        private java.lang.String encoding
        File encoding used when reading and writing files. Default system encoding used when not specified.
      • inputFilePattern

        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

        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

        private java.lang.Integer maxReplacements
        Set a maximum number of files which can be replaced per execution.
      • files

        private java.util.List<java.lang.String> files
        list files
      • outputFiles

        private java.util.List<java.lang.String> outputFiles
        list out put file
    • 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)
      • 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)