Skip to content

Instantly share code, notes, and snippets.

@ricston-git
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ricston-git/4642ca2b03b50d9dc1b9 to your computer and use it in GitHub Desktop.
Save ricston-git/4642ca2b03b50d9dc1b9 to your computer and use it in GitHub Desktop.
How to create a Maven plugin to search and replace
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.4</version>
<scope>provided</scope><!-- annotations are needed only to build the plugin -->
</dependency>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.4</version>
<executions>
<execution>
<id>default-descriptor</id>
<phase>process-classes</phase>
</execution>
<!-- if you want to generate help goal -->
<execution>
<id>help-goal</id>
<goals>
<goal>helpmojo</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
mvn archetype:create \
-DgroupId=com.ricston \
-DartifactId=searchreplace-maven-plugin \
-DarchetypeGroupId=org.apache.maven.archetypes \
-DarchetypeArtifactId=maven-archetype-mojo
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.LifecyclePhase;
@Mojo(name = "searchreplace", defaultPhase = LifecyclePhase.PROCESS_SOURCES)
public class MyMojo
extends AbstractMojo
{
@Parameter(property = "outputDirectory", defaultValue = "${project.build.directory}")
private File outputDirectory;
// ...
package com.ricston;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
/**
* Search and replace text in given package
*
*/
@Mojo(name = "searchreplace", defaultPhase = LifecyclePhase.PROCESS_SOURCES)
public class SearchReplaceMojo extends AbstractMojo {
public static final String SAME_TARGET_PATH_AS_SOURCE = "";
@Parameter(property = "sr.replaceString")
private String replaceString;
@Parameter(property = "sr.searchRegex")
private String searchRegex;
@Parameter(property = "sr.targetPackage")
private String targetPackage;
@Parameter(property = "sr.sourceDirectory", defaultValue = "${project.build.sourceDirectory}")
protected File sourceDirectory;
@Parameter(property = "sr.targetDirectoryPath")
protected String targetDirectoryPath = SAME_TARGET_PATH_AS_SOURCE;
public void execute() throws MojoExecutionException {
final Log log = getLog();
log.info("=======================");
log.info(String.format("${sr.replaceString}: %s", replaceString));
log.info(String.format("${sr.searchRegex}: %s", searchRegex));
log.info(String.format("${sr.targetPackage}: %s", targetPackage));
log.info(String.format("${sr.sourceDirectory}: %s", sourceDirectory));
log.info(String.format("${sr.targetDirectoryPath}: %s", targetDirectoryPath));
log.info("=======================");
final String targetPackageWithOsSeparator = targetPackage.replaceAll("\\.", File.separator);
final File directoryToProcess = new File(sourceDirectory.getAbsolutePath(), targetPackageWithOsSeparator);
log.info(String.format("directory to process: %s", directoryToProcess));
List<File> files = getFilesInFolder(directoryToProcess, new ArrayList<File>(100));
log.info(String.format("files to process: %s", files));
for (final File file : files) {
try {
String content = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);
content = content.replaceAll(searchRegex, replaceString);
if (SAME_TARGET_PATH_AS_SOURCE.equals(targetDirectoryPath)) {
Path targetPath = file.toPath();
log.info(String.format("By default, writing [%s] to [%s]", file.getName(), targetPath));
Files.write(targetPath, content.getBytes(StandardCharsets.UTF_8));
} else {
Path targetPath = new File(targetDirectoryPath, file.getName()).toPath();
log.info(String.format("Writing [%s] to [%s]", file.getName(), targetPath));
Files.write(targetPath, content.getBytes(StandardCharsets.UTF_8));
}
} catch (IOException e) {
log.info(String.format("could not process %s", file.getName()));
e.printStackTrace();
}
}
}
public List<File> getFilesInFolder(final File folder, List<File> files) {
for (final File fileEntry : folder.listFiles()) {
if (fileEntry.isDirectory()) {
getFilesInFolder(fileEntry, files);
} else {
files.add(fileEntry);
}
}
return files;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment