---
title: IntelliJ IDEA
tags: ThingsBoard
image: https://i.imgur.com/36DZwsa.png
disqus: hackmd
---
IntelliJ IDEA
===
:::info
首次編譯TB,不建議開 IDEA IDE 直接進行操作,它會自動下載很多相關及不相關套件,不易控制。
先透過command line編譯成功後,再導入 IDEA 進行二次開發工作。
最常見的開發方式之一: Remote Development using SSH
https://www.jetbrains.com/help/idea/creating-a-remote-server-configuration.html
Local (IDEA) --- ssh --- Remote Server (VM)
:::
:::info
* Before importing the project into the IDE please build it using Maven tool from the root folder
* Any modern Java IDE, although TB recommend IntelliJ IDEA
:::
[toc]
Windows 10 Environment
---
### Install JDK 11
* Download zipped package [here](https://github.com/AdoptOpenJDK/openjdk11-binaries/releases/download/jdk-11.0.11%2B9/OpenJDK11U-jdk_x64_windows_hotspot_11.0.11_9.zip)
* Unzip JDK 11 as below
```
C:\Program Files\Java\jdk-11.0.11+9\bin
```
* Set up Path
> Windows key > Edit the system environment variables > Environment variables
```
User variables for User > New
Variable name: JAVA_HOME
Variable value: C:\Program Files\Java\jdk-11.0.11+9
Click Path in System Variables > Edit
New C:\Program Files\Java\jdk-11.0.11+9\bin
```
> close all existing CMD window(s) before version check

### Install Maven 3.6.3
* Download zipped package [here](https://downloads.apache.org/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.zip)
* Unzip Maven as below
```
C:\Program Files\apache-maven-3.6.3\bin
```
* Set up Path
> Windows key > Edit the system environment variables > Environment variables
```
User variables for User > New
Variable name: MAVEN_HOME
Variable value: C:\Program Files\apache-maven-3.6.3
Click Path in System Variables > Edit
New C:\Program Files\apache-maven-3.6.3\bin
```
> close all existing CMD window(s) before version check

### Build from source
> Recommended Windows tool: [Git for Windows](https://gitforwindows.org/)
```
git clone https://github.com/thingsboard/thingsboard.git
cd thingsboard/
git checkout release-3.2
```
or
```
git clone https://github.com/thingsboard/thingsboard.git
cd thingsboard/
git checkout release-3.3
```
> Build
```
mvn clean install -DskipTests
```


> **IF** Failure to find org.gradle:**gradle-tooling-api:jar:6.3**

```
Download gradle-tooling-api-6.3.jar to your local directory
https://repo.gradle.org/gradle/libs-releases-local/org/gradle/gradle-tooling-api/6.3/gradle-tooling-api-6.3.jar
###Windows CMD
mvn install:install-file -Dfile="C:\Users\User\Documents\workspace\gradle-tooling-api-6.3.jar" -DgroupId=org.gradle -DartifactId=gradle-tooling-api -Dversion=6.3 -Dpackaging=jar
###Git Bash(MINGW64)
mvn install:install-file -Dfile=/c/Users/User/Documents/workspace/gradle-tooling-api-6.3.jar -DgroupId=org.gradle -DartifactId=gradle-tooling-api -Dversion=6.3 -Dpackaging=jar
```
### PostgreSQL 12
:::info
- ip addr: 192.168.1.117
- port: 5432
- username/password: postgres/postgres
- database: thingsboard
:::
> Prepare another Ubuntu 20.04.2 VM running up PostgreSQL 12 (bridged mode, ip addr: 192.168.1.117)
```
# Create the file repository configuration:
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
# Import the repository signing key:
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
# Update the package lists:
sudo apt-get update
# Install the latest version of PostgreSQL.
# If you want a specific version, use 'postgresql-12' or similar instead of 'postgresql':
sudo apt-get -y install postgresql-12
sudo service postgresql status
```

> Set the password for main postgresql user
```
sudo su - postgres
psql
\password
\q
```
> Connect to the database to create **thingsboard** DB
```
psql -U postgres -d postgres -h 127.0.0.1 -W
CREATE DATABASE thingsboard;
\q
```

> Configure ThingsBoard to use external database (change localhost to 192.168.1.117)
* TB configuration file, ./application/src/main/resources/thingsboard.yml
* TB env variable file, ./application//src/main/conf/thingsboard.conf
```
# Edit thingsboard.yml
440 # SQL DAO Configuration
441 spring:
442 data:
443 jpa:
444 repositories:
445 enabled: "true"
446 jpa:
447 open-in-view: "false"
448 hibernate:
449 ddl-auto: "none"
450 database-platform: "${SPRING_JPA_DATABASE_PLATFORM:org.hibernate.dialect.PostgreSQLDialect}"
451 datasource:
452 driverClassName: "${SPRING_DRIVER_CLASS_NAME:org.postgresql.Driver}"
453 url: "${SPRING_DATASOURCE_URL:jdbc:postgresql://localhost:5432/thingsboard}"
454 username: "${SPRING_DATASOURCE_USERNAME:postgres}"
455 password: "${SPRING_DATASOURCE_PASSWORD:postgres}"
456 hikari:
457 maximumPoolSize: "${SPRING_DATASOURCE_MAXIMUM_POOL_SIZE:16}"
```
> Enable remote access to your PostgreSQL serv
* Edit /etc/postgresql/12/main/postgresql.conf
```
# line 58
- #listen_addresses = 'localhost'
+ listen_addresses = '*'
```

* Edit /etc/postgresql/12/main/pg_hba.conf
```
# The user postgres can access all databases from a trusted location (192.168.1.106) without a password
host all postgres 192.168.1.106 trust
```
```
sudo service postgresql restart
```
* Using UFW to manage firewall rules
```
sudo ufw allow proto tcp from 192.168.1.0/24 to any port 5432
```

* Install database schema (schema files are from your thingsboard repository)
```
star@tb-postgresql:~$ psql -h localhost -U postgres -d thingsboard -a -f schema-entities.sql
star@tb-postgresql:~$ psql -h localhost -U postgres -d thingsboard -a -f schema-entities-idx.sql
star@tb-postgresql:~$ psql -h localhost -U postgres -d thingsboard -a -f schema-ts-psql.sql
star@tb-postgresql:~$ psql -h localhost -U postgres -d thingsboard -a -f system-data.sql
```
### Import the source code into IDEA
* [IDEA 2021.1.2 CE](https://www.jetbrains.com/idea/download/)

* Select thingsboard root directory, click "ok", import may take a little time, please be patient

> Configure JDK, File > Project Structure... > Project SDK: **11 version 11.0.11**, Apply

> Maven home path

> Configure support es6 syntax (to be update)
```
Use Visual Studio Code for Javascript support when using IntelliJ Community Edition. It's free and has all the support you would get for the paid version of IntelliJ Ultimate.
```
> (optional) Copy sql schema files (schema-entities.sql, schema-entities-idx.sql, schema-ts-psql.sql)
```
from: C:\Users\User\Documents\workspace\tb\thingsboard\dao\src\main\resources\sql
to: C:\Users\User\Documents\workspace\tb\thingsboard\application\src\main\data\sql
```
### One time Initialization
> ThingsboardInstallApplication Environment variables
```
SPRING_DATASOURCE_URL=jdbc:postgresql://192.168.1.117:5432/thingsboard;SPRING_DATASOURCE_USERNAME=postgres;SPRING_DATASOURCE_PASSWORD=postgres;install.load_demo=true;install.data_dir=C:\Users\User\Documents\workspace\tb\thingsboard\application\src\main\data
```

### Run Server Application
> ThingsboardServerApplication Environment variables
```
SPRING_DATASOURCE_URL=jdbc:postgresql://192.168.1.117:5432/thingsboard;SPRING_DATASOURCE_USERNAME=postgres;SPRING_DATASOURCE_PASSWORD=postgres
```

> New add ==Shorten command line: JAR manifest== to avoid Error running ‘Application’: Command line is too long. Shorten command line for Application or also for Spring Boot default configuration.

> Local TB server application running up

> Using sysadmin@thingsboard.org/sysadmin to login

Resource(s)
---
- [Facebook ThingsBoard Taiwan Group](https://facebook.com/groups/thingsboard)
- https://thingsboard.io/docs/user-guide/contribution/how-to-contribute/
- https://anote.dev/install-jdk-11-apache-maven-3-6/
- https://www.yuque.com/thingsboard/book/iefexn
- https://linuxize.com/post/how-to-install-postgresql-on-ubuntu-20-04/