Author: tomwhite
Date: Tue Nov 13 09:55:28 2012
New Revision: 1408658
URL: http://svn.apache.org/viewvc?rev=1408658&view=rev
Log:
AVRO-1188. Java: Permit external schema imports for schemas in Maven plugin.
Contributed by Sharmarke Aden.
Modified:
avro/trunk/CHANGES.txt
avro/trunk/lang/java/maven-plugin/src/main/java/org/apache/avro/mojo/AbstractAvroMojo.java
avro/trunk/lang/java/maven-plugin/src/main/java/org/apache/avro/mojo/SchemaMojo.java
Modified: avro/trunk/CHANGES.txt
URL:
http://svn.apache.org/viewvc/avro/trunk/CHANGES.txt?rev=1408658&r1=1408657&r2=1408658&view=diff
==============================================================================
--- avro/trunk/CHANGES.txt (original)
+++ avro/trunk/CHANGES.txt Tue Nov 13 09:55:28 2012
@@ -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.
Modified:
avro/trunk/lang/java/maven-plugin/src/main/java/org/apache/avro/mojo/AbstractAvroMojo.java
URL:
http://svn.apache.org/viewvc/avro/trunk/lang/java/maven-plugin/src/main/java/org/apache/avro/mojo/AbstractAvroMojo.java?rev=1408658&r1=1408657&r2=1408658&view=diff
==============================================================================
---
avro/trunk/lang/java/maven-plugin/src/main/java/org/apache/avro/mojo/AbstractAvroMojo.java
(original)
+++
avro/trunk/lang/java/maven-plugin/src/main/java/org/apache/avro/mojo/AbstractAvroMojo.java
Tue Nov 13 09:55:28 2012
@@ -20,6 +20,7 @@ package org.apache.avro.mojo;
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 @@ public abstract class AbstractAvroMojo e
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,6 +111,7 @@ public abstract class AbstractAvroMojo e
public void execute() throws MojoExecutionException {
boolean hasSourceDir = null != sourceDirectory
&& sourceDirectory.isDirectory();
+ boolean hasImports = null != imports;
boolean hasTestDir = null != testSourceDirectory
&& testSourceDirectory.isDirectory();
if (!hasSourceDir && !hasTestDir) {
@@ -109,12 +119,32 @@ public abstract class AbstractAvroMojo e
+ 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 abstract class AbstractAvroMojo e
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);
}
Modified:
avro/trunk/lang/java/maven-plugin/src/main/java/org/apache/avro/mojo/SchemaMojo.java
URL:
http://svn.apache.org/viewvc/avro/trunk/lang/java/maven-plugin/src/main/java/org/apache/avro/mojo/SchemaMojo.java?rev=1408658&r1=1408657&r2=1408658&view=diff
==============================================================================
---
avro/trunk/lang/java/maven-plugin/src/main/java/org/apache/avro/mojo/SchemaMojo.java
(original)
+++
avro/trunk/lang/java/maven-plugin/src/main/java/org/apache/avro/mojo/SchemaMojo.java
Tue Nov 13 09:55:28 2012
@@ -34,6 +34,12 @@ import org.apache.avro.compiler.specific
*/
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>**/*.avsc</code> is used to select grammar files.
@@ -54,8 +60,18 @@ public class SchemaMojo extends Abstract
@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));