Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added goal 'compile-directory' #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.5</version>
<version>2.9</version>
<configuration>
<excludePackageNames>com.google.*:org.apache.*:org.codehaus.*:javax.*:com.sun.*</excludePackageNames>
<tags>
Expand Down
87 changes: 87 additions & 0 deletions protostuff-maven-plugin/README.md
Original file line number Diff line number Diff line change
@@ -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 <code>compile</code> 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
<configuration>
<protoModules>
<protoModule>
<source>src/main/resources/foo.proto</source>
<outputDir>src/main/java</outputDir>
<output>java_bean</output>
<encoding>UTF-8</encoding>
<options>
<property>
<name>generate_field_map</name>
</property>
</options>
</protoModule>
</protoModules>
</configuration>
```

- or using a property file for the compiler, adding the filepath to the plugin like this:

```xml
<configuration>
<modulesFile>./src/main/resources/config.properties</modulesFile>
</configuration>
```

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 <code>compile-directory</code> goal.
This is how it would look like:

```xml
<executions>
<execution>
<id>generate-protobuf-sources</id>
<phase>generate-sources</phase>
<goals>
<goal>compile-directory</goal>
</goals>
<configuration>
<outputBaseDir>${project.build.generatedSourceOutput}/protostuff</outputBaseDir>
<protoDirectory>
<pathToProtoDirectories>
<pathToProtoDirectory>${project.build.sourceDirectory}/../proto</pathToProtoDirectory>
</pathToProtoDirectories>
<output>java_bean</output>
</protoDirectory>
</configuration>
</execution>
</executions>
```

The outputBaseDir tag points to the root directory, in which the java classes will be created. The default value is <code>${project.build.Directory}/generated-sources/protostuff</code>.
(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 <code>java_bean</code>. 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).
12 changes: 12 additions & 0 deletions protostuff-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,18 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-plugin-testing-harness</artifactId>
<version>1.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
<version>3.0.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.dyuproject.protostuff</groupId>
<artifactId>protostuff-codegen</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
//========================================================================
//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 {

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.
*
* @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.<String>emptyList());
}
}

private void createProtobufsUsingDirs() throws Exception {
List<String> 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();
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 ");
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());
}

/**
* <p>
* Determine if the mojo execution should get skipped.
* </p>
* This is the case if:
* <ul>
* <li>{@link #skip} is <code>true</code></li>
* <li>if the mojo gets executed on a project with packaging type 'pom' and
* {@link #forceMojoExecution} is <code>false</code></li>
* </ul>
*
* @return <code>true</code> 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;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.dyuproject.protostuff.mojo;

import java.io.Serializable;
import java.util.List;

public class ProtoDirConfig implements Serializable {

private static final long serialVersionUID = 5547658091474270213L;

/**
* searchablePaths
*/
private List<String> 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<String> getPathToProtoDirectories() {
return pathToProtoDirectories;
}

public void setPathToProtoDirectories(List<String> pathToProtoDirectories) {
this.pathToProtoDirectories = pathToProtoDirectories;
}

public String getOutput() {
return output;
}

public void setOutput(String output) {
this.output = output;
}

public boolean isRecursive() {
return recursive;
}

public void setRecursive(boolean recursive) {
this.recursive = recursive;
}

public ProtoDirConfig() {}
}
Loading