
com.google.code.maven_replacer_plugin.ReplacerMojo Maven / Gradle / Ivy
package com.google.code.maven_replacer_plugin;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import com.google.code.maven_replacer_plugin.file.FileUtils;
import com.google.code.maven_replacer_plugin.include.FileSelector;
/**
* Goal replaces token with value inside file
*
* @goal replace
*
* @phase compile
*/
public class ReplacerMojo extends AbstractMojo {
private final FileUtils fileUtils;
private final TokenReplacer tokenReplacer;
private final ReplacerFactory replacerFactory;
private final TokenValueMapFactory tokenValueMapFactory;
private final FileSelector fileSelector;
private final PatternFlagsFactory patternFlagsFactory;
/**
* File to check and replace tokens
*
* @parameter expression=""
*/
private String file;
/**
* List of included files pattern in ant format. Cannot use with outputFile.
*
* @parameter expression=""
*/
private List includes;
/**
* List of excluded files pattern in ant format. Cannot use with outputFile.
*
* @parameter expression=""
*/
private List excludes;
/**
* Comma separated list of includes. This is split up and used the same way a array of includes would be.
*
* @parameter expression=""
*/
private String filesToInclude;
/**
* Comma separated list of excludes. This is split up and used the same way a array of excludes would be.
*
* @parameter expression=""
*/
private String filesToExclude;
/**
* Token
*
* @parameter expression=""
*/
private String token;
/**
* Token file
*
* @parameter expression=""
*/
private String tokenFile;
/**
* Ignore missing files
*
* @parameter expression=""
*/
private boolean ignoreMissingFile;
/**
* Value to replace token with
*
* @parameter expression=""
*/
private String value;
/**
* Value file to read value to replace token with
*
* @parameter expression=""
*/
private String valueFile;
/**
* Token uses regex
*
* @parameter expression=""
*/
private boolean regex = true;
/**
* Output to another file
*
* @parameter expression=""
*/
private String outputFile;
/**
* Map of tokens and respective values to replace with
*
* @parameter expression=""
*/
private String tokenValueMap;
/**
* Optional base directory for each file to replace
*
* @parameter expression="${basedir}"
*/
private String basedir;
/**
* List of regex flags.
* Must contain one or more of:
* * CANON_EQ
* * CASE_INSENSITIVE
* * COMMENTS
* * DOTALL
* * LITERAL
* * MULTILINE
* * UNICODE_CASE
* * UNIX_LINES
*
* @parameter expression=""
*/
private List regexFlags;
/**
* List of replacements with token/value pairs
*
* @parameter expression=""
*/
private List replacements;
public ReplacerMojo() {
super();
this.basedir = ".";
this.fileUtils = new FileUtils();
this.tokenReplacer = new TokenReplacer();
this.replacerFactory = new ReplacerFactory(fileUtils, tokenReplacer);
this.tokenValueMapFactory = new TokenValueMapFactory(fileUtils);
this.fileSelector = new FileSelector();
this.patternFlagsFactory = new PatternFlagsFactory();
}
public ReplacerMojo(FileUtils fileUtils, TokenReplacer tokenReplacer,
ReplacerFactory replacerFactory, TokenValueMapFactory tokenValueMapFactory,
FileSelector fileSelector, PatternFlagsFactory patternFlagsFactory) {
super();
this.basedir = ".";
this.fileUtils = fileUtils;
this.tokenReplacer = tokenReplacer;
this.replacerFactory = replacerFactory;
this.tokenValueMapFactory = tokenValueMapFactory;
this.fileSelector = fileSelector;
this.patternFlagsFactory = patternFlagsFactory;
}
public void execute() throws MojoExecutionException {
try {
if (ignoreMissingFile && fileUtils.fileNotExists(file)) {
getLog().info("Ignoring missing file");
return;
}
Replacer replacer = replacerFactory.create();
List contexts = getContexts();
addIncludesFilesAndExcludedFiles();
if (includes == null || includes.isEmpty()) {
getOutputFile(file);
replaceContents(replacer, contexts, file);
return;
}
for (String file : fileSelector.listIncludes(basedir, includes, excludes)) {
getOutputFile(file);
replaceContents(replacer, contexts, file);
}
} catch (IOException e) {
throw new MojoExecutionException(e.getMessage(), e);
}
}
private String getFilename(String file) {
return basedir + '/' + file;
}
private void addIncludesFilesAndExcludedFiles() {
if (filesToInclude != null) {
String[] splitFiles = filesToInclude.split(",");
if (includes == null) {
includes = new ArrayList();
}
addToList(Arrays.asList(splitFiles), includes);
}
if (filesToExclude != null) {
String[] splitFiles = filesToExclude.split(",");
if (excludes == null) {
excludes = new ArrayList();
}
addToList(Arrays.asList(splitFiles), excludes);
}
}
private void addToList(List toAdds, List destination) {
for (String toAdd : toAdds) {
destination.add(toAdd.trim());
}
}
private void replaceContents(Replacer replacer, List contexts, String inputFile) throws IOException {
getLog().info("Replacing content in " + getFilename(inputFile));
replacer.replace(contexts, regex, getFilename(inputFile), getOutputFile(getFilename(inputFile)),
patternFlagsFactory.buildFlags(regexFlags));
}
private List getContexts() throws IOException {
if (replacements != null) {
return replacements;
}
if (tokenValueMap == null) {
Replacement context = new Replacement(fileUtils, token, value);
context.setTokenFile(tokenFile);
context.setValueFile(valueFile);
return Arrays.asList(context);
}
return tokenValueMapFactory.contextsForFile(tokenValueMap);
}
private String getOutputFile(String file) {
if (outputFile == null) {
return file;
}
getLog().info("Outputting to: " + getFilename(outputFile));
if (fileUtils.fileNotExists(file)) {
fileUtils.ensureFolderStructureExists(getFilename(outputFile));
}
return getFilename(outputFile);
}
public void setRegex(boolean regex) {
this.regex = regex;
}
public void setFile(String file) {
this.file = file;
}
public void setToken(String token) {
this.token = token;
}
public void setValue(String value) {
this.value = value;
}
public void setTokenFile(String tokenFile) {
this.tokenFile = tokenFile;
}
public void setValueFile(String valueFile) {
this.valueFile = valueFile;
}
public void setIgnoreMissingFile(boolean ignoreMissingFile) {
this.ignoreMissingFile = ignoreMissingFile;
}
public void setOutputFile(String outputFile) {
this.outputFile = outputFile;
}
public void setTokenValueMap(String tokenValueMap) {
this.tokenValueMap = tokenValueMap;
}
public void setFilesToInclude(String filesToInclude) {
this.filesToInclude = filesToInclude;
}
public void setFilesToExclude(String filesToExclude) {
this.filesToExclude = filesToExclude;
}
public void setBasedir(String baseDir) {
this.basedir = baseDir;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy