在Idea中打造比较舒服的Protobuf开发环境

安装Protobuf语法高亮插件

插件使用:Protobuf Support,安装重启后即可实现语法高亮。

配置Maven插件

首先,proto文件需要放置在src.main.proto目录下,其次对Maven进行如下配置:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

<build>
    <extensions>
        <extension>
            <groupId>kr.motd.maven</groupId>
            <artifactId>os-maven-plugin</artifactId>
            <version>1.7.0</version>
        </extension>
    </extensions>
    <plugins>
        <plugin>
            <groupId>org.xolstice.maven.plugins</groupId>
            <artifactId>protobuf-maven-plugin</artifactId>
            <version>0.6.1</version>
            <extensions>true</extensions>
            <configuration>
                <protocArtifact>
                    com.google.protobuf:protoc:${j.protobuf-java.version}:exe:${os.detected.classifier}
                </protocArtifact>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                        <goal>test-compile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

os-maven-plugin插件用于设置各种有用的属性,我对此的理解就是相当于设置了一些properties标签。

配置完成后就可以在target中找到生成的字节码文件:

2021-07-28-15-07-06

遇到的问题

  1. 遇到如下报错:

Failed to execute goal org.xolstice.maven.plugins:protobuf-maven-plugin:0.6.1:compile (default) on project Netty: protoc did not exit cleanly. Review output for more information.

我解决该问题的方式是,检查自己的proto文件,删除语法错误的proto文件(本次实验中是两个文件的java_outer_classname设置成一样的了)

随着对portobuf较深入的了解,我发现protobuf貌似不允许同一个命名空间存在两个相同的message。

  1. 貌似不会生成相应的源码

我增加了如下的配置(还导致我一些代码丢失,找都找不会了,头疼):

1
2
3
4

<outputDirectory>${project.build.sourceDirectory}</outputDirectory>
<clearOutputDirectory>false</clearOutputDirectory>

我现在很好奇,我该如何查看一个插件支持哪些配置?

  1. 我新一个项目,进行如此配置后,报如下错误,此时我旧项目也会报该错误:

[ERROR] Failed to execute goal org.xolstice.maven.plugins:protobuf-maven-plugin:0.6.1:compile (default) on project Netty: Unable to resolve artifact: Missing:
[ERROR] ----------
[ERROR] 1) com.google.protobuf2:protoc:exe:windows-x86_64:3.17.3
[ERROR] 
[ERROR]   Try downloading the file manually from the project website.
[ERROR] 
[ERROR]   Then, install it using the command: 
[ERROR]       mvn install:install-file -DgroupId=com.google.protobuf2 -DartifactId=protoc -Dversion=3.17.3 -Dclassifier=windows-x86_64 -Dpackaging=exe -Dfile=/path/to/file
[ERROR] 
[ERROR]   Alternatively, if you host your own repository you can deploy the file there: 
[ERROR]       mvn deploy:deploy-file -DgroupId=com.google.protobuf2 -DartifactId=protoc -Dversion=3.17.3 -Dclassifier=windows-x86_64 -Dpackaging=exe -Dfile=/path/to/file -Durl=[url] -DrepositoryId=[id]
[ERROR] 
[ERROR]   Path to dependency: 
[ERROR]   	1) org.example:Netty:jar:1.0-SNAPSHOT
[ERROR]   	2) com.google.protobuf2:protoc:exe:windows-x86_64:3.17.3
[ERROR] 
[ERROR] ----------
[ERROR] 1 required artifact is missing.
[ERROR] 
[ERROR] for artifact: 
[ERROR]   org.example:Netty:jar:1.0-SNAPSHOT
[ERROR] 
[ERROR] from the specified remote repositories:
[ERROR]   central (https://repo.maven.apache.org/maven2, releases=true, snapshots=false)
[ERROR] 
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

Process finished with exit code 1

经过我的观察,我发现我配置中的com.google.protobuf:protoc:${j.protobuf-java.version}:exe:${os.detected.classifier}被改为了com.google.protobuf2:protoc:${j.protobuf-java.version}:exe:${os.detected.classifier}。之所以会发生这样的改变,是因为我昨天在调整代码文件时用了Shift + F6快捷键,一个包原本为protobuf,我用快捷键改为了protobuf2

2021-07-29-09-54-07

这件事给我提了一个醒,我下次修改代码时一定要防止不小心改动了不该改动的东西(最好找找Idea是否存在相关的解决方案)。

小结

我最终使用的配置如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

<build>
    <extensions>
        <extension>
            <groupId>kr.motd.maven</groupId>
            <artifactId>os-maven-plugin</artifactId>
            <version>1.7.0</version>
        </extension>
    </extensions>
    <plugins>
        <plugin>
            <groupId>org.xolstice.maven.plugins</groupId>
            <artifactId>protobuf-maven-plugin</artifactId>
            <version>0.6.1</version>
            <extensions>true</extensions>
            <configuration>
                <outputDirectory>${project.build.sourceDirectory}</outputDirectory>
                <clearOutputDirectory>false</clearOutputDirectory>
                <protocArtifact>
                    com.google.protobuf:protoc:${j.protobuf-java.version}:exe:${os.detected.classifier}
                </protocArtifact>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                        <goal>test-compile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

参考资料

  1. Intellij IDEA中使用Protobuf的正确姿势
  2. os-maven-plugin Maven 插件
  3. 使用maven自动编译protocol buffer的配置
  4. protobuf + maven 爬坑记