Last active
August 29, 2015 14:27
-
-
Save ricston-git/4642ca2b03b50d9dc1b9 to your computer and use it in GitHub Desktop.
How to create a Maven plugin to search and replace
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mvn archetype:create \ | |
-DgroupId=com.ricston \ | |
-DartifactId=searchreplace-maven-plugin \ | |
-DarchetypeGroupId=org.apache.maven.archetypes \ | |
-DarchetypeArtifactId=maven-archetype-mojo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
// ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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