![](/img/trans.png)
[英]How to use multiple annotation processors with maven-compiler-plugin
[英]How to use google protobuf compiler with maven-compiler-plugin
我有一個簡單的 java 項目(無彈簧),我在 src/resources 文件夾中有一個 protobuf 文件 test.proto,我想從中生成源。 我在幾個地方讀到,要使用插件,我需要先在本地安裝它。 但是在我之前的 gradle 項目中,我不需要做任何這樣的事情,只需像下面這樣的簡單配置就適用於 gradle:
protobuf {
protoc {
artifact = 'com.google.protobuf:protoc:3.8.0'
}
generateProtoTasks.generatedFilesBaseDir = generatedProtoPath
}
Maven 中有什么類似的東西,如果我想使用谷歌提供的protoc
編譯器並生成代碼而不在我的生產機器上下載和安裝任何東西。
要回答我自己的問題,以便有人覺得它有幫助:
google protobuf 3.7.0: Maven 更改——將此添加到您的 pom.xml 中:
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>3.7.0</version>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java-util</artifactId>
<version>3.7.0</version>
</dependency>
</dependencies>
<build>
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.6.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>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<configuration>
<additionalProtoPathElements>
<additionalProtoPathElement>${project.basedir}/src/main/resources</additionalProtoPathElement>
</additionalProtoPathElements>
<protocArtifact>com.google.protobuf:protoc:3.7.0:exe:${os.detected.classifier}</protocArtifact>
</configuration>
</plugin>
這是 java class:
public class ProtobufTrial {
public static void main(String[] args) throws Exception {
String jsonString = "";
MyProto.MyEventMsg.Builder builder = MyProto.MyEventMsg.newBuilder();
JsonFormat.parser().ignoringUnknownFields().merge(jsonString, builder);
MyProto.MyEventMsg value = builder.build();
// This is for printing the proto in string format
System.out.println(JsonFormat.printer().print(value));
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.