【问题标题】:Are there any ways to generate Scala code from Protobuf files in a Maven build?有什么方法可以在 Maven 构建中从 Protobuf 文件生成 Scala 代码?
【发布时间】:2013-11-10 09:11:22
【问题描述】:

我正在寻找一个包含所有这 3 件事的解决方案。到目前为止,我已经能够找到可以在构建期间从 proto 文件生成 Java 代码的 Maven 插件,以及可以从 proto 文件生成 Scala 代码的命令行工具,但没有任何东西可以将所有内容混合在一起。

到目前为止,我发现的最有前途的东西是 ScalaBuff,事实上它存在于 Maven 存储库中。如果我因此将其添加为依赖项...

    <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>

...有没有什么方法可以让 Maven 构建在构建阶段以命令行工具的形式启动它? (希望生成源)我也找到了这个,但不知道如何让它们很好地结合在一起:Maven:http://mojo.codehaus.org/exec-maven-plugin/

注意:我真的希望它是可移植的,而不是依赖于我本地机器上安装的东西,但是非常欢迎黑客攻击(即添加一个 jar 或可执行文件到源代码控制)

提前致谢!

更新:

除了上面的依赖之外,如果我添加以下...

    <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>

...我可以在构建期间生成源代码(在 generate-sources 阶段),但是由于某种原因在运行 exec 插件后构建立即停止。很近!如果有人能解决最后一个问题,这将得到解决。

【问题讨论】:

  • 我不认为 sbt 是可以接受的? github.com/gseitz/sbt-protobuf/tree/scalabuff ...如果你真的想坚持使用 Maven,你可以配置 Maven 来启动一个 sbt 构建。
  • 我必须坚持使用 Maven - 这不是一个选择。似乎仍然可以让 Maven 使用 exec 插件调用 ScalaBuff 编译器,但我想我也可以尝试让 Maven 调用准系统 SBT 构建以使用该插件。
  • 请您将您的解决方案添加为答案并将其标记为已接受?我同意 exec 插件是正确的解决方法,正如您所发现的那样。

标签: scala maven protocol-buffers


【解决方案1】:

已解决:maven exec 插件 java 目标没有分叉,因此 scalabuff 编译器中的 exit(0) 导致整个构建退出。还必须在 generate-sources 之前创建一个目录以使 ScalaBuff 满意。使用 ScalaBuff 依赖项和以下插件确实有效!:

<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>

【讨论】:

  • 你在 github 上有一个示例项目可以使用这个你可以分享吗?
猜你喜欢
  • 2016-04-07
  • 2014-05-20
  • 2015-11-06
  • 1970-01-01
  • 1970-01-01
  • 2011-09-05
  • 1970-01-01
  • 2019-08-26
  • 2011-05-10
相关资源
最近更新 更多