1、下载源代码
Thingsboard是目前最主流的开源物联网平台:https://github.com/thingsboard/thingsboard
2、配置Java和Maven
具体过程不再赘述,Java版本1.8.0;Maven版本3.3.9。
3、编译Thingsboard
具体介绍见文档:
https://thingsboard.io/docs/user-guide/install/building-from-source/
https://thingsboard.io/docs/user-guide/contribution/how-to-contribute/
进入Thingsboard目录,执行:
mvn clean install –DskipTests
为加快编译,用-DskipTests跳过测试
maven执行编译并下载依赖包,需要保证互联网连通
在编译ui过程中,会下载并安装node.js运行环境。下载安装时间会比较久(1小时或更多),请耐心等待
编译生成的文件,在thingsboard-master\application\target目录下。
如果在mvn过程中,编译ui出错,可以进入 ui目录,执行:
mvn clean install -DskipTests
在多数计算机上,可以顺利编译成功,如有问题,请参考后续解决方法。
4、初始化数据库
在编译成功后,在target目录下生成了jar文件和windows系统下的安装包,并生成application\target\windows目录,其中包括数据库初始化文件:
执行application\target\windows目录下的install_dev_db.bat,创建数据库和Demo所需的数据。
如果是在Linux下,执行
cd ${TB_WORK_DIR}/application/target/bin/install
chmod +x install_dev_db.sh
./install_dev_db.sh
具体见Contribution Guide:
https://thingsboard.io/docs/user-guide/contribution/how-to-contribute/#create-database-schema-and-populate-demo-data
ThingsBoard缺省使用嵌入式数据库HSQLDB,也可以安装PostgreSQL,具体的配置在
application/src/main/resources/thingsboard.yml中
5、启动ThingsBoard
在命令行中执行
java -jar application/target/thingsboard-2.1.1-SNAPSHOT-boot.jar
显示:
Started ThingsboardServerApplication in 39.086 seconds (JVM running for 40.088),说明启动成功,在浏览器中http://localhost:8080/login
用户名:[email protected],密码tenant,登陆系统:
6、在Eclipse中导入Thingsboard工程
为进行程序开发,在在Eclipse中导入Thingsboard工程,会自动下载安装所需的Maven插件等。
虽然已经编译通过,但是在eclipse中,存在大量“log cannot be resolved”错误,原因是缺少lombok插件,解决方法见:
https://www.cnblogs.com/danny-djy/p/9051714.html
前往官网下载:https://projectlombok.org/download lombok.jar
将lombok.jar放在eclipse的安装目录下。
在eclipse安装目录下,执行:
D:\eclipse>java -jar lombok.jar
选择eclipse 的安装目录。然后点击install/update,安装成功:
在Eclipse中Clean,上述问题解决。
Application包,存在错误:
Could not find artifact com.sun:tools:jar:1.4.2 at specified path C:\Program Files\Java\jre1.8.0_91/../lib/tools.jar
设置JRE路径为JDK安装目录:
修改 eclipse.exe 目录下的 eclipse.ini 指定vm,注意 -vm后面不能有空格:
-vm
C:/Program Files/Java/jdk1.8.0_91/bin/javaw.exe
-vmargs
-Dosgi.requiredJavaVersion=1.8
参考:https://www.cnblogs.com/amosli/p/3446316.html
如果改变了Maven本地库的缺省目录,也需要在Eclipse中设置。
总之,设置Eclipse中的编译选项和插件,与命令行编译保持一致。
7、解决Protocol Buffer编译问题
Thingsboard工程用到了gRPC,是 Google 开源的一套成熟的结构数据序列化机制,默认使用 protocol buffers格式,基于在代码中定义的proto文件,生成对应的java文件。
protobuf插件安装教程:
https://blog.csdn.net/iphone4grf/article/details/50902816
从github可以下载protobuf源代码和编译器protoc.exe
https://github.com/protocolbuffers/protobuf/releases
注意,如果在第3步中已经正常编译通过,所需的文件已经生成,并不需要安装上述的protobuf源码和编译器。
在Eclipse中编译Message包,存在错误:MsgProtos cannot be resolved,等等,原因是protobuf相关的代码文件没有生成。参考:
https://www.cnblogs.com/danny-djy/p/9051714.html
重新在命令行中执行
D:\thingsboard-master\common>mvn clean install
生成MsgProtos.java文件,在
D:\thingsboard-master\common\message\target\generated-sources\protobuf\java\org\thingsboard\server\common\msg\gen目录下
如果在命令行编译Application时,存在:
D:\thingsboard-master\application\target\protoc-plugins: D:\thingsboard-master\application\target\protoc-plugins\protoc-3.0.2-windows-x86_64.exe (另一个程序正在使用此文件,进程无法访问。),如图所示:
protoc-3.0.2-windows-x86_64.exe,是生成*Protos.java的程序,
另外,还有protoc-gen-grpc-java-1.0.0-windows-x86_64.exe,是生成*.grpc.java的工具,在以上截图中的compile和compile-custom,分别调用上述两个文件,具体见thingsboard-master/pom.xml中的定义:
可见,在生成*Protos.java文件时,以上exe文件正在被调用,然后又重复拷贝引起的问题,所以在pom中注释拷贝文件的部分:
并注释
重新编译,得到ClusterAPIProtos.java、ServerInstanceProtos.java
再取消对compile-custom的注释,并注释
重新编译,得到ClusterRpcServiceGrpc.java
这样,就得到了所需的所有文件,不需要再在编译时调用Protoc和Grpc了,注释掉
thingsboard-master\application\pom.xml中的protobuf-maven-plugin:
并在application中建立对应的package,把生成的代码文件拷贝进去:
然后,再进行编译,无论是命令行,还是Eclipse中编译,都可以通过。
到此为止,ThingsBoard开发环境搭建完成。
8、解决proto生成文件中缺少License的问题
在命令行中重新用mvn编译时,报错:
Failed to execute goal com.mycila:license-maven-plugin:3.0:check (default) on project message: Some files do not have the expected license header -> [Help 1]
原因是用Protoc生成的文件中缺少License声明,在4个生成的文件中,和其他代码文件一样,添加:
/**
* Copyright © 2016-2018 The Thingsboard Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
编译正常。这样,可以在Win10环境下进行Thingsboard开发了。