[Thingsboard] 1. 源码的下载和编译

一、前言

本文基于 Thingsboard 4.0.2 编写,对应提交Version set to 4.0.2(01c5ba7d37006e1f8a3492afbb3c67d017ca8dd3)
最近我在研究 Thingsboard,因此写下这一系列文章,文本记录构建过程中遇到的问题与解决方法。所用方法依赖代理来解决网络相关的构建失败问题。虽然必须使用代理,但内容仍具备一定的参考价值,尤其适合在环境配置上遇到困难的开发者。由于写作经验有限,欢迎读者指出文中的错误与不足。

二、JDK下载

需要JDK 17,不考虑使用最新的版本,可以到WEJDK学习站下载。

三、源码下载

参考 Building from sources

我们可以从 github.com/thingsboard/thingsboard 获取源码。需要注意的是,根据参考文档的说明,下载 release-* 分支的源码:

git clone -b release-4.0 https://github.com/thingsboard/thingsboard.git --depth 1
cd thingsboard

我曾尝试使用 tag 中的源码,虽然编译成功,但运行时存在功能异常的问题,因此建议务必使用 release-* 分支。

四、Maven同步

Maven 同步过程中的最大难点就是网络问题。本文提供两种思路。

1. 镜像仓库

不会.jpg

2. 代理

默认情况下,我们需要修改 %USERPROFILE%/.m2/settings.xml(例如 C:/Users/Admin/.m2/settings.xml)。
如果你在 IDEA 中修改了 Maven | User settings file(用户设置文件),则应前往你自定义的路径进行修改。
找到<proxies>在下方添加

<proxy>
        <id>http</id>
        <active>true</active>
        <protocol>http</protocol>
        <host>127.0.0.1</host>
        <port>7890</port>
</proxy>
<proxy>
        <id>http2</id>
        <active>true</active>
        <protocol>https</protocol>
        <host>127.0.0.1</host>
        <port>7890</port>
</proxy>

其中 hostport 需要根据你的实际代理设置进行修改。建议同时设置 httphttps 两种协议,因为有些工具严格区别两种协议的代理。

五、编译前准备工作

1. 注释许可证检查插件(可选)

使用 Ctrl+Shift+F 全局查找 license-maven-plugin,将每一处包含该插件的 <plugin>...</plugin> 标签块整体注释掉。
该插件会检查每个文件开头是否包含 license 信息,如果你新建的文件缺少相关注释,就会导致编译失败。注意,这类插件在多个模块中可能存在,因此需要全部注释。
示例如下:

<!--            <plugin>-->
<!--                <groupId>com.mycila</groupId>-->
<!--                <artifactId>license-maven-plugin</artifactId>-->
<!--            </plugin>-->

2. ui-ngx编译修改其一(设置maven代理)

报错信息如下:

[ERROR] Failed to execute goal com.github.eirslett:frontend-maven-plugin:1.12.0:yarn (yarn build) on project ui-ngx: Failed to run task: 'yarn run build:prod --https-proxy=http://127.0.0.1:7890 --proxy=http://127.0.0.1:7890' failed. org.apache.commons.exec.ExecuteException: Process exited with an error: 1 (Exit value: 1)

这是因为 frontend-maven-plugin 会默认继承 Maven 的代理设置,并将代理参数附加到 yarn 命令中。虽然在依赖安装阶段这是有用的,但在构建时反而可能引发问题。

我们可以通过修改 ui-ngxpom.xml 文件的配置来关闭该行为。搜索 yarn build,在对应的 <execution> 中的 <configuration> 进行修改,示例如下:

<configuration>
    <yarnInheritsProxyConfigFromMaven>false</yarnInheritsProxyConfigFromMaven>
    <arguments>run build:prod</arguments>
</configuration>

3. ui-ngx编译修改其二 (未设置maven代理,本人未测试)

ui-ngx的编译分为两步。

  1. nodeyarn环境下载。

    可以通过手动下载来解决,参考id为install node and npm<execution>元素,下载对应版本的 node.exeyarn 依赖文件,放置在 msa:js-executormsa:web-uiui-ngxtarget 目录下。下面是文件目录结构示例:

target
├─node
│  └─yarn
│      └─dist
│          ├─bin
│          └─lib
  1. yarn安装前端依赖。

    对于未设置 Maven 代理的情况,可以在 yarn install<execution> 区块中添加 --registry 参数。示例如下:

<configuration>
    <arguments>install --registry https://registry.npmmirror.com --non-interactive --network-concurrency 4 --network-timeout 100000 --mutex network</arguments>
</configuration>

六、编译

参考 Building from sources

执行 Maven 的 install 生命周期,开始编译 Thingsboard 源码并发布到本地maven仓库。

跳过测试(可选)

为了提升编译效率,推荐加上 -DskipTests 参数跳过测试步骤。

  • IDEA | Maven(侧边栏) | Execute Maven Goal(执行Maven目标)
  • IDEA | Ctrl+Shift+A | 输入execute maven,找到Execute Maven Goal(执行Maven目标)
  • VSCode(保证安装对应插件)| Ctrl+Shift+P | 输入execute,找到Maven: Execute Commands...(Maven: 执行命令...)| 选择 ThingsBoard

输入install -DskipTests

  • IDEA的内容应该是mvn install -DskipTests
  • VSCode的内容应该是install -DskipTests

七、结尾

如果你已经成功编译通过,那么恭喜你!我认为整个配置过程中最棘手的问题就是网络。你需要在各种工具链中逐一设置代理,尤其是在 IDE 中更为复杂,因为 IDE 经常会自动处理这些过程,导致我们难以直接干预。

posted @ 2025-07-03 22:37  sheng_ri  阅读(141)  评论(0)    收藏  举报