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

net.dongliu.prettypb.maven.AbstractProtocMojo Maven / Gradle / Ivy

There is a newer version: 0.3.5
Show newest version
package net.dongliu.prettypb.maven;

import org.apache.commons.lang3.StringUtils;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.DependencyResolutionRequiredException;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectHelper;
import org.codehaus.plexus.util.cli.CommandLineException;
import org.codehaus.plexus.util.io.RawInputStreamFacade;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import static com.google.inject.internal.util.Preconditions.*;
import static java.lang.String.format;
import static java.util.Arrays.asList;
import static java.util.Collections.list;
import static org.codehaus.plexus.util.FileUtils.*;

/**
 * Abstract Mojo implementation.
 *
 * This class is extended by ProtocCompileMojo and ProtocTestCompileMojo in order to override the specific configuration for
 * compiling the main or test classes respectively.
 *
 * @author Gregory Kick
 * @author David Trott
 * @author Brice Figureau
 * @author Dong Liu
 */
abstract class AbstractProtocMojo extends AbstractMojo {

    private static final String PROTO_FILE_SUFFIX = ".proto";

    private static final String DEFAULT_INCLUDES = "**/*" + PROTO_FILE_SUFFIX;

    /**
     * The current Maven project.
     *
     * @parameter default-value="${project}"
     * @readonly
     * @required
     */
    protected MavenProject project;

    /**
     * A helper used to add resources to the project.
     *
     * @component
     * @required
     */
    protected MavenProjectHelper projectHelper;

    /**
     * This is the path to the {@code protoc} executable. By default it will search the {@code $PATH}.
     *
     * @parameter default-value="protoc"
     * @required
     */
    private String protocExecutable;

    /**
     * @parameter
     */
    private File[] additionalProtoPathElements = new File[]{};

    /**
     * Since {@code protoc} cannot access jars, proto files in dependencies are extracted to this location
     * and deleted on exit. This directory is always cleaned during execution.
     *
     * @parameter expression="${project.build.directory}/protoc-dependencies"
     * @required
     */
    private File tempProtoFileDirectory;

    /**
     * This is the path to the local maven {@code repository}.
     *
     * @parameter default-value="${localRepository}"
     * @required
     */
    private ArtifactRepository localRepository;

    /**
     * Set this to {@code false} to disable hashing of dependent jar paths.
     *
     * This plugin expands jars on the classpath looking for embedded .proto files.
     * Normally these paths are hashed (MD5) to avoid issues with long file names on windows.
     * However if this property is set to {@code false} longer paths will be used.
     *
     * @parameter default-value="true"
     * @required
     */
    private boolean hashDependentPaths;

    /**
     * @parameter
     */
    private Set includes = Collections.unmodifiableSet(new HashSet<>(Arrays.asList(DEFAULT_INCLUDES)));

    /**
     * @parameter
     */
    private Set excludes = Collections.emptySet();

    /**
     * @parameter
     */
    private long staleMillis = 0;

    /**
     * @parameter
     */
    private boolean checkStaleness = false;

    /**
     * @parameter
     */
    private String type = "java";

    /**
     * Executes the mojo.
     */
    public void execute() throws MojoExecutionException, MojoFailureException {
        checkParameters();
        final File protoSourceRoot = getProtoSourceRoot();

        if (!protoSourceRoot.exists()) {
            getLog().info(format("%s does not exist.", protoSourceRoot));
        }

        try {
            Set protoFiles = findProtoFilesInDirectory(protoSourceRoot);
            final File outputDirectory = getOutputDirectory();
            Set outputFiles = findGeneratedFilesInDirectory(getOutputDirectory());

            if (protoFiles.isEmpty()) {
                getLog().info("No proto files to compile.");
                return;
            }
            if (checkStaleness && ((lastModified(protoFiles) + staleMillis) < lastModified(outputFiles))) {
                getLog().info("Skipping compilation because target directory newer than sources.");
                attachFiles();
                return;
            }

            Set derivedProtoPathElements = extractProtoFromJars(tempProtoFileDirectory,
                    getDependencyArtifactFiles());
            outputDirectory.mkdirs();

            // Quick fix to fix issues with two mvn installs in a row (ie no clean)
            cleanDirectory(outputDirectory);

            Protoc protoc = new Protoc.Builder(protocExecutable, outputDirectory)
                    .addProtoPathElement(protoSourceRoot)
                    .addProtoPathElements(derivedProtoPathElements)
                    .addProtoPathElements(asList(additionalProtoPathElements))
                    .addProtoFiles(protoFiles)
                    .setType(type)
                    .build();
            final int exitStatus = protoc.compile();
            if (exitStatus != 0) {
                getLog().error("protoc failed error: " + protoc.getError());
                getLog().error("protoc failed error: " + StringUtils.join(protoc.getProtocArgs(),
                        ","));
                throw new MojoFailureException(
                        "protoc did not exit cleanly. Review output for more information.");
            }
            attachFiles();
        } catch (IOException | IllegalArgumentException | CommandLineException |
                DependencyResolutionRequiredException e) {
            throw new MojoExecutionException("", e);
        }
    }

    Set findGeneratedFilesInDirectory(File directory) throws IOException {
        if (directory == null || !directory.isDirectory())
            return Collections.emptySet();

        @SuppressWarnings("unchecked")
        List javaFilesInDirectory = getFiles(directory, "**/*.java", null);
        return Collections.unmodifiableSet(new HashSet<>(javaFilesInDirectory));
    }

    private long lastModified(Set files) {
        long result = 0;
        for (File file : files) {
            if (file.lastModified() > result)
                result = file.lastModified();
        }
        return result;
    }

