Maven項目中,編譯proto檔案成Java類

來源:互聯網
上載者:User

標籤:

建立Maven項目

建立一個 Maven 項目:

  • pom定義了最小的maven2元素,即:groupId,artifactId,version。
  • groupId:項目或者組織的唯一標誌,並且配置時產生的路徑也是由此產生,如org.codehaus.mojo產生的相對路徑為:/org/codehaus/mojo
  • artifactId: 項目的通用名稱
  • version:項目的版本
  • packaging: 打包的機制,如pom, jar, maven-plugin, ejb, war, ear, rar, par
  • classifier: 分類

項目名稱:

我們建立的空白 Maven 項目結構如下:

 

匯入 gRPC 對應包依賴

對Maven 項目來說,在 pom.xml 檔案中需要增加對應 jar

<dependency>  <groupId>io.grpc</groupId>  <artifactId>grpc-all</artifactId>  <version>0.13.2</version></dependency>

匯入 proto 外掛程式

 

匯入 Maven protobuf 外掛程式

protobuf-maven-plugin:

https://www.xolstice.org/protobuf-maven-plugin/

修改 pom.xml 檔案,增加下面節:

<build>
    <extensions>
        <extension>
            <groupId>kr.motd.maven</groupId>
            <artifactId>os-maven-plugin</artifactId>
            <version>1.4.1.Final</version>
        </extension>
    </extensions>
    <plugins>
        <plugin>
            <groupId>org.xolstice.maven.plugins</groupId>
            <artifactId>protobuf-maven-plugin</artifactId>
            <version>0.5.0</version>
            <configuration>
                <!--
                  The version of protoc must match protobuf-java. If you don‘t depend on
                  protobuf-java directly, you will be transitively depending on the
                  protobuf-java version that grpc depends on.
                -->
                <protocArtifact>com.google.protobuf:protoc:3.0.0-beta-2:exe:${os.detected.classifier}</protocArtifact>
                <pluginId>grpc-java</pluginId>
                <pluginArtifact>io.grpc:protoc-gen-grpc-java:0.13.2:exe:${os.detected.classifier}</pluginArtifact>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                        <goal>compile-custom</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
執行Maven外掛程式命令。

在輸出命令中,我們可以看到編譯後的輸出。

[INFO] --- protobuf-maven-plugin:0.5.0:compile (default-cli) @ myTestMaven ---
[INFO] Compiling 2 proto file(s) to /Users/ghj1976/project/mystudy/Demo1/target/generated-sources/protobuf/java

 

 

參考: https://github.com/grpc/grpc-java/blob/master/README.md

Maven項目中,編譯proto檔案成Java類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.