com.github.ngeor.yak4j.FilenameConventionsMojo Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of yak4j-filename-conventions-maven-plugin Show documentation
Show all versions of yak4j-filename-conventions-maven-plugin Show documentation
yak shaving for Java: A Maven plugin which enforces filename conventions
package com.github.ngeor.yak4j;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
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;
import org.codehaus.plexus.util.FileUtils;
/**
* A mojo that checks filename conventions.
*/
@Mojo(name = "check", defaultPhase = LifecyclePhase.VALIDATE)
public class FilenameConventionsMojo extends AbstractMojo {
@Parameter(required = true)
private File directory;
@Parameter(required = true)
private String[] includes;
@Parameter
private String[] excludes;
@Parameter(required = true)
private String pattern;
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
Log log = getLog();
try {
log.debug("Getting filenames");
List fileNames = FileUtils.getFileNames(
directory,
String.join(",", includes),
excludes != null ? String.join(",", excludes) : null,
false
);
log.debug("Compiling regex pattern");
Pattern regex = Pattern.compile(pattern);
for (String fileName : fileNames) {
log.debug(String.format("Testing filename %s", fileName));
Matcher matcher = regex.matcher(fileName);
if (!matcher.matches()) {
throw new MojoFailureException(
String.format("Filename %s did not match pattern %s", fileName, pattern));
}
}
log.info(String.format("%d file(s) in directory %s match patter %s", fileNames.size(), directory, pattern));
} catch (IOException ex) {
throw new MojoExecutionException(ex.getMessage(), ex);
}
}
}