001    /**
002     * Copyright (C) 2009 Progress Software, Inc.
003     * http://fusesource.com
004     * 
005     * Licensed under the Apache License, Version 2.0 (the "License");
006     * you may not use this file except in compliance with the License.
007     * You may obtain a copy of the License at
008     * 
009     *    http://www.apache.org/licenses/LICENSE-2.0
010     * 
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.fusesource.mvnplugins.avro;
018    
019    import java.io.File;
020    import java.io.FileFilter;
021    import java.io.IOException;
022    
023    import org.apache.avro.specific.SpecificCompiler;
024    import org.apache.maven.plugin.AbstractMojo;
025    import org.apache.maven.plugin.MojoExecutionException;
026    import org.apache.maven.project.MavenProject;
027    
028    /**
029     * A Maven Mojo so that the Avro compiler can be used with maven.
030     * 
031     * @goal compile
032     * @phase process-sources 
033     * @author <a href="http://hiramchirino.com">Hiram Chirino</a>
034     */
035    public class AvroMojo extends AbstractMojo {
036    
037        /**
038         * The maven project.
039         * 
040         * @parameter expression="${project}"
041         * @required
042         * @readonly
043         */
044        protected MavenProject project;
045    
046        /**
047         * The directory where the Avro files (<code>*.avpr</code> or <code>*.avsc</code>) are
048         * located.
049         * 
050         * @parameter default-value="${basedir}/src/main/avro"
051         */
052        private File mainSourceDirectory;
053    
054        /**
055         * The directory where the output files will be located.
056         * 
057         * @parameter default-value="${project.build.directory}/generated-sources/avro"
058         */
059        private File mainOutputDirectory;
060        
061        /**
062         * The directory where the Avro files (<code>*.avpr</code> or <code>*.avsc</code>) are
063         * located.
064         * 
065         * @parameter default-value="${basedir}/src/test/avro"
066         */
067        private File testSourceDirectory;
068    
069        /**
070         * The directory where the output files will be located.
071         * 
072         * @parameter default-value="${project.build.directory}/test-generated-sources/avro"
073         */
074        private File testOutputDirectory;
075    
076        public void execute() throws MojoExecutionException {
077    
078            File[] mainFiles = null;
079            if ( mainSourceDirectory.exists() ) {
080                mainFiles = mainSourceDirectory.listFiles(new FileFilter() {
081                    public boolean accept(File pathname) {
082                        String name = pathname.getName();
083                        return name.endsWith(".avsc") || name.endsWith(".avpr");
084                    }
085                });
086                if (mainFiles==null || mainFiles.length==0) {
087                    getLog().warn("No avro files found in directory: " + mainSourceDirectory.getPath());
088                } else {
089                    processFiles(mainFiles, mainOutputDirectory);
090                    this.project.addCompileSourceRoot(mainOutputDirectory.getAbsolutePath());
091                }
092            }
093            
094            File[] testFiles = null;
095            if ( testSourceDirectory.exists() ) {
096                testFiles = testSourceDirectory.listFiles(new FileFilter() {
097                    public boolean accept(File pathname) {
098                        String name = pathname.getName();
099                        return name.endsWith(".avsc") || name.endsWith(".avpr");
100                    }
101                });
102                if (testFiles==null || testFiles.length==0) {
103                    getLog().warn("No avro files found in directory: " + testSourceDirectory.getPath());
104                } else {
105                    processFiles(testFiles, testOutputDirectory);
106                    this.project.addTestCompileSourceRoot(testOutputDirectory.getAbsolutePath());
107                }
108            }
109        }
110    
111        private void processFiles(File[] mainFiles, File outputDir) throws MojoExecutionException {
112            for (File file : mainFiles) {
113                try {
114                    getLog().info("Compiling: "+file.getPath());
115                    if( file.getName().endsWith(".avpr") ) {
116                        SpecificCompiler.compileProtocol(file, outputDir);
117                    } else if( file.getName().endsWith(".avsc") ) {
118                        SpecificCompiler.compileSchema(file, outputDir);
119                    } else {
120                        throw new RuntimeException("Unhandled file type: "+file.getName());
121                    }
122                } catch (IOException e) {
123                    throw new MojoExecutionException("Avro compile failed.", e);
124                }
125            }
126        }
127    
128    }