Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there any ways to generate Scala code from Protobuf files in a Maven build?

I'm looking for a solution that includes all 3 of these things. Thus far, I've been able to find Maven plugins that will generate Java code from proto files during a build, and command line tools that will generate Scala code from proto files, but nothing that mixes everything together.

The most promising thing I've found so far is ScalaBuff, and the fact that it exists in the Maven repos. If I add it as a dependency thusly...

    <dependency>
        <groupId>net.sandrogrzicic</groupId>
        <artifactId>scalabuff-compiler_2.10</artifactId>
        <version>1.3.6</version>
    </dependency>
    <dependency>
        <groupId>net.sandrogrzicic</groupId>
        <artifactId>scalabuff-runtime_2.10</artifactId>
        <version>1.3.6</version>
    </dependency>

...is there any way to simply have the Maven build launch it as a command line util during a phase of the build? (hopefully generate-sources) I also found this, but am at a loss as to how to get these to play nicely together: Maven: http://mojo.codehaus.org/exec-maven-plugin/

Note: I'd really like this to be portable and not depend on something installed on my local box, but hacks are perfectly welcome (i.e. adding a jar or executable to source control)

Thanks in advance!

UPDATE:

In addition to the above dependency, if I add the following...

    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.2.1</version>
        <executions>
            <execution>
                <id>protobuf-sources</id>
                <phase>generate-sources</phase>
                <goals>
                    <goal>java</goal>
                </goals>
            </execution>
        </executions>
        <configuration>
            <mainClass>net.sandrogrzicic.scalabuff.compiler.ScalaBuff</mainClass>
            <arguments>
                <argument>--proto_path=src/main/protobuf</argument>
                <argument>--scala_out=target/generated-sources/scalabuff</argument>
            </arguments>
            <sourceRoot>target/generated-sources/scalabuff</sourceRoot>
        </configuration>
    </plugin>

...I can generate source during my build (in the generate-sources phase), but the build halts immediately after running the exec plugin for some reason. So close! If anyone can fix that last problem, this will be solved.

like image 366
Jonathan Morabito Avatar asked Oct 30 '13 16:10

Jonathan Morabito


1 Answers

SOLVED: The maven exec plugin java goal doesn't fork, so the exit(0) in the scalabuff compiler was causing the whole build to exit. Also had to create a directory before generate-sources to keep ScalaBuff happy. Using the ScalaBuff dependencies and the following plugins actually works!:

<plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.7</version>
    <executions>
        <execution>
            <phase>initialize</phase>
            <configuration>
                <tasks>
                    <mkdir dir="target/generated-sources/scalabuff" />
                </tasks>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.2.1</version>
    <executions>
        <execution>
            <id>protobuf-sources</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>exec</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <executable>java</executable>
        <arguments>
          <argument>-classpath</argument>
          <!-- automatically creates the classpath using all project dependencies,
           also adding the project build directory -->
          <classpath/>
          <argument>net.sandrogrzicic.scalabuff.compiler.ScalaBuff</argument>
            <argument>--proto_path=src/main/protobuf</argument>
            <argument>--scala_out=target/generated-sources/scalabuff</argument>
        </arguments>
        <sourceRoot>target/generated-sources/scalabuff</sourceRoot>
    </configuration>
</plugin>
like image 108
Jonathan Morabito Avatar answered Sep 20 '22 03:09

Jonathan Morabito