编译protobuf的jar文件

1、准备工作

需要到github上下载相应的文件,地址https://github.com/google/protobuf/releases

protobuf有很多不同语言的版本,因为我们需要的是jar文件,所以选择java版本下载。以下以版本3.1.0进行举例说明。

  • protobuf-java-3.1.0.tar.gz
  • protoc-3.1.0-linux-x86_64.zip

如果选择在Windows环境下进行编译,选择以下两个文件,同样,第一个相当于java发行版本的源码文件,第二个是一个编译好的protoc程序文件,只不过windows下面的程序版本目前只有32位的。

  • protobuf-java-3.1.0.zip
  • protoc-3.1.0-win32.zip

2、编译

1> 在Linux环境下(需要提前装好maven

将以上两个文件分别进行解压,然后将解压protoc-3.1.0-linux-x86_64.zip获取的protoc程序文件复制到解压文件目录protobuf-java-3.1.0的对应位置。

注意位置相当重要,在以下两个文件夹中

 src目录下面直接放一个protoc程序文件

扫描二维码关注公众号,回复: 1968715 查看本文章

 java文件夹下面的core/src文件夹中也要放置一个protoc程序文件

之后,cd到上面所述java文件夹下面直接运行以下命令即可

mvn package

会在java文件夹下面的core/target文件夹下面生成protobuf-java-3.1.0.jar文件

2> 在windows下面,与在linux中放置文件的位置相同,需要放置好protoc.exe程序文件,之后直接利用eclipse程序进行编译即可

右键点击java文件夹下面的pom.xml文件

3、其他说明

 为了加快maven的编译,可以将maven的源换成国内的

eclpse可以点击【Window】菜单——【Preferences】——【Maven】——【User Settings】,在maven的仓储位置.m2文件夹中添加一个settings.xml文件

Linux下类似,直接在.m2文件夹中添加settings.xml文件

<settings xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
    <mirrors>
        <!-- mirror | Specifies a repository mirror site to use instead of a given 
            repository. The repository that | this mirror serves has an ID that matches 
            the mirrorOf element of this mirror. IDs are used | for inheritance and direct 
            lookup purposes, and must be unique across the set of mirrors. | -->
        <mirror>
            <id>alimaven</id>
            <mirrorOf>central</mirrorOf>
            <name>aliyun maven</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
        </mirror>
        <mirror>
            <id>aliyunpublic</id>
            <name>aliyunpublic</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
            <mirrorOf>public</mirrorOf>       
        </mirror>
        <mirror>
            <id>centralmaven</id>
            <mirrorOf>centralmaven</mirrorOf>
            <name>centralmaven</name>
            <url>http://central.maven.org/maven2/</url>
        </mirror>
    </mirrors>

    <profiles>
        <profile>
            <id>default</id>
            <repositories>
                <repository>
                    <id>nexus</id>
                    <name>local private nexus</name>
                    <url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>false</enabled>
                    </snapshots>
                </repository>
            </repositories>
            <pluginRepositories>
                <pluginRepository>
                    <id>nexus</id>
                    <name>local private nexus</name>
                    <url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>false</enabled>
                    </snapshots>
                </pluginRepository>
            </pluginRepositories>
        </profile>
    </profiles>
</settings>

里面的几个地址相似,都可以用,可以随便换位置,并非固定在一个地方,这里只是举例。

猜你喜欢

转载自www.linuxidc.com/Linux/2016-12/138717.htm