Commit 965a3bbd authored by David Trott's avatar David Trott

Update code to be consistent with the protoc code.

This shortens the generated paths based on the location of the m2 repository.
Prints any thirft errors to stderr.
Moves the dependencies that are unpacked as part of the build to the target directory.
parent 0cbbbea0
......@@ -2,6 +2,7 @@ package org.apache.thrift.maven;
import com.google.common.collect.ImmutableSet;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.repository.DefaultArtifactRepository;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
......@@ -11,6 +12,7 @@ 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.util.List;
import java.util.Set;
......@@ -81,11 +83,19 @@ abstract class AbstractThriftMojo extends AbstractMojo {
* Since {@code thrift} cannot access jars, thrift files in dependencies are extracted to this location
* and deleted on exit. This directory is always cleaned during execution.
*
* @parameter expression="${java.io.tmpdir}/maven-thrift"
* @parameter expression="${project.build.directory}/thrift-dependencies"
* @required
*/
private File temporaryThriftFileDirectory;
/**
* This is the path to the local maven {@code repository}.
*
* @parameter default-value="${localRepository}"
* @required
*/
private DefaultArtifactRepository localRepository;
/**
* @parameter
*/
......@@ -124,6 +134,8 @@ abstract class AbstractThriftMojo extends AbstractMojo {
.build();
final int exitStatus = thrift.compile();
if (exitStatus != 0) {
getLog().error("thrift failed output: " + thrift.getOutput());
getLog().error("thrift failed error: " + thrift.getError());
throw new MojoFailureException(
"thrift did not exit cleanly. Review output for more information.");
}
......@@ -134,7 +146,7 @@ abstract class AbstractThriftMojo extends AbstractMojo {
} catch (IllegalArgumentException e) {
throw new MojoFailureException("thrift failed to execute because: " + e.getMessage(), e);
} catch (CommandLineException e) {
throw new MojoExecutionException("An error occurred while invoking thrift.", e);
throw new MojoExecutionException("An error occured while invoking thrift.", e);
}
} else {
getLog().info(format("%s does not exist. Review the configuration or consider disabling the plugin.",
......@@ -189,8 +201,7 @@ abstract class AbstractThriftMojo extends AbstractMojo {
}
Set<File> thriftDirectories = newHashSet();
for (File classpathElementFile : classpathElementFiles) {
checkArgument(classpathElementFile.isFile(), "%s is not a file",
classpathElementFile);
if (classpathElementFile.isFile() && classpathElementFile.canRead()) {
// create the jar file. the constructor validates.
JarFile classpathJar;
try {
......@@ -202,17 +213,26 @@ abstract class AbstractThriftMojo extends AbstractMojo {
for (JarEntry jarEntry : list(classpathJar.entries())) {
final String jarEntryName = jarEntry.getName();
if (jarEntry.getName().endsWith(THRIFT_FILE_SUFFIX)) {
//replace logic is used in order to fix issue of an invalid windows classpath to the thrift file
// inside a jar.
final File uncompressedCopy =
new File(new File(temporaryThriftFileDirectory, classpathJar
.getName().replace(":", "_")), jarEntryName);
new File(new File(temporaryThriftFileDirectory,
truncatePath(classpathJar.getName())), jarEntryName);
uncompressedCopy.getParentFile().mkdirs();
copyStreamToFile(new RawInputStreamFacade(classpathJar
.getInputStream(jarEntry)), uncompressedCopy);
thriftDirectories.add(uncompressedCopy.getParentFile());
}
}
} else if (classpathElementFile.isDirectory()) {
File[] thriftFiles = classpathElementFile.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.endsWith(THRIFT_FILE_SUFFIX);
}
});
if (thriftFiles.length > 0) {
thriftDirectories.add(classpathElementFile);
}
}
}
forceDeleteOnExit(temporaryThriftFileDirectory);
return ImmutableSet.copyOf(thriftDirectories);
......@@ -235,4 +255,32 @@ abstract class AbstractThriftMojo extends AbstractMojo {
}
return ImmutableSet.copyOf(thriftFiles);
}
/**
* 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.
*/
String truncatePath(final String jarPath) {
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;
}
}
......@@ -36,6 +36,8 @@ final class Thrift {
private final ImmutableSet<File> thriftPathElements;
private final ImmutableSet<File> thriftFiles;
private final File javaOutputDirectory;
private final CommandLineUtils.StringStreamConsumer output;
private final CommandLineUtils.StringStreamConsumer error;
/**
* Constructs a new instance. This should only be used by the {@link Builder}.
......@@ -51,8 +53,9 @@ final class Thrift {
this.executable = checkNotNull(executable, "executable");
this.thriftPathElements = checkNotNull(thriftPath, "thriftPath");
this.thriftFiles = checkNotNull(thriftFiles, "thriftFiles");
this.javaOutputDirectory =
checkNotNull(javaOutputDirectory, "javaOutputDirectory");
this.javaOutputDirectory = checkNotNull(javaOutputDirectory, "javaOutputDirectory");
this.error = new CommandLineUtils.StringStreamConsumer();
this.output = new CommandLineUtils.StringStreamConsumer();
}
/**
......@@ -67,8 +70,6 @@ final class Thrift {
for (File thriftFile : thriftFiles) {
Commandline cl = new Commandline(executable);
cl.addArguments(buildThriftCommand(thriftFile).toArray(new String[]{}));
StreamConsumer output = new DefaultConsumer();
StreamConsumer error = new DefaultConsumer();
final int result = CommandLineUtils.executeCommandLine(cl, null, output, error);
if (result != 0) {
......@@ -126,6 +127,20 @@ final class Thrift {
}
}
/**
* @return the output
*/
public String getOutput() {
return output.getOutput();
}
/**
* @return the error
*/
public String getError() {
return error.getOutput();
}
/**
* This class builds {@link Thrift} instances.
*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment