All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.ngeor.yak4j.FilenameConventionsMojo Maven / Gradle / Ivy

There is a newer version: 0.18.2
Show newest version
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);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy