Mac上protobuf环境构建-java

这篇具有很好参考价值的文章主要介绍了Mac上protobuf环境构建-java。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

参考文献
getting-started
官网pb java介绍
maven protobuf插件
简单入门1
简单入门2

1. protoc编译器下载安装

https://github.com/protocolbuffers/protobuf/releases?page=10
Mac上protobuf环境构建-java,macos,java,开发语言
放入.zshrc中配置环境变量

 ~/IdeaProjects/test2/ protoc --version
libprotoc 3.12.1
 ~/IdeaProjects/test2/ 
 ~/IdeaProjects/test2/ protoc -h       
Usage: protoc [OPTION] PROTO_FILES
Parse PROTO_FILES and generate output based on the options given:
  -IPATH, --proto_path=PATH   Specify the directory in which to search for
                              imports.  May be specified multiple times;
                              directories will be searched in order.  If not
                              given, the current working directory is used.
                              If not found in any of the these directories,
                              the --descriptor_set_in descriptors will be
                              checked for required proto file.
  --version                   Show version info and exit.
  -h, --help                  Show this text and exit.
  --encode=MESSAGE_TYPE       Read a text-format message of the given type
                              from standard input and write it in binary
                              to standard output.  The message type must
                              be defined in PROTO_FILES or their imports.
  --decode=MESSAGE_TYPE       Read a binary message of the given type from
                              standard input and write it in text format
                              to standard output.  The message type must
                              be defined in PROTO_FILES or their imports.
  --decode_raw                Read an arbitrary protocol message from
                              standard input and write the raw tag/value
                              pairs in text format to standard output.  No
                              PROTO_FILES should be given when using this
                              flag.
  --descriptor_set_in=FILES   Specifies a delimited list of FILES
                              each containing a FileDescriptorSet (a
                              protocol buffer defined in descriptor.proto).
                              The FileDescriptor for each of the PROTO_FILES
                              provided will be loaded from these
                              FileDescriptorSets. If a FileDescriptor
                              appears multiple times, the first occurrence
                              will be used.
  -oFILE,                     Writes a FileDescriptorSet (a protocol buffer,
    --descriptor_set_out=FILE defined in descriptor.proto) containing all of
                              the input files to FILE.
  --include_imports           When using --descriptor_set_out, also include
                              all dependencies of the input files in the
                              set, so that the set is self-contained.
  --include_source_info       When using --descriptor_set_out, do not strip
                              SourceCodeInfo from the FileDescriptorProto.
                              This results in vastly larger descriptors that
                              include information about the original
                              location of each decl in the source file as
                              well as surrounding comments.
  --dependency_out=FILE       Write a dependency output file in the format
                              expected by make. This writes the transitive
                              set of input file paths to FILE
  --error_format=FORMAT       Set the format in which to print errors.
                              FORMAT may be 'gcc' (the default) or 'msvs'
                              (Microsoft Visual Studio format).
  --print_free_field_numbers  Print the free field numbers of the messages
                              defined in the given proto files. Groups share
                              the same field number space with the parent 
                              message. Extension ranges are counted as 
                              occupied fields numbers.

  --plugin=EXECUTABLE         Specifies a plugin executable to use.
                              Normally, protoc searches the PATH for
                              plugins, but you may specify additional
                              executables not in the path using this flag.
                              Additionally, EXECUTABLE may be of the form
                              NAME=PATH, in which case the given plugin name
                              is mapped to the given executable even if
                              the executable's own name differs.
  --cpp_out=OUT_DIR           Generate C++ header and source.
  --csharp_out=OUT_DIR        Generate C# source file.
  --java_out=OUT_DIR          Generate Java source file.
  --js_out=OUT_DIR            Generate JavaScript source.
  --objc_out=OUT_DIR          Generate Objective C header and source.
  --php_out=OUT_DIR           Generate PHP source file.
  --python_out=OUT_DIR        Generate Python source file.
  --ruby_out=OUT_DIR          Generate Ruby source file.
  @<filename>                 Read options and filenames from file. If a
                              relative file path is specified, the file
                              will be searched in the working directory.
                              The --proto_path option will not affect how
                              this argument file is searched. Content of
                              the file will be expanded in the position of
                              @<filename> as in the argument list. Note
                              that shell expansion is not applied to the
                              content of the file (i.e., you cannot use
                              quotes, wildcards, escapes, commands, etc.).
                              Each line corresponds to a single argument,
                              even if it contains spaces.
 ~/IdeaProjects/test2/ 

安装好上面的编译器就可以手动编译proto文件了,但是java程序员肯定是用maven项目的方式使用了,如何操作呢?下面介绍

2. demo项目构建

Mac上protobuf环境构建-java,macos,java,开发语言

2.1 pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.tom</groupId>
    <artifactId>lnacos</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <os.detected.classifier>osx-x86_64</os.detected.classifier>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <!-- https://mvnrepository.com/artifact/com.alibaba.nacos/nacos-client -->
    <dependencies>
        <!-- https://mvnrepository.com/artifact/com.alibaba.nacos/nacos-client -->
        <dependency>
            <groupId>com.alibaba.nacos</groupId>
            <artifactId>nacos-client</artifactId>
            <version>1.1.4</version>
        </dependency>


        <dependency>
            <groupId>com.google.protobuf</groupId>
            <artifactId>protobuf-java</artifactId>
            <version>3.12.1</version>
        </dependency>

        <dependency>
            <groupId>com.google.protobuf</groupId>
            <artifactId>protobuf-java-util</artifactId>
            <version>3.12.1</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.xolstice.maven.plugins</groupId>
                <artifactId>protobuf-maven-plugin</artifactId>
                <version>0.6.1</version>
                <configuration>
                    <protocArtifact>com.google.protobuf:protoc:3.12.0:exe:${os.detected.classifier}</protocArtifact>
                    <pluginId>proto</pluginId>
                    <pluginArtifact>io.grpc:protoc-gen-grpc-java:1.32.1:exe:${os.detected.classifier}</pluginArtifact>
                    <protoSourceRoot>src/main/resources/proto3</protoSourceRoot>
                    <outputDirectory>src/main/java</outputDirectory>
                    <clearOutputDirectory>false</clearOutputDirectory>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>test-compile</goal>
<!--                            <goal>compile-custom</goal>-->
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>

    </build>
</project>

2.2 mvn clean complie 生成java文件

测试类LProto

package com.tom.model;

import com.google.protobuf.util.JsonFormat;

import java.util.Arrays;

public class LProto {
    public static void main(String[] args) throws Exception{
        DemoModel.Demo.Builder builder = DemoModel.Demo.newBuilder();

        DemoModel.Demo build = builder.setId(123).setName("123").build();
        byte[] byteArray = build.toByteArray();
        System.out.println(Arrays.toString(byteArray));
        System.out.println(byteArray.length);

        String print = JsonFormat.printer().print(build);
        System.out.println(print);
        System.out.println(print.length());
    }
}

输出
[8, 123, 26, 3, 49, 50, 51]
7
{
  "id": 123,
  "name": "123"
}
32

可以看到使用pb压缩后的字段小了很多。

问题1-os.detected.classifier

Mac上protobuf环境构建-java,macos,java,开发语言
增加配置文章来源地址https://www.toymoban.com/news/detail-728359.html

    <properties>
        <os.detected.classifier>osx-x86_64</os.detected.classifier>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

到了这里,关于Mac上protobuf环境构建-java的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • java后端开发环境搭建 mac

    在mac pro上搭建一套java 后端开发环境,主要安装的内容有:jdk、maven、git、tomcat、mysql、navicat、IntelliJ、redis。 本人mac pro的系统为mac OS Monterey 12.6.7,主机的硬件架构为x86_64。 左上角关于本机查看系统版本;终端上输入 查看机器架构: 下载官网:Java Downloads | Oracle 中国 安装步

    2024年02月15日
    浏览(33)
  • mac pro java后端开发环境搭建

        从零开始,在mac pro上搭建一套java 后端开发环境,主要安装的内容有:jdk、maven、git、tomcat、mysql、navicat、IntelliJ、redis。     因为网上有很多很详细的教程,这里主要记录大概的安装过程和本人在安装过程中遇到的问题以及解决办法。     本人mac pro的系统为mac OS

    2024年02月11日
    浏览(31)
  • I.MX6U C语言运行环境构建及驱动开发格式

    1.设置处理器模式 设置6ULL处于SVC模式下。设置下CPSR寄存器的bit4-0,也就是M[4:0]为10011=0x13.。读写状态寄存器需要用到MRS和MSR指令。MRS将CPSR寄存器数据读出到通用寄存器里面,MSR指令将通用寄存器的值写入到CPSR寄存器里面去。 2.设置SP指针 SP可以指向内部RAM,也可以指向DDR,我

    2024年02月19日
    浏览(27)
  • mac电脑m1搭建java开发环境参考手册

    开发人员经常会换电脑,或者换新电脑,意味着重新搭建开发环境,很麻烦。但新电脑到手里面了,不换又不好,此篇专门用来记录mac电脑m1搭建java开发环境的步骤。希望对读者有所帮助,一条龙服务。 后期有时间,会出关于win10环境的,目前介绍mac环境。 M1版MacBook安装Ar

    2024年02月09日
    浏览(39)
  • 在MAC OS上的vscode 安装java开发环境

    在Mac OS上安装vs code的java开发环境. 按照vs code的官方说明安装Java相关插件, 遇见下列问题并解决了. 安装JDK环境 安装Extension Pack for Java 插件后,vscode会提示你安装一个java,我安装提示安装了java.后来才发现安装的是jre,并不是JDK. 还是需要去oracle或者micorsoft网站上下载一个JAVA SE安

    2024年02月12日
    浏览(40)
  • 1.mac M1 Java 开发环境的安装与配置

    1.首先我们打开谷歌浏览器复制下面的网址安装jdk: Java Download | Java 7, Java 8, Java 11, Java 13, Java 15, Java 17, Java 19 - Linux, Windows and macOS https://www.azul.com/downloads/?package=jdk#zulu 2.我们翻到最下面去选择我们需要的版本, 选择Java8 ,  系统是 masOs , 架构选择ARM 64 bit 的,java安装包选择 J

    2024年02月12日
    浏览(41)
  • Mac-Java开发环境安装(JDK和Maven)

    1、访问oracle官网,下载jdk 点击下载链接:https://www.oracle.com/java/technologies/downloads/#java11-mac 选择Mac版本,下载dmg 打勾点击下载,跳转登陆,没有就注册,输入账号密码即可下载成功。 下载之后直接双击安装下一步就完事了,Mac安装JDK时无需配置安装目录。 2、检查是否安装成

    2024年02月06日
    浏览(64)
  • 【开发环境】Mac 安装 Visual Studio Code ① ( VSCode 简介 | 下载 VSCode | 安装 VSCode | 安装中文语言包 )

    Visual Studio Code 简称 VSCode , 是 微软 开发的一款 轻量级 / 跨平台 的代码编辑器 ; VSCode 支持 Windows、macOS 和 Linux 操作系统 ; VSCode 支持如下 编程语言 : JavaScript TypeScript Node.js C++ C# Java Python PHP Go VSCode 集成了调试器 , 可以调试 上述 多种语言 ; VSCode 内置了 Git 版本控制插件 , 可以

    2024年04月28日
    浏览(52)
  • 【开发环境】Mac 安装 Visual Studio Code ② ( 装 C/C++ 扩展 | 安装配置 Code Runner 扩展插件 | 运行 C 语言程序 )

    在 VSCode 中 , 点击 左侧 的 \\\" 扩展 \\\" 按键 , 在 扩展 面板中 , 默认显示之前已经安装过的 扩展 ; 在 扩展面板 中的 搜索栏 , 输入 \\\" C \\\" , 第一个就是 C/C++ 扩展 , 点击 \\\" 安装 \\\" 按钮 , 安装改 扩展插件 ; 安装完毕后 , 提示 如下内容 ; 在 扩展 面板中 , 搜索 \\\" runner \\\" , 搜索出的第一个

    2024年04月25日
    浏览(62)
  • macOS配置Python开发环境

    现在的macOS系统(以本人MacBook Air 15.3英寸 M2芯片macOS Ventura系统为例)已经集成好了python环境,且在自带终端zsh中使用python命令。 /Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9 注意:\\\"/\\\"代表 系统根目录 (即Machintosh HD),\\\"~\\\"代表 用户根目录 (即根目录下的

    2024年02月13日
    浏览(33)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包