石娅凉
2025-7-3 21:26:44
前言
最近我在研究 Thingsboard,因此写下这一系列文章,文本记录构建过程中遇到的问题与解决方法。本文基于 Thingsboard 4.0.1 编写 ,所用方法依赖代理 来解决网络相关的构建失败问题。虽然必须使用代理,但内容仍具备一定的参考价值,尤其适合在环境配置上遇到困难的开发者 。由于写作经验有限,欢迎读者指出文中的错误与不足。
源码下载
参考 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(用户设置文件),则应前往你自定义的路径进行修改。
找到在下方添加<proxy> <id>http</id> true</active> <protocol>http</protocol> <host>127.0.0.1</host> <port>7890</port> </proxy> <proxy> <id>http2</id> true</active> <protocol>https</protocol> <host>127.0.0.1</host> <port>7890</port> </proxy> 复制代码 其中 host 和 port 需要根据你的实际代理设置进行修改。建议同时设置 http 和 https 两种协议,因为有些工具严格区别两种协议的代理。
准备工作
1. 注释许可证检查插件(可选)
使用 Ctrl+Shift+F 全局查找 license-maven-plugin,将每一处包含该插件的 ... 标签块整体注释掉。
该插件会检查每个文件开头是否包含 license 信息,如果你新建的文件缺少相关注释,就会导致编译失败。注意,这类插件在多个模块中可能存在,因此需要全部注释。
示例如下:[/code][size=4]2. ui-ngx编译修改其一(设置maven代理)[/size] 报错信息如下: [code][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-ngx 中 pom.xml 文件的配置来关闭该行为。搜索 yarn build,在对应的 中的 进行修改,示例如下:<configuration> <yarnInheritsProxyConfigFromMaven>false</yarnInheritsProxyConfigFromMaven> run build:prod</arguments> </configuration> 复制代码 3. ui-ngx编译修改其二 (未设置maven代理,本人未测试)
ui-ngx的编译分为两步。
node和yarn环境下载。
可以通过手动下载来解决,参考id为install node and npm的元素,下载对应版本的 node.exe 和 yarn 依赖文件,放置在 msa:js-executor、msa:web-ui 和 ui-ngx 的 target 目录下。下面是文件目录结构示例:
target ├─node │ └─yarn │ └─dist │ ├─bin │ └─lib 复制代码
yarn安装前端依赖。
对于未设置 Maven 代理的情况,可以在 yarn install 的 区块中添加 --registry 参数。示例如下:
<configuration> install --registry https://registry.npmmirror.com --non-interactive --network-concurrency 4 --network-timeout 100000 --mutex network</arguments> </configuration> 复制代码 编译
参考 Building from sources
执行 Maven 的 package 生命周期,开始编译 Thingsboard 源码。
跳过测试(可选)
为了提升编译效率,推荐加上 -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
输入package -DskipTests。
IDEA的内容应该是mvn package -DskipTests
VSCode的内容应该是package -DskipTests
结尾
如果你已经成功编译通过,那么恭喜你!我认为整个配置过程中最棘手的问题就是网络。你需要在各种工具链中逐一设置代理,尤其是在 IDE 中更为复杂,因为 IDE 经常会自动处理这些过程,导致我们难以直接干预。
来源:豆瓜网用户自行投稿发布,如果侵权,请联系站长删除
相关推荐