    private void checkParameters() {
        checkNotNull(project, "project");
        checkNotNull(projectHelper, "projectHelper");
        checkNotNull(protocExecutable, "protocExecutable");
        final File protoSourceRoot = getProtoSourceRoot();
        checkNotNull(protoSourceRoot);
        checkArgument(!protoSourceRoot.isFile(), "protoSourceRoot is a file, not a directory");
        checkNotNull(tempProtoFileDirectory, "tempProtoFileDirectory");
        checkState(!tempProtoFileDirectory.isFile(), "tempProtoFileDirectory is a file, not a directory");
        final File outputDirectory = getOutputDirectory();
        checkNotNull(outputDirectory);
        checkState(!outputDirectory.isFile(), "the outputDirectory is a file, not a directory");
    }

    protected abstract File getProtoSourceRoot();

    protected abstract Set getDependencyArtifacts();

    protected abstract File getOutputDirectory();

    protected abstract void attachFiles();

    /**
     * Gets the {@link java.io.File} for each dependency artifact.
     *
     * @return A set of all dependency artifacts.
     */
    private Set getDependencyArtifactFiles() throws DependencyResolutionRequiredException {
        Set dependencyArtifactFiles = new HashSet<>();
        for (Artifact artifact : getDependencyArtifacts()) {
            dependencyArtifactFiles.add(artifact.getFile());
        }
        return Collections.unmodifiableSet(dependencyArtifactFiles);
    }

    private Set extractProtoFromJars(File temporaryProtoFileDirectory,
                                           Iterable classpathElementFiles)
            throws IOException, MojoExecutionException {
        checkNotNull(classpathElementFiles, "classpathElementFiles");
        // clean the temporary directory to ensure that stale files aren't used
        if (temporaryProtoFileDirectory.exists()) {
            cleanDirectory(temporaryProtoFileDirectory);
        }
        Set protoDirectories = new HashSet<>();
        for (File classpathElementFile : classpathElementFiles) {
            // for some reason under IAM, we receive poms as dependent files
            // I am excluding .xml rather than including .jar as there may be other extensions in use (sar, har, zip)
            if (classpathElementFile.isFile() && classpathElementFile.canRead() &&
                    !classpathElementFile.getName().endsWith(".xml")) {

                // create the jar file. the constructor validates.
                JarFile classpathJar;
                try {
                    classpathJar = new JarFile(classpathElementFile);
                } catch (IOException e) {
                    throw new IllegalArgumentException(format(
                            "%s was not a readable artifact", classpathElementFile));
                }
                for (JarEntry jarEntry : list(classpathJar.entries())) {
                    final String jarEntryName = jarEntry.getName();
                    if (jarEntry.getName().endsWith(PROTO_FILE_SUFFIX)) {
                        final File uncompressedCopy =
                                new File(new File(temporaryProtoFileDirectory,
                                        truncatePath(classpathJar.getName())), jarEntryName);
                        uncompressedCopy.getParentFile().mkdirs();
                        copyStreamToFile(new RawInputStreamFacade(classpathJar
                                .getInputStream(jarEntry)), uncompressedCopy);
                        protoDirectories.add(uncompressedCopy.getParentFile());
                    }
                }
            } else if (classpathElementFile.isDirectory()) {
                File[] protoFiles = classpathElementFile.listFiles(new FilenameFilter() {
                    public boolean accept(File dir, String name) {
                        return name.endsWith(PROTO_FILE_SUFFIX);
                    }
                });

                if (protoFiles.length > 0) {
                    protoDirectories.add(classpathElementFile);
                }
            }
        }
        return Collections.unmodifiableSet(protoDirectories);
    }

    private Set findProtoFilesInDirectory(File directory) throws IOException {
        checkNotNull(directory);
        checkArgument(directory.isDirectory(), "%s is not a directory", directory);
        List protoFilesInDirectory = getFiles(directory, StringUtils.join(includes, ","),
                StringUtils.join(excludes, ','));
        return Collections.unmodifiableSet(new HashSet<>(protoFilesInDirectory));
    }

    private Set findProtoFilesInDirectories(Iterable directories) throws IOException {
        checkNotNull(directories);
        Set protoFiles = new HashSet<>();
        for (File directory : directories) {
            protoFiles.addAll(findProtoFilesInDirectory(directory));
        }
        return Collections.unmodifiableSet(protoFiles);
    }

    /**
     * Truncates the path of jar files so that they are relative to the local repository.
     *
     * @param jarPath the full path of a jar file.
     * @return the truncated path relative to the local repository or root of the drive.
     */
    private String truncatePath(final String jarPath) throws MojoExecutionException {

        if (hashDependentPaths) {
            try {
                return toHexString(MessageDigest.getInstance("MD5").digest(jarPath.getBytes()));
            } catch (NoSuchAlgorithmException e) {
                throw new MojoExecutionException("Failed to expand dependent jar", e);
            }
        }

        String repository = localRepository.getBasedir().replace('\\', '/');
        if (!repository.endsWith("/")) {
            repository += "/";
        }

        String path = jarPath.replace('\\', '/');
        int repositoryIndex = path.indexOf(repository);
        if (repositoryIndex != -1) {
            path = path.substring(repositoryIndex + repository.length());
        }

        // By now the path should be good, but do a final check to fix windows machines.
        int colonIndex = path.indexOf(':');
        if (colonIndex != -1) {
            // 2 = :\ in C:\
            path = path.substring(colonIndex + 2);
        }

        return path;
    }

    private static final char[] HEX_CHARS = "0123456789abcdef".toCharArray();

    public static String toHexString(byte[] byteArray) {
        final StringBuilder hexString = new StringBuilder(2 * byteArray.length);
        for (final byte b : byteArray) {
            hexString.append(HEX_CHARS[(b & 0xF0) >> 4]).append(HEX_CHARS[b & 0x0F]);
        }
        return hexString.toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy