001 /** 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 package org.fusesource.hawtbuf.proto.compiler; 019 020 import java.io.File; 021 import java.io.FileFilter; 022 import java.util.Arrays; 023 import java.util.List; 024 025 import org.apache.maven.plugin.AbstractMojo; 026 import org.apache.maven.plugin.MojoExecutionException; 027 import org.apache.maven.project.MavenProject; 028 029 /** 030 * A Maven Mojo so that the Proto compiler can be used with maven. 031 * 032 * @goal compile 033 * @phase process-sources 034 */ 035 public class ProtoMojo 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 proto files (<code>*.proto</code>) are 048 * located. 049 * 050 * @parameter default-value="${basedir}/src/main/proto" 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/proto" 058 */ 059 private File mainOutputDirectory; 060 061 /** 062 * The directory where the proto files (<code>*.proto</code>) are 063 * located. 064 * 065 * @parameter default-value="${basedir}/src/test/proto" 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/proto" 073 */ 074 private File testOutputDirectory; 075 076 /** 077 * The type of generator to run. 078 * 079 * @parameter default-value="default" 080 */ 081 private String type; 082 083 public void execute() throws MojoExecutionException { 084 085 File[] mainFiles = null; 086 if ( mainSourceDirectory.exists() ) { 087 mainFiles = mainSourceDirectory.listFiles(new FileFilter() { 088 public boolean accept(File pathname) { 089 return pathname.getName().endsWith(".proto"); 090 } 091 }); 092 if (mainFiles==null || mainFiles.length==0) { 093 getLog().warn("No proto files found in directory: " + mainSourceDirectory.getPath()); 094 } else { 095 processFiles(mainFiles, mainOutputDirectory); 096 this.project.addCompileSourceRoot(mainOutputDirectory.getAbsolutePath()); 097 } 098 } 099 100 File[] testFiles = null; 101 if ( testSourceDirectory.exists() ) { 102 testFiles = testSourceDirectory.listFiles(new FileFilter() { 103 public boolean accept(File pathname) { 104 return pathname.getName().endsWith(".proto"); 105 } 106 }); 107 if (testFiles==null || testFiles.length==0) { 108 getLog().warn("No proto files found in directory: " + testSourceDirectory.getPath()); 109 } else { 110 processFiles(testFiles, testOutputDirectory); 111 this.project.addTestCompileSourceRoot(testOutputDirectory.getAbsolutePath()); 112 } 113 } 114 } 115 116 private void processFiles(File[] mainFiles, File outputDir) throws MojoExecutionException { 117 List<File> recFiles = Arrays.asList(mainFiles); 118 for (File file : recFiles) { 119 try { 120 getLog().info("Compiling: "+file.getPath()); 121 if( "default".equals(type) ) { 122 JavaGenerator generator = new JavaGenerator(); 123 generator.setOut(outputDir); 124 generator.compile(file); 125 } else if( "alt".equals(type) ) { 126 AltJavaGenerator generator = new AltJavaGenerator(); 127 generator.setOut(outputDir); 128 generator.compile(file); 129 } 130 } catch (CompilerException e) { 131 getLog().error("Protocol Buffer Compiler failed with the following error(s):"); 132 for (String error : e.getErrors() ) { 133 getLog().error(""); 134 getLog().error(error); 135 } 136 getLog().error(""); 137 throw new MojoExecutionException("Compile failed. For more details see error messages listed above.", e); 138 } 139 } 140 } 141 142 }