歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> 編譯protobuf的jar文件

編譯protobuf的jar文件

日期:2017/2/28 13:45:41   编辑:Linux教程

1、准備工作

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

protobuf有很多不同語言的版本,因為我們需要的是jar文件,所以選擇java版本下載。以下以版本3.1.0進行舉例說明。

如果是在Linux64環境下編譯,可以選擇以下兩個文件,第一個相當於java發行版本的源碼文件,第二個是一個編譯好的protoc程序文件(如果想自己編譯protobuf程序文件,參考上篇文章 CentOS 7下protobuf的源碼編譯安裝 見 http://www.linuxidc.com/Linux/2016-12/138716.htm)。

  • 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的對應位置。

Maven權威指南_中文完整版清晰PDF http://www.linuxidc.com/Linux/2014-06/103690.htm

Linux 安裝 Maven http://www.linuxidc.com/Linux/2013-05/84489.htm

Maven3.0 配置和簡單使用 http://www.linuxidc.com/Linux/2013-04/82939.htm

Ubuntu下搭建sun-jdk和Maven2 http://www.linuxidc.com/Linux/2012-12/76531.htm

Maven使用入門 http://www.linuxidc.com/Linux/2012-11/74354.htm

Ubuntu 下 搭建Nexus Maven私服中央倉庫 http://www.linuxidc.com/Linux/2016-08/133936.htm

Linux下使用Nexus搭建Maven私服詳解 http://www.linuxidc.com/Linux/2016-08/134630.htm

注意位置相當重要,在以下兩個文件夾中

src目錄下面直接放一個protoc程序文件

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>

裡面的幾個地址相似,都可以用,可以隨便換位置,並非固定在一個地方,這裡只是舉例。

Copyright © Linux教程網 All Rights Reserved