Permalink
Browse files

AVRO-1188. Java: Permit external schema imports for schemas in Maven …

…plugin. Contributed by Sharmarke Aden.

git-svn-id: https://svn.apache.org/repos/asf/avro/trunk@1408658 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information...
1 parent b81a91a commit 957ccf706a1d124c7fb74caf31862b9c03a99a1b Thomas White committed Nov 13, 2012
View
@@ -15,6 +15,9 @@ Trunk (not yet released)
string or a JSON array of strings in order to emit multiple
annotations. (cutting)
+ AVRO-1188. Java: Permit external schema imports for schemas in
+ Maven plugin. (Sharmarke Aden via tomwhite)
+
IMPROVEMENTS
AVRO-1169. Java: Reduce memory footprint of resolver.
@@ -20,6 +20,7 @@
import java.io.File;
import java.io.IOException;
+import java.util.Arrays;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
@@ -56,6 +57,14 @@
private File testOutputDirectory;
/**
+ * A list of files or directories that should be compiled first thus making
+ * them importable by subsequently compiled schemas. Note that imported files
+ * should not reference each other.
+ * @parameter
+ */
+ protected String[] imports;
+
+ /**
* A set of Ant-like exclusion patterns used to prevent certain files from
* being processed. By default, this set is empty such that no files are
* excluded.
@@ -102,19 +111,40 @@
public void execute() throws MojoExecutionException {
boolean hasSourceDir = null != sourceDirectory
&& sourceDirectory.isDirectory();
+ boolean hasImports = null != imports;
boolean hasTestDir = null != testSourceDirectory
&& testSourceDirectory.isDirectory();
if (!hasSourceDir && !hasTestDir) {
throw new MojoExecutionException("neither sourceDirectory: "
+ sourceDirectory + " or testSourceDirectory: " + testSourceDirectory
+ " are directories");
}
+
+ if (hasImports) {
+ for (String importedFile : imports) {
+ File file = new File(importedFile);
+ if (file.isDirectory()) {
+ String[] includedFiles = getIncludedFiles(file.getAbsolutePath(), excludes, getIncludes());
+ getLog().info("Importing Directory: " + file.getAbsolutePath());
+ getLog().debug("Importing Directory Files: " + Arrays.toString(includedFiles));
+ compileFiles(includedFiles, file, outputDirectory);
+ } else if (file.isFile()) {
+ getLog().info("Importing File: " + file.getAbsolutePath());
+ compileFiles(new String[]{file.getName()}, file.getParentFile(), outputDirectory);
+ }
+ }
+ }
+
if (hasSourceDir) {
String[] includedFiles = getIncludedFiles(
sourceDirectory.getAbsolutePath(), excludes, getIncludes());
compileFiles(includedFiles, sourceDirectory, outputDirectory);
+ }
+
+ if (hasImports || hasSourceDir) {
project.addCompileSourceRoot(outputDirectory.getAbsolutePath());
}
+
if (hasTestDir) {
String[] includedFiles = getIncludedFiles(
testSourceDirectory.getAbsolutePath(), testExcludes,
@@ -130,6 +160,23 @@ public void execute() throws MojoExecutionException {
FileSet fs = new FileSet();
fs.setDirectory(absPath);
fs.setFollowSymlinks(false);
+
+ //exclude imports directory since it has already been compiled.
+ if (imports != null) {
+ String importExclude = null;
+
+ for (String importFile : this.imports) {
+ File file = new File(importFile);
+
+ if (file.isDirectory()) {
+ importExclude = file.getName() + "/**";
+ } else if (file.isFile()) {
+ importExclude = "**/" + file.getName();
+ }
+
+ fs.addExclude(importExclude);
+ }
+ }
for (String include : includes) {
fs.addInclude(include);
}
@@ -34,6 +34,12 @@
*/
public class SchemaMojo extends AbstractAvroMojo {
/**
+ * A parser used to parse all schema files. Using a common parser will
+ * facilitate the import of external schemas.
+ */
+ private Schema.Parser schemaParser = new Schema.Parser();
+
+ /**
* A set of Ant-like inclusion patterns used to select files from the source
* directory for processing. By default, the pattern
* <code>**&#47;*.avsc</code> is used to select grammar files.
@@ -54,8 +60,18 @@
@Override
protected void doCompile(String filename, File sourceDirectory, File outputDirectory) throws IOException {
File src = new File(sourceDirectory, filename);
- Schema.Parser parser = new Schema.Parser();
- Schema schema = parser.parse(src);
+ Schema schema;
+
+ // This is necessary to maintain backward-compatibility. If there are
+ // no imported files then isolate the schemas from each other, otherwise
+ // allow them to share a single schema so resuse and sharing of schema
+ // is possible.
+ if (imports == null) {
+ schema = new Schema.Parser().parse(src);
+ } else {
+ schema = schemaParser.parse(src);
+ }
+
SpecificCompiler compiler = new SpecificCompiler(schema);
compiler.setTemplateDir(templateDirectory);
compiler.setStringType(StringType.valueOf(stringType));

0 comments on commit 957ccf7

Please sign in to comment.