From 2a13245d5d3c5a09c36f2c54f14657eea94f24dd Mon Sep 17 00:00:00 2001 From: Dan Haeberlein Date: Mon, 13 May 2013 11:51:40 +0200 Subject: [PATCH 1/2] added goal 'compile-directory' --- pom.xml | 2 +- protostuff-maven-plugin/README.md | 87 ++++++++ protostuff-maven-plugin/pom.xml | 12 ++ .../protostuff/mojo/ProtoCompilerDirMojo.java | 187 ++++++++++++++++++ .../protostuff/mojo/ProtoDirConfig.java | 30 +++ .../mojo/TestProtoCompilerMojo.java | 168 ++++++++++++++++ .../mojo/stubs/ProtostuffMavenPluginStub.java | 91 +++++++++ .../proto_directory/default_dir/pom.xml | 36 ++++ .../proto_directory/default_output/pom.xml | 35 ++++ .../empty_proto_directory/pom.xml | 35 ++++ .../proto_directory/multiple_dir/pom.xml | 37 ++++ .../test/resources/proto/fixture1/foo.proto | 15 ++ .../test/resources/proto/fixture1/test.txt | 1 + .../test/resources/proto/fixture2/name.proto | 10 + .../resources/proto/fixture2/person.proto | 27 +++ 15 files changed, 772 insertions(+), 1 deletion(-) create mode 100644 protostuff-maven-plugin/README.md create mode 100644 protostuff-maven-plugin/src/main/java/com/dyuproject/protostuff/mojo/ProtoCompilerDirMojo.java create mode 100644 protostuff-maven-plugin/src/main/java/com/dyuproject/protostuff/mojo/ProtoDirConfig.java create mode 100644 protostuff-maven-plugin/src/test/java/com/dyuproject/protostuff/mojo/TestProtoCompilerMojo.java create mode 100644 protostuff-maven-plugin/src/test/java/com/dyuproject/protostuff/mojo/stubs/ProtostuffMavenPluginStub.java create mode 100644 protostuff-maven-plugin/src/test/resources/project-to-test/proto_directory/default_dir/pom.xml create mode 100644 protostuff-maven-plugin/src/test/resources/project-to-test/proto_directory/default_output/pom.xml create mode 100644 protostuff-maven-plugin/src/test/resources/project-to-test/proto_directory/empty_proto_directory/pom.xml create mode 100644 protostuff-maven-plugin/src/test/resources/project-to-test/proto_directory/multiple_dir/pom.xml create mode 100644 protostuff-maven-plugin/src/test/resources/proto/fixture1/foo.proto create mode 100644 protostuff-maven-plugin/src/test/resources/proto/fixture1/test.txt create mode 100644 protostuff-maven-plugin/src/test/resources/proto/fixture2/name.proto create mode 100644 protostuff-maven-plugin/src/test/resources/proto/fixture2/person.proto diff --git a/pom.xml b/pom.xml index a3db61e1..1d83a3e4 100644 --- a/pom.xml +++ b/pom.xml @@ -119,7 +119,7 @@ org.apache.maven.plugins maven-javadoc-plugin - 2.5 + 2.9 com.google.*:org.apache.*:org.codehaus.*:javax.*:com.sun.* diff --git a/protostuff-maven-plugin/README.md b/protostuff-maven-plugin/README.md new file mode 100644 index 00000000..8eb34387 --- /dev/null +++ b/protostuff-maven-plugin/README.md @@ -0,0 +1,87 @@ +# Protostuff Maven Plugin + +Maven plugin to execute the protostuff compiler to create java classes from *.proto files. + +## Getting started + +There are two goals to invoke the plugin. + +### compile goal + +The compile goal allows you to invoke the plugin with detailed configuration about your proto files. + +- it is possible to configure the input using the following configuration tags: + +```xml + + + + src/main/resources/foo.proto + src/main/java + java_bean + UTF-8 + + + generate_field_map + + + + + +``` + +- or using a property file for the compiler, adding the filepath to the plugin like this: + +```xml + + ./src/main/resources/config.properties + +``` + +More information to the compile goal are available [here](https://code.google.com/p/protostuff/wiki/CompilerViaMaven) on the project site. + +### compile-directory goal + +For being able to compile all "proto" files of certain directories, you can use the compile-directory goal. +This is how it would look like: + +```xml + + + generate-protobuf-sources + generate-sources + + compile-directory + + + ${project.build.generatedSourceOutput}/protostuff + + + ${project.build.sourceDirectory}/../proto + + java_bean + + + + +``` + +The outputBaseDir tag points to the root directory, in which the java classes will be created. The default value is ${project.build.Directory}/generated-sources/protostuff. +(For inserting the classes afterwards in the build process, you need to invoke the "add-source" goal of the build helper plugin with the apropriate path.) + +The pathToProtoDirectories tag can be filled with subtags containing a directory path. All files with the extention .proto in this folder will be compiled to +the outputBaseDir. + +The output tag indicates possible types of the output, the default value is set to java_bean. Look at the [plugin site](https://code.google.com/p/protostuff/wiki/CompilerViaMaven) to +know, which one to use best! + +## Colophon + +### Contributers +This maven plugin was created as a part of [protostuff](https://code.google.com/p/protostuff/). + +The compile-directory goal was added by Dan Häberlein, [TIQ-Solutions](www.tiq-solutions.com), Germany. + +### License + +All changes made are licensed under [Apache License, version 2.0](http://www.apache.org/licenses/LICENSE-2.0). diff --git a/protostuff-maven-plugin/pom.xml b/protostuff-maven-plugin/pom.xml index de387139..63283ed8 100644 --- a/protostuff-maven-plugin/pom.xml +++ b/protostuff-maven-plugin/pom.xml @@ -61,6 +61,18 @@ junit test + + org.apache.maven.shared + maven-plugin-testing-harness + 1.1 + test + + + org.codehaus.plexus + plexus-utils + 3.0.10 + test + com.dyuproject.protostuff protostuff-codegen diff --git a/protostuff-maven-plugin/src/main/java/com/dyuproject/protostuff/mojo/ProtoCompilerDirMojo.java b/protostuff-maven-plugin/src/main/java/com/dyuproject/protostuff/mojo/ProtoCompilerDirMojo.java new file mode 100644 index 00000000..d15ab376 --- /dev/null +++ b/protostuff-maven-plugin/src/main/java/com/dyuproject/protostuff/mojo/ProtoCompilerDirMojo.java @@ -0,0 +1,187 @@ +//======================================================================== +//Copyright 2007-2010 David Yu dyuproject@gmail.com +//Copyright 2013 Dan Häberlein dan.haeberlein@tiq-solutions.de +//------------------------------------------------------------------------ +//Licensed under the Apache License, Version 2.0 (the "License"); +//you may not use this file except in compliance with the License. +//You may obtain a copy of the License at +//http://www.apache.org/licenses/LICENSE-2.0 +//Unless required by applicable law or agreed to in writing, software +//distributed under the License is distributed on an "AS IS" BASIS, +//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +//See the License for the specific language governing permissions and +//limitations under the License. +//======================================================================== + +package com.dyuproject.protostuff.mojo; + +import java.io.File; +import java.io.FileFilter; +import java.io.IOException; +import java.util.Collections; +import java.util.List; + +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.project.MavenProject; + +import com.dyuproject.protostuff.compiler.CompilerMain; + +/** + * Compiles proto files to java/gwt/etc. + * + * @author Dan Häberlein + * @created May 08, 2013 + * @goal compile-directory + * @phase generate-sources + * @requiresDependencyResolution runtime + */ +public class ProtoCompilerDirMojo extends AbstractMojo { + + /** + * The current Maven project. + * + * @parameter default-value="${project}" + * @readonly + * @required + * @since 1.0.1 + */ + private MavenProject project; + + /** + * When {@code true}, skip the execution. + * + * @parameter expression="${protostuff.compiler.skip}" default-value="false" + * @since 1.0.1 + */ + private boolean skip; + + /** + * Usually most of protostuff mojos will not get executed on parent poms + * (i.e. projects with packaging type 'pom'). Setting this parameter to + * {@code true} will force the execution of this mojo, even if it would + * usually get skipped in this case. + * + * @parameter expression="${protostuff.compiler.force}" + * default-value="false" + * @required + * @since 1.0.1 + */ + private boolean forceMojoExecution; + + /** + * Config for using a directory which contains .proto files. They + * will be compiled into outputBaseDir. + * + * @parameter + * @since 1.0.8 + */ + private ProtoDirConfig protoDirectory; + + /** + * If not specified, the directory + * "${project.build.directory}/generated-sources/protostuff" will be used as + * its base dir. + * + * This is only relevent when {@link #modulesFile is provided}. + * + * @parameter + * @since 1.0.8 + */ + private File outputBaseDir; + + public void execute() throws MojoExecutionException, MojoFailureException { + if (!skipMojo()) { + try { + setDefaultOutputDirectory(); + initDirectoryListWhenEmpty(); + createProtobufsUsingDirs(); + } catch (Exception e) { + throw new MojoExecutionException(e.getMessage(), e); + } + } + } + + private void setDefaultOutputDirectory() { + if (outputBaseDir == null) + outputBaseDir = new File(project.getBuild().getDirectory() + "/generated-sources/protostuff"); + } + + private void initDirectoryListWhenEmpty() { + boolean pathDirIsNull = protoDirectory.getPathToProtoDirectories() == null; + if (pathDirIsNull) { + protoDirectory.setPathToProtoDirectories(Collections.emptyList()); + } + } + + private void createProtobufsUsingDirs() throws Exception { + List pathToProtoDirectories = protoDirectory.getPathToProtoDirectories(); + for (String currentPathToProto : pathToProtoDirectories) { + File currentFileHandlerToProto = new File(currentPathToProto); + boolean exists = currentFileHandlerToProto.exists(); + boolean isDir = currentFileHandlerToProto.isDirectory(); + if (exists && isDir) { + compileContainingProtoFiles(currentFileHandlerToProto); + } else { + printWarnMessage(currentFileHandlerToProto, exists, isDir); + } + } + } + + private void compileContainingProtoFiles(File currentMojoDir) throws Exception { + File[] filesList = currentMojoDir.listFiles(new ProtoFileFilter()); + for (File currentProtoFile : filesList) { + com.dyuproject.protostuff.compiler.ProtoModule module = new com.dyuproject.protostuff.compiler.ProtoModule(currentProtoFile, + protoDirectory.getOutput(), "UTF-8", outputBaseDir); + CompilerMain.compile(module); + } + } + + private void printWarnMessage(File currentMojoDir, boolean exists, boolean isDir) throws IOException { + StringBuilder warnMessageSb = new StringBuilder(); + warnMessageSb.append("given path "); + warnMessageSb.append(currentMojoDir.getCanonicalPath().toString()); + warnMessageSb.append(" is not valid. reason:\n"); + warnMessageSb.append("exits " + exists); + warnMessageSb.append("\nis directory " + isDir); + getLog().warn(warnMessageSb.toString()); + } + + /** + *

+ * Determine if the mojo execution should get skipped. + *

+ * This is the case if: + *
    + *
  • {@link #skip} is true
  • + *
  • if the mojo gets executed on a project with packaging type 'pom' and + * {@link #forceMojoExecution} is false
  • + *
+ * + * @return true if the mojo execution should be skipped. + * @since 1.0.1 + */ + protected boolean skipMojo() { + if (skip) { + getLog().info("Skipping protostuff mojo execution"); + return true; + } + + if (!forceMojoExecution && project != null && "pom".equals(project.getPackaging())) { + getLog().info("Skipping protostuff mojo execution for project with packaging type 'pom'"); + return true; + } + + return false; + } + + private static class ProtoFileFilter implements FileFilter { + + private static final String PROTOBUF_FILE_EXTENTION = ".proto"; + + public boolean accept(File pathname) { + return pathname.getName().endsWith(PROTOBUF_FILE_EXTENTION); + } + } +} diff --git a/protostuff-maven-plugin/src/main/java/com/dyuproject/protostuff/mojo/ProtoDirConfig.java b/protostuff-maven-plugin/src/main/java/com/dyuproject/protostuff/mojo/ProtoDirConfig.java new file mode 100644 index 00000000..fb154055 --- /dev/null +++ b/protostuff-maven-plugin/src/main/java/com/dyuproject/protostuff/mojo/ProtoDirConfig.java @@ -0,0 +1,30 @@ +package com.dyuproject.protostuff.mojo; + +import java.io.Serializable; +import java.util.List; + +public class ProtoDirConfig implements Serializable { + + private static final long serialVersionUID = 5547658091474270213L; + + private List pathToProtoDirectories; + private String output = "java_bean"; + + public List getPathToProtoDirectories() { + return pathToProtoDirectories; + } + + public void setPathToProtoDirectories(List pathToProtoDirectories) { + this.pathToProtoDirectories = pathToProtoDirectories; + } + + public String getOutput() { + return output; + } + + public void setOutput(String output) { + this.output = output; + } + + public ProtoDirConfig() {} +} diff --git a/protostuff-maven-plugin/src/test/java/com/dyuproject/protostuff/mojo/TestProtoCompilerMojo.java b/protostuff-maven-plugin/src/test/java/com/dyuproject/protostuff/mojo/TestProtoCompilerMojo.java new file mode 100644 index 00000000..70785fb5 --- /dev/null +++ b/protostuff-maven-plugin/src/test/java/com/dyuproject/protostuff/mojo/TestProtoCompilerMojo.java @@ -0,0 +1,168 @@ +package com.dyuproject.protostuff.mojo; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugin.testing.AbstractMojoTestCase; + +import com.dyuproject.protostuff.mojo.stubs.ProtostuffMavenPluginStub; + +public class TestProtoCompilerMojo extends AbstractMojoTestCase { + + private static final String DEFAULT_BASEDIR = "./target/test-harness/protostuff"; + private static final String POM_FIXTURE_DIR = "src/test/resources/project-to-test"; + private static final String GOAL = "compile-directory"; + + private String FILE_SEPARATOR = System.getProperty("file.separator"); + + private File fileHandlerWorkingDir; + + @Override + protected void setUp() throws Exception { + super.setUp(); + fileHandlerWorkingDir = new File(DEFAULT_BASEDIR); + } + + @Override + protected void tearDown() throws Exception { + super.tearDown(); + try { + deleteFromDirRecursively(fileHandlerWorkingDir); + } catch (Exception e) { + System.err.println("cant't delete files"); + e.printStackTrace(); + } + + } + + private void deleteFromDirRecursively(File fileHandlerWorkingDir) { + if (fileHandlerWorkingDir != null && fileHandlerWorkingDir.exists() && fileHandlerWorkingDir.isDirectory()) { + File[] folderContent = fileHandlerWorkingDir.listFiles(); + for (File currentFile : folderContent) { + if (currentFile.isFile()) { + currentFile.delete(); + } else { + deleteFromDirRecursively(currentFile); + currentFile.delete(); + } + } + } + } + + /* + * Tests + */ + + // protoDirectory Tests + + public void testBuildingProtosFromMultipleDirectories() throws Exception { + executeMavenPlugin(POM_FIXTURE_DIR + "/proto_directory/multiple_dir/pom.xml"); + Set expectedPaths = new HashSet(); + expectedPaths.add(createFileSeparatorString("protostuff", "com","example","foo","Person.java")); + expectedPaths.add(createFileSeparatorString("protostuff", "com","example","tutorial","AddressBook.java")); + expectedPaths.add(createFileSeparatorString("protostuff", "com","example","tutorial","Name.java")); + expectedPaths.add(createFileSeparatorString("protostuff", "com","example","tutorial","Person.java")); + checkGeneratedClasses(expectedPaths); + } + + public void testBuildingProtosUsingDifferentOutputDirectory() throws Exception { + executeMavenPlugin(POM_FIXTURE_DIR + "/proto_directory/default_dir/pom.xml"); + Set expectedPaths = new HashSet(); + expectedPaths.add(createFileSeparatorString("target", "generated-sources", "protostuff", "com","example","foo","Person.java")); + expectedPaths.add(createFileSeparatorString("target", "generated-sources", "protostuff", "com","example","tutorial","AddressBook.java")); + expectedPaths.add(createFileSeparatorString("target", "generated-sources", "protostuff", "com","example","tutorial","Name.java")); + expectedPaths.add(createFileSeparatorString("target", "generated-sources", "protostuff", "com","example","tutorial","Person.java")); + checkGeneratedClasses(expectedPaths); + } + + public void testBuildingProtosUsingDefaultOutput() throws Exception { + executeMavenPlugin(POM_FIXTURE_DIR + "/proto_directory/default_output/pom.xml"); + Set expectedPaths = new HashSet(); + expectedPaths.add(createFileSeparatorString("com","example","foo","Person.java")); + checkGeneratedClasses(expectedPaths); + } + + public void testBuildingProtosWithoutInput() throws Exception { + executeMavenPlugin(POM_FIXTURE_DIR + "/proto_directory/empty_proto_directory/pom.xml"); + List files = getFilesFromDirs(fileHandlerWorkingDir); + assertTrue(files.isEmpty()); + } + + private void executeMavenPlugin(String pomLocation) throws Exception, MojoExecutionException, MojoFailureException { + executeMavenPlugin(pomLocation, DEFAULT_BASEDIR); + } + + private void executeMavenPlugin(String pomLocation, String baseDir) throws Exception, MojoExecutionException, MojoFailureException { + File pom = getTestFile(pomLocation); + assertNotNull(pom); + assertTrue(pom.exists()); + ProtoCompilerDirMojo pcMojo = (ProtoCompilerDirMojo) lookupMojo(GOAL, pom); + setVariableValueToObject(pcMojo, "project", new ProtostuffMavenPluginStub(pomLocation, baseDir)); + assertNotNull(pcMojo); + pcMojo.execute(); + } + + private String createFileSeparatorString(String... values) { + StringBuilder sb = new StringBuilder(""); + if (values.length > 0) { + for (int i = 0; i < values.length - 1; i++) { + sb.append(values[i]).append(FILE_SEPARATOR); + } + sb.append(values[values.length - 1]); + } + return sb.toString(); + } + + private void checkGeneratedClasses(Set expectedPaths) throws IOException{ + checkGeneratedClasses(expectedPaths, fileHandlerWorkingDir); + } + + private void checkGeneratedClasses(Set expectedPaths, File fileHandler) throws IOException { + List createdJavaClasses = getFilesFromDirs(fileHandler); + int i = 0; + for (; i < createdJavaClasses.size(); i++) { + String pathToCreatedJavaFile = createdJavaClasses.get(i).getCanonicalPath(); + assertTrue(pathToCreatedJavaFile + "was not created during the build process", valueContainedInPrefixSet(expectedPaths, pathToCreatedJavaFile)); + } + assertEquals(expectedPaths.size(), i); + } + + private List getFilesFromDirs(File fileHandlerWorkingDir) { + List foundFiles = new ArrayList(); + if (fileHandlerWorkingDir != null && fileHandlerWorkingDir.exists()) { + if (fileHandlerWorkingDir.isDirectory()) { + File[] folderContent = fileHandlerWorkingDir.listFiles(); + for (File currentFile : folderContent) { + if (currentFile.isFile()) { + foundFiles.add(currentFile); + } else { + foundFiles.addAll(getFilesFromDirs(currentFile)); + } + } + + } else { + foundFiles.add(fileHandlerWorkingDir); + } + } + return foundFiles; + } + + private boolean valueContainedInPrefixSet(Set prefixValues, String value){ + boolean found = false; + if(prefixValues != null) + for(String currentPrefix : prefixValues){ + if(value.endsWith(currentPrefix)){ + found = true; + break; + } + } + return found; + } + +} diff --git a/protostuff-maven-plugin/src/test/java/com/dyuproject/protostuff/mojo/stubs/ProtostuffMavenPluginStub.java b/protostuff-maven-plugin/src/test/java/com/dyuproject/protostuff/mojo/stubs/ProtostuffMavenPluginStub.java new file mode 100644 index 00000000..1782c7b3 --- /dev/null +++ b/protostuff-maven-plugin/src/test/java/com/dyuproject/protostuff/mojo/stubs/ProtostuffMavenPluginStub.java @@ -0,0 +1,91 @@ +package com.dyuproject.protostuff.mojo.stubs; + +import java.io.File; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import org.apache.maven.model.Build; +import org.apache.maven.model.Model; +import org.apache.maven.model.io.xpp3.MavenXpp3Reader; +import org.apache.maven.plugin.testing.stubs.MavenProjectStub; +import org.apache.maven.settings.Settings; +import org.codehaus.plexus.util.ReaderFactory; + +public class ProtostuffMavenPluginStub extends MavenProjectStub { + + private File baseDir; + private Model model; + private String pomLocation; + + public ProtostuffMavenPluginStub(String pomLocation, String baseDir) { + this.pomLocation = pomLocation; + this.baseDir = new File(baseDir); + model = createPomModel(); + setProjectInfos(); + setDirectoryPaths(); + setSrcPaths(); + } + + private Model createPomModel() { + Model model; + try { + MavenXpp3Reader pomReader = new MavenXpp3Reader(); + model = pomReader.read(ReaderFactory.newXmlReader(new File(pomLocation))); + setModel(model); + } catch (Exception e) { + throw new RuntimeException(e); + } + return model; + } + + private void setProjectInfos() { + setGroupId(model.getGroupId()); + setArtifactId(model.getArtifactId()); + setVersion(model.getVersion()); + setName(model.getName()); + setUrl(model.getUrl()); + setPackaging(model.getPackaging()); + } + + private void setDirectoryPaths() { + Build build = new Build(); + build.setFinalName(model.getArtifactId()); + build.setDirectory(getBasedir() + "/target"); + build.setSourceDirectory(getBasedir() + "/src/main/java"); + build.setOutputDirectory(getBasedir() + "/target/classes"); + build.setTestSourceDirectory(getBasedir() + "/src/test/java"); + build.setTestOutputDirectory(getBasedir() + "/target/test-classes"); + model.setBuild(build); + } + + private void setSrcPaths() { + List compileSourceRoots = new ArrayList(); + compileSourceRoots.add(getBasedir() + "/src/main/java"); + setCompileSourceRoots(compileSourceRoots); + + List testCompileSourceRoots = new ArrayList(); + testCompileSourceRoots.add(getBasedir() + "/src/test/java"); + setTestCompileSourceRoots(testCompileSourceRoots); + } + + @Override + public File getBasedir() { + return baseDir; + } + + @Override + public Build getBuild() { + return model.getBuild(); + } + + public class SettingsStub extends Settings { + + private static final long serialVersionUID = 8210937661580151495L; + + /** {@inheritDoc} */ + public List getProxies() { + return Collections.EMPTY_LIST; + } + } +} diff --git a/protostuff-maven-plugin/src/test/resources/project-to-test/proto_directory/default_dir/pom.xml b/protostuff-maven-plugin/src/test/resources/project-to-test/proto_directory/default_dir/pom.xml new file mode 100644 index 00000000..9f23dff1 --- /dev/null +++ b/protostuff-maven-plugin/src/test/resources/project-to-test/proto_directory/default_dir/pom.xml @@ -0,0 +1,36 @@ + + 4.0.0 + protostuff-maven-plugin-test-1 + com.dyuproject.protostuff + test generate proto classes using a set of directories + 0.0.1-SNAPSHOT + jar + + + junit + junit + 4.11 + test + + + + + + protostuff-maven-plugin + com.dyuproject.protostuff + 1.0.9-SNAPSHOT + + + + ./src/test/resources/proto/fixture1 + ./src/test/resources/proto/fixture2 + + java_bean + + + + + + \ No newline at end of file diff --git a/protostuff-maven-plugin/src/test/resources/project-to-test/proto_directory/default_output/pom.xml b/protostuff-maven-plugin/src/test/resources/project-to-test/proto_directory/default_output/pom.xml new file mode 100644 index 00000000..7eaba948 --- /dev/null +++ b/protostuff-maven-plugin/src/test/resources/project-to-test/proto_directory/default_output/pom.xml @@ -0,0 +1,35 @@ + + 4.0.0 + protostuff-maven-plugin-test-1 + com.dyuproject.protostuff + test generate proto classes using a set of directories + 0.0.1-SNAPSHOT + jar + + + junit + junit + 4.11 + test + + + + + + protostuff-maven-plugin + com.dyuproject.protostuff + 1.0.9-SNAPSHOT + + target/test-harness/protostuff + + + ./src/test/resources/proto/fixture1 + + + + + + + \ No newline at end of file diff --git a/protostuff-maven-plugin/src/test/resources/project-to-test/proto_directory/empty_proto_directory/pom.xml b/protostuff-maven-plugin/src/test/resources/project-to-test/proto_directory/empty_proto_directory/pom.xml new file mode 100644 index 00000000..8ece5a02 --- /dev/null +++ b/protostuff-maven-plugin/src/test/resources/project-to-test/proto_directory/empty_proto_directory/pom.xml @@ -0,0 +1,35 @@ + + 4.0.0 + protostuff-maven-plugin-test-1 + com.dyuproject.protostuff + test generate proto classes using a set of directories + 0.0.1-SNAPSHOT + jar + + + junit + junit + 4.11 + test + + + + + + protostuff-maven-plugin + com.dyuproject.protostuff + 1.0.9-SNAPSHOT + + target/test-harness/protostuff + + + + java_bean + + + + + + \ No newline at end of file diff --git a/protostuff-maven-plugin/src/test/resources/project-to-test/proto_directory/multiple_dir/pom.xml b/protostuff-maven-plugin/src/test/resources/project-to-test/proto_directory/multiple_dir/pom.xml new file mode 100644 index 00000000..77c78989 --- /dev/null +++ b/protostuff-maven-plugin/src/test/resources/project-to-test/proto_directory/multiple_dir/pom.xml @@ -0,0 +1,37 @@ + + 4.0.0 + protostuff-maven-plugin-test-1 + com.dyuproject.protostuff + test generate proto classes using a set of directories + 0.0.1-SNAPSHOT + jar + + + junit + junit + 4.11 + test + + + + + + protostuff-maven-plugin + com.dyuproject.protostuff + 1.0.9-SNAPSHOT + + target/test-harness/protostuff + + + ./src/test/resources/proto/fixture1 + ./src/test/resources/proto/fixture2 + + java_bean + + + + + + \ No newline at end of file diff --git a/protostuff-maven-plugin/src/test/resources/proto/fixture1/foo.proto b/protostuff-maven-plugin/src/test/resources/proto/fixture1/foo.proto new file mode 100644 index 00000000..d35ddd57 --- /dev/null +++ b/protostuff-maven-plugin/src/test/resources/proto/fixture1/foo.proto @@ -0,0 +1,15 @@ +package foo; + +option optimize_for = LITE_RUNTIME; +option java_package = "com.example.foo"; + +message Person { + required int32 id = 1; + optional string name = 2; + optional string motto = 3 [default="When the cat is away, the mouse is alone!"]; + enum Gender { + MALE = 1; + FEMALE = 2; + } + optional Gender gender = 4; +} \ No newline at end of file diff --git a/protostuff-maven-plugin/src/test/resources/proto/fixture1/test.txt b/protostuff-maven-plugin/src/test/resources/proto/fixture1/test.txt new file mode 100644 index 00000000..d9f35882 --- /dev/null +++ b/protostuff-maven-plugin/src/test/resources/proto/fixture1/test.txt @@ -0,0 +1 @@ +no proto file - fixture for unit tests \ No newline at end of file diff --git a/protostuff-maven-plugin/src/test/resources/proto/fixture2/name.proto b/protostuff-maven-plugin/src/test/resources/proto/fixture2/name.proto new file mode 100644 index 00000000..206f716c --- /dev/null +++ b/protostuff-maven-plugin/src/test/resources/proto/fixture2/name.proto @@ -0,0 +1,10 @@ +package tutorial; + +option java_package = "com.example.tutorial"; +option java_outer_classname = "NamePojo"; + +message Name { + required string firstName = 1; + required string lastName = 2; + optional string middleName = 3; +} diff --git a/protostuff-maven-plugin/src/test/resources/proto/fixture2/person.proto b/protostuff-maven-plugin/src/test/resources/proto/fixture2/person.proto new file mode 100644 index 00000000..3287519b --- /dev/null +++ b/protostuff-maven-plugin/src/test/resources/proto/fixture2/person.proto @@ -0,0 +1,27 @@ +package tutorial; + +option java_package = "com.example.tutorial"; +option java_outer_classname = "AddressBookProtos"; + +message Person { + required string name = 1; + required int32 id = 2; + optional string email = 3; + + enum PhoneType { + MOBILE = 0; + HOME = 1; + WORK = 2; + } + + message PhoneNumber { + required string number = 1; + optional PhoneType type = 2 [default = HOME]; + } + + repeated PhoneNumber phone = 4; +} + +message AddressBook { + repeated Person person = 1; +} \ No newline at end of file From 145b46a7eea19143c2d336ed3725839605faf04d Mon Sep 17 00:00:00 2001 From: Dan Haeberlein Date: Mon, 3 Jun 2013 17:02:19 +0200 Subject: [PATCH 2/2] added recursive flag for search for proto files also in subdirectories --- .../protostuff/mojo/ProtoCompilerDirMojo.java | 40 ++++++++++++------ .../protostuff/mojo/ProtoDirConfig.java | 20 +++++++++ .../mojo/TestProtoCompilerMojo.java | 41 +++++++++++++------ .../recursive_option/pom_non_rec.xml | 36 ++++++++++++++++ .../recursive_option/pom_rec.xml | 37 +++++++++++++++++ .../resources/proto/fixture3/person.proto | 27 ++++++++++++ .../proto/fixture3/subfolder/name.proto | 10 +++++ 7 files changed, 186 insertions(+), 25 deletions(-) create mode 100644 protostuff-maven-plugin/src/test/resources/project-to-test/proto_directory/recursive_option/pom_non_rec.xml create mode 100644 protostuff-maven-plugin/src/test/resources/project-to-test/proto_directory/recursive_option/pom_rec.xml create mode 100644 protostuff-maven-plugin/src/test/resources/proto/fixture3/person.proto create mode 100644 protostuff-maven-plugin/src/test/resources/proto/fixture3/subfolder/name.proto diff --git a/protostuff-maven-plugin/src/main/java/com/dyuproject/protostuff/mojo/ProtoCompilerDirMojo.java b/protostuff-maven-plugin/src/main/java/com/dyuproject/protostuff/mojo/ProtoCompilerDirMojo.java index d15ab376..545f84a7 100644 --- a/protostuff-maven-plugin/src/main/java/com/dyuproject/protostuff/mojo/ProtoCompilerDirMojo.java +++ b/protostuff-maven-plugin/src/main/java/com/dyuproject/protostuff/mojo/ProtoCompilerDirMojo.java @@ -39,6 +39,17 @@ */ public class ProtoCompilerDirMojo extends AbstractMojo { + private static class ProtoFileFilter implements FileFilter { + + private static final String PROTOBUF_FILE_EXTENTION = ".proto"; + + public boolean accept(File pathname) { + return pathname.getName().endsWith(PROTOBUF_FILE_EXTENTION); + } + } + + private static FileFilter PROTOC_FILE_FILTER = new ProtoFileFilter(); + /** * The current Maven project. * @@ -130,14 +141,25 @@ private void createProtobufsUsingDirs() throws Exception { } private void compileContainingProtoFiles(File currentMojoDir) throws Exception { - File[] filesList = currentMojoDir.listFiles(new ProtoFileFilter()); - for (File currentProtoFile : filesList) { - com.dyuproject.protostuff.compiler.ProtoModule module = new com.dyuproject.protostuff.compiler.ProtoModule(currentProtoFile, - protoDirectory.getOutput(), "UTF-8", outputBaseDir); - CompilerMain.compile(module); + File[] filesList = currentMojoDir.listFiles(); + for (File currentFileHandler : filesList) { + if(currentFileHandler.isDirectory()){ + if(protoDirectory.isRecursive()){ + compileContainingProtoFiles(currentFileHandler); + } + } else if(currentFileHandler.isFile() && PROTOC_FILE_FILTER.accept(currentFileHandler)){ + compileProtoFile(currentFileHandler); + } } } + private void compileProtoFile(File currentFileHandler) throws Exception { + com.dyuproject.protostuff.compiler.ProtoModule module = new com.dyuproject.protostuff.compiler.ProtoModule(currentFileHandler, + protoDirectory.getOutput(), + "UTF-8", outputBaseDir); + CompilerMain.compile(module); + } + private void printWarnMessage(File currentMojoDir, boolean exists, boolean isDir) throws IOException { StringBuilder warnMessageSb = new StringBuilder(); warnMessageSb.append("given path "); @@ -176,12 +198,4 @@ protected boolean skipMojo() { return false; } - private static class ProtoFileFilter implements FileFilter { - - private static final String PROTOBUF_FILE_EXTENTION = ".proto"; - - public boolean accept(File pathname) { - return pathname.getName().endsWith(PROTOBUF_FILE_EXTENTION); - } - } } diff --git a/protostuff-maven-plugin/src/main/java/com/dyuproject/protostuff/mojo/ProtoDirConfig.java b/protostuff-maven-plugin/src/main/java/com/dyuproject/protostuff/mojo/ProtoDirConfig.java index fb154055..7920d702 100644 --- a/protostuff-maven-plugin/src/main/java/com/dyuproject/protostuff/mojo/ProtoDirConfig.java +++ b/protostuff-maven-plugin/src/main/java/com/dyuproject/protostuff/mojo/ProtoDirConfig.java @@ -7,9 +7,21 @@ public class ProtoDirConfig implements Serializable { private static final long serialVersionUID = 5547658091474270213L; + /** + * searchablePaths + */ private List pathToProtoDirectories; + + /** + * Output format of the protostuff compiler + */ private String output = "java_bean"; + /** + * Search for .proto files in subdirs (default=false) + */ + private boolean recursive; + public List getPathToProtoDirectories() { return pathToProtoDirectories; } @@ -26,5 +38,13 @@ public void setOutput(String output) { this.output = output; } + public boolean isRecursive() { + return recursive; + } + + public void setRecursive(boolean recursive) { + this.recursive = recursive; + } + public ProtoDirConfig() {} } diff --git a/protostuff-maven-plugin/src/test/java/com/dyuproject/protostuff/mojo/TestProtoCompilerMojo.java b/protostuff-maven-plugin/src/test/java/com/dyuproject/protostuff/mojo/TestProtoCompilerMojo.java index 70785fb5..b618c37f 100644 --- a/protostuff-maven-plugin/src/test/java/com/dyuproject/protostuff/mojo/TestProtoCompilerMojo.java +++ b/protostuff-maven-plugin/src/test/java/com/dyuproject/protostuff/mojo/TestProtoCompilerMojo.java @@ -94,6 +94,23 @@ public void testBuildingProtosWithoutInput() throws Exception { assertTrue(files.isEmpty()); } + public void testRecursive() throws Exception { + executeMavenPlugin(POM_FIXTURE_DIR + "/proto_directory/recursive_option/pom_rec.xml"); + Set expectedPaths = new HashSet(); + expectedPaths.add(createFileSeparatorString("protostuff", "com","example","tutorial","AddressBook.java")); + expectedPaths.add(createFileSeparatorString("protostuff", "com","example","tutorial","Name.java")); + expectedPaths.add(createFileSeparatorString("protostuff", "com","example","tutorial","Person.java")); + checkGeneratedClasses(expectedPaths); + } + + public void testNonRecursive() throws Exception { + executeMavenPlugin(POM_FIXTURE_DIR + "/proto_directory/recursive_option/pom_non_rec.xml"); + Set expectedPaths = new HashSet(); + expectedPaths.add(createFileSeparatorString("protostuff", "com","example","tutorial","AddressBook.java")); + expectedPaths.add(createFileSeparatorString("protostuff", "com","example","tutorial","Person.java")); + checkGeneratedClasses(expectedPaths); + } + private void executeMavenPlugin(String pomLocation) throws Exception, MojoExecutionException, MojoFailureException { executeMavenPlugin(pomLocation, DEFAULT_BASEDIR); } @@ -133,6 +150,18 @@ private void checkGeneratedClasses(Set expectedPaths, File fileHandler) assertEquals(expectedPaths.size(), i); } + private boolean valueContainedInPrefixSet(Set prefixValues, String value){ + boolean found = false; + if(prefixValues != null) + for(String currentPrefix : prefixValues){ + if(value.endsWith(currentPrefix)){ + found = true; + break; + } + } + return found; + } + private List getFilesFromDirs(File fileHandlerWorkingDir) { List foundFiles = new ArrayList(); if (fileHandlerWorkingDir != null && fileHandlerWorkingDir.exists()) { @@ -153,16 +182,4 @@ private List getFilesFromDirs(File fileHandlerWorkingDir) { return foundFiles; } - private boolean valueContainedInPrefixSet(Set prefixValues, String value){ - boolean found = false; - if(prefixValues != null) - for(String currentPrefix : prefixValues){ - if(value.endsWith(currentPrefix)){ - found = true; - break; - } - } - return found; - } - } diff --git a/protostuff-maven-plugin/src/test/resources/project-to-test/proto_directory/recursive_option/pom_non_rec.xml b/protostuff-maven-plugin/src/test/resources/project-to-test/proto_directory/recursive_option/pom_non_rec.xml new file mode 100644 index 00000000..9512544c --- /dev/null +++ b/protostuff-maven-plugin/src/test/resources/project-to-test/proto_directory/recursive_option/pom_non_rec.xml @@ -0,0 +1,36 @@ + + 4.0.0 + protostuff-maven-plugin-test-1 + com.dyuproject.protostuff + test generate proto classes using a set of directories + 0.0.1-SNAPSHOT + jar + + + junit + junit + 4.11 + test + + + + + + protostuff-maven-plugin + com.dyuproject.protostuff + 1.0.9-SNAPSHOT + + target/test-harness/protostuff + + + ./src/test/resources/proto/fixture3 + + java_bean + + + + + + \ No newline at end of file diff --git a/protostuff-maven-plugin/src/test/resources/project-to-test/proto_directory/recursive_option/pom_rec.xml b/protostuff-maven-plugin/src/test/resources/project-to-test/proto_directory/recursive_option/pom_rec.xml new file mode 100644 index 00000000..be339b99 --- /dev/null +++ b/protostuff-maven-plugin/src/test/resources/project-to-test/proto_directory/recursive_option/pom_rec.xml @@ -0,0 +1,37 @@ + + 4.0.0 + protostuff-maven-plugin-test-1 + com.dyuproject.protostuff + test generate proto classes using a set of directories + 0.0.1-SNAPSHOT + jar + + + junit + junit + 4.11 + test + + + + + + protostuff-maven-plugin + com.dyuproject.protostuff + 1.0.9-SNAPSHOT + + target/test-harness/protostuff + + + ./src/test/resources/proto/fixture3 + + java_bean + true + + + + + + \ No newline at end of file diff --git a/protostuff-maven-plugin/src/test/resources/proto/fixture3/person.proto b/protostuff-maven-plugin/src/test/resources/proto/fixture3/person.proto new file mode 100644 index 00000000..3287519b --- /dev/null +++ b/protostuff-maven-plugin/src/test/resources/proto/fixture3/person.proto @@ -0,0 +1,27 @@ +package tutorial; + +option java_package = "com.example.tutorial"; +option java_outer_classname = "AddressBookProtos"; + +message Person { + required string name = 1; + required int32 id = 2; + optional string email = 3; + + enum PhoneType { + MOBILE = 0; + HOME = 1; + WORK = 2; + } + + message PhoneNumber { + required string number = 1; + optional PhoneType type = 2 [default = HOME]; + } + + repeated PhoneNumber phone = 4; +} + +message AddressBook { + repeated Person person = 1; +} \ No newline at end of file diff --git a/protostuff-maven-plugin/src/test/resources/proto/fixture3/subfolder/name.proto b/protostuff-maven-plugin/src/test/resources/proto/fixture3/subfolder/name.proto new file mode 100644 index 00000000..206f716c --- /dev/null +++ b/protostuff-maven-plugin/src/test/resources/proto/fixture3/subfolder/name.proto @@ -0,0 +1,10 @@ +package tutorial; + +option java_package = "com.example.tutorial"; +option java_outer_classname = "NamePojo"; + +message Name { + required string firstName = 1; + required string lastName = 2; + optional string middleName = 3; +}