ArduBlock のビルド

:::note
20221113追記
今どきの環境にあわせたビルド方法を新たに書きました。
↓以下の記事です。
# Ardublock を Docker でビルドする
https://qiita.com/nanbuwks/items/1a576859987e463264c3

Docker使わない場合でもリンク先の新しい記事のビルド方法をお使いいただくのがいいかと思います。
:::

# ArduBlock とは

Scratch ライクなブロックプログラムを Arduino で行うプログラム環境。
Arduino IDE のプラグインとして動作する。

# 準備

## 環境

- Ubuntu 16.04 LTS (AMD64)
- Ubuntu 20.04 LTS (AMD64) 


## ArduBlock のソースを取得

最近公式の更新が捗っているので現時点での最新版で試してみる

https://github.com/taweili/ardublock
よりパッケージをダウンロードして展開
```
 tar xzvf ardublock-beta-20170414.tar.gz 
 cd ardublock-beta-20170414/
```

## 必要なパッケージをインストール

### openjdk

ここでいろいろ問題発生。結論から言うと、openjdk-8-jdk を使えば良い。openjdk-9-jdk をインストールするためにアレコレやったのは必要ありませんでした。


```
$ sudo apt install openjdk-8-jdk

```
(以下は無駄なことをやった記録)

```
sudo apt-get install openjdk-9-jdk
パッケージリストを読み込んでいます... 完了
依存関係ツリーを作成しています                
状態情報を読み取っています... 完了
...

...
openjdk-9-jdk-headless:amd64 (9~b114-0ubuntu1) を展開しています...
以前に未選択のパッケージ openjdk-9-jdk:amd64 を選択しています。
.../openjdk-9-jdk_9~b114-0ubuntu1_amd64.deb を展開する準備をしています ...
openjdk-9-jdk:amd64 (9~b114-0ubuntu1) を展開しています...
dpkg: アーカイブ /var/cache/apt/archives/openjdk-9-jdk_9~b114-0ubuntu1_amd64.deb の処理中にエラーが発生しました (--unpack):
 '/usr/lib/jvm/java-9-openjdk-amd64/include/linux/jawt_md.h' を上書きしようとしています。これはパッケージ openjdk-9-jdk-headless:amd64 9~b114-0ubuntu1 にも存在します
man-db (2.7.5-1) のトリガを処理しています ...
libc-bin (2.23-0ubuntu7) のトリガを処理しています ...
ca-certificates (20160104ubuntu1) のトリガを処理しています ...
Updating certificates in /etc/ssl/certs...
0 added, 0 removed; done.
Running hooks in /etc/ca-certificates/update.d...
done.
fontconfig (2.11.94-0ubuntu1.1) のトリガを処理しています ...
gconf2 (3.2.6-3ubuntu6) のトリガを処理しています ...
bamfdaemon (0.5.3~bzr0+16.04.20160824-0ubuntu1) のトリガを処理しています ...
Rebuilding /usr/share/applications/bamf-2.index...
gnome-menus (3.13.3-6ubuntu3.1) のトリガを処理しています ...
desktop-file-utils (0.22-1ubuntu5) のトリガを処理しています ...
mime-support (3.59ubuntu1) のトリガを処理しています ...
hicolor-icon-theme (0.15-0ubuntu1) のトリガを処理しています ...
処理中にエラーが発生しました:
 /var/cache/apt/archives/openjdk-9-jdk_9~b114-0ubuntu1_amd64.deb
E: Sub-process /usr/bin/dpkg returned an error code (1)
```

パッケージのミスかな？


```

  sudo dpkg --configure -a
  sudo dpkg -i --force-overwrite '/var/cache/apt/archives/openjdk-9-jdk_9~b114-0ubuntu1_amd64.deb'
```
これでクリア

### maven 

```

 sudo apt-get install maven
```
# ビルド

```

mvn install
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building ArduBlock 0.1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-install-plugin/2.5/maven-install-plugin-2.5.pom
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-install-plugin/2.5/maven-install-plugin-2.5.pom (8 KB at 8.4 KB/sec)
...


...
Downloaded: http://repo1.maven.org/maven2/org/apache/poi/poi-ooxml-schemas/3.9/poi-ooxml-schemas-3.9.jar (4691 KB at 877.6 KB/sec)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 29.665 s
[INFO] Finished at: 2017-05-24T18:15:30+09:00
[INFO] Final Memory: 12M/40M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal on project ardublock: Could not resolve dependencies for project org.ardublock:ardublock:jar:0.1.0-SNAPSHOT: The following artifacts could not be resolved: edu.mit:openblocks:jar:1.0.2-SNAPSHOT, arduino:pde:jar:1.1, arduino:arduino-core:jar:1.6.12: Could not find artifact edu.mit:openblocks:jar:1.0.2-SNAPSHOT in central (http://repo1.maven.org/maven2) -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/DependencyResolutionException
```

ああ、そうだった！ openblocks が必要なのでした。

## openblock のインストール

https://github.com/taweili/openblocks
からソースをダウンロード。適当なディレクトリで

```

  unzip openblocks-master.zip 
  cd openblocks-master/
  mvn install

```

## ArduBlock 

ArduBlock作業のディレクトリに戻って

```

$  mvn exec:java -Dexec.mainClass="com.ardublock.Main"
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building ArduBlock 0.1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- exec-maven-plugin:1.6.0:java (default-cli) @ ardublock ---
Downloading: 
...

...
https://repo.maven.apache.org/maven2/org/sonatype/plexus/plexus-cipher/1.4/plexus-cipher-1.4.jar (14 KB at 5.9 KB/sec)
Downloaded: https://repo.maven.apache.org/maven2/org/apache/commons/commons-exec/1.3/commons-exec-1.3.jar (54 KB at 21.6 KB/sec)
Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/3.0.20/plexus-utils-3.0.20.jar (238 KB at 78.3 KB/sec)
[WARNING] 
java.lang.ClassNotFoundException: com.ardublock.Main
	at java.net.URLClassLoader.findClass(java.base@9-internal/URLClassLoader.java:384)
	at java.lang.ClassLoader.loadClass(java.base@9-internal/ClassLoader.java:486)
	at java.lang.ClassLoader.loadClass(java.base@9-internal/ClassLoader.java:419)
	at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:270)
	at java.lang.Thread.run(java.base@9-internal/Thread.java:804)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 35.100 s
[INFO] Finished at: 2017-05-24T18:27:55+09:00
[INFO] Final Memory: 14M/47M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.6.0:java (default-cli) on project ardublock: An exception occured while executing the Java class. com.ardublock.Main -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

```

エラーで止まる。

何年か前にビルドした時は

```

 mvn exec:java -Dexec.mainClass="com.ardublock.Main"
```

ではなく

```

 mvn compile exec:java -Dexec.mainClass="com.ardublock.Main"

```

とするとビルドできたのでこちらで試してみる。


```
$ mvn compile exec:java -Dexec.mainClass="com.ardublock.Main"
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building ArduBlock 0.1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-install-plugin:2.5:install-file (install-arduino-pde) @ ardublock ---
[INFO] pom.xml not found in pde.jar
[INFO] Installing /home/nanbuwks/Downloads/ardublock-beta-20170414/lib/pde.jar to /home/nanbuwks/.m2/repository/arduino/pde/1.1/pde-1.1.jar
[INFO] Installing /tmp/mvninstall4390836131857961915.pom to /home/nanbuwks/.m2/repository/arduino/pde/1.1/pde-1.1.pom
[INFO] 
[INFO] --- maven-install-plugin:2.5:install-file (install-arduino-core) @ ardublock ---
[INFO] pom.xml not found in arduino-core.jar
[INFO] Installing /home/nanbuwks/Downloads/ardublock-beta-20170414/lib/arduino-core.jar to /home/nanbuwks/.m2/repository/arduino/arduino-core/1.6.12/arduino-core-1.6.12.jar
[INFO] Installing /tmp/mvninstall1253892278502483144.pom to /home/nanbuwks/.m2/repository/arduino/arduino-core/1.6.12/arduino-core-1.6.12.pom
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ ardublock ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 343 resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ ardublock ---
[INFO] Compiling 624 source files to /home/nanbuwks/Downloads/ardublock-beta-20170414/target/classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] Failure executing javac, but could not parse the error:
エラー: Source option 1.5 is no longer supported. Use 1.6 or later.
エラー: Target option 1.5 is no longer supported. Use 1.6 or later.

[INFO] 1 error
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 8.633 s
[INFO] Finished at: 2017-05-24T18:28:30+09:00
[INFO] Final Memory: 10M/35M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project ardublock: Compilation failure
[ERROR] Failure executing javac, but could not parse the error:
[ERROR] エラー: Source option 1.5 is no longer supported. Use 1.6 or later.
[ERROR] エラー: Target option 1.5 is no longer supported. Use 1.6 or later.
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
```
この対策として、
```
$ vim pom.xml

```
として

```

                        <plugin>
                                <groupId>org.apache.maven.plugins</groupId>
                                <artifactId>maven-compiler-plugin</artifactId>
                                <version>2.3.2</version>
                                <configuration>
                                        <source>1.5</source>
                                        <target>1.5</target>
                                </configuration>
                        </plugin>
```

↓書き換える

```

                        <plugin>
                                <groupId>org.apache.maven.plugins</groupId>
                                <artifactId>maven-compiler-plugin</artifactId>
                                <configuration>
                                        <source>1.6</source>
                                        <target>1.6</target>
                                </configuration>
                        </plugin>

```


(2022.02.24 に試したときには後述のエラーが出たため以下のようにしました。)

```
                                        <source>${java.version}</source>
                                        <target>${java.version}</target>
```

## 文字コードによるエラー

ここから、UTF-8にマップできない文字によるエラーの対処。
結局ここの作業は必要なかった。openjdk-9だとこのエラー出るがopenjdk-8を使うことになったので問題は解消。

```

mvn compile exec:java -Dexec.mainClass="com.ardublock.Main"

```
↓

```

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project ardublock: Compilation failure: Compilation failure:
[ERROR] /home/nanbuwks/Downloads/ardublock-beta-20170414/src/main/java/com/ardublock/translator/block/dfrobot/lcdkeypad.java:[18,6] エラー: この文字は、エンコーディングUTF-8にマップできません
[ERROR] /home/nanbuwks/Downloads/ardublock-beta-20170414/src/main/java/com/ardublock/translator/block/dfrobot/lcdkeypad.java:[18,10] エラー: この文字は、エンコーディングUTF-8にマップできません
[ERROR] /home/nanbuwks/Downloads/ardublock-beta-20170414/src/main/java/com/ardublock/translator/block/dfrobot/lcdkeypad.java:[18,27] エラー: この文字は、エンコーディングUTF-8にマップできません
[ERROR] /home/nanbuwks/Downloads/ardublock-beta-20170414/src/main/java/com/ardublock/translator/block/dfrobot/lcdkeypad.java:[18,63] エラー: この文字は、エンコーディングUTF-8にマップできません
[ERROR] /home/nanbuwks/Downloads/ardublock-beta-20170414/src/main/java/com/ardublock/translator/block/dfrobot/lcdkeypad.java:[19,8] エラー: この文字は、エンコーディングUTF-8にマップできません
[ERROR] /home/nanbuwks/Downloads/ardublock-beta-20170414/src/main/java/com/ardublock/translator/block/dfrobot/lcdkeypad.java:[19,27] エラー: この文字は、エンコーディングUTF-8にマップできません
[ERROR] /home/nanbuwks/Downloads/ardublock-beta-20170414/src/main/java/com/ardublock/translator/block/dfrobot/lcdkeypad.java:[19,31] エラー: この文字は、エンコーディングUTF-8にマップできません
[ERROR] /home/nanbuwks/Downloads/ardublock-beta-20170414/src/main/java/com/ardublock/translator/block/dfrobot/lcdkeypad.java:[19,33] エラー: この文字は、エンコーディングUTF-8にマップできません
[ERROR] /home/nanbuwks/Downloads/ardublock-beta-20170414/src/main/java/com/ardublock/translator/block/dfrobot/lcdkeypad.java:[21,7] エラー: この文字は、エンコーディングUTF-8にマップできません
```

```
vim src/main/java/com/ardublock/translator/block/dfrobot/lcdkeypad.java
```
として下記部分のコメントを削除

```

        {
                // récupération des paramètres du module, ici le message placé en rang 0
                // on écrit donc le code Ã  générer
                TranslatorBlock translatorBlock = this.getRequiredTranslatorBlockAtSocket(0, "lcd.print( ", " );\n");
                // création du texte de code correspondant

```

この後、以下のように出るエラーを対処するために

```

[INFO] --- exec-maven-plugin:1.6.0:java (default-cli) @ ardublock ---
5月 24, 2017 6:37:26 午後 sun.util.PropertyResourceBundleCharset$PropertiesFileDecoder decodeLoop
INFO: Invalid or unmappable UTF-8 sequence detected. Switching encoding from UTF-8 to ISO-8859-1
ardublock.xml: missing bg.DuinoEDU_infrared_compare.description
#
# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGSEGV (0xb) at pc=0x00007ffad7df0009, pid=23183, tid=23280
#
# JRE version: OpenJDK Runtime Environment (9.0) (build 9-internal+0-2016-04-14-195246.buildd.src)
# Java VM: OpenJDK 64-Bit Server VM (9-internal+0-2016-04-14-195246.buildd.src, mixed mode, tiered, compressed oops, g1 gc, linux-amd64)
# Problematic frame:
# C  [libjava.so+0x1d009]  JNU_GetEnv+0x19
#
# Core dump will be written. Default location: Core dumps may be processed with "/usr/share/apport/apport %p %s %c %P" (or dumping to /home/nanbuwks/Downloads/ardublock-beta-20170414/core.23183)
#
# An error report file with more information is saved as:
# /home/nanbuwks/Downloads/ardublock-beta-20170414/hs_err_pid23183.log
#
# If you would like to submit a bug report, please visit:
#   http://bugreport.java.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#
中止 (コアダンプ)

```

エスケープされた文字をデコードするプログラムをワンライナーで対処したり、

```
for textfile in $( ls . | grep properties$ ); do  cat  "${textfile}"  | sed -e 's/\\u\(....\)/\&#x\1;/g' | nkf --numcha
```
( src/main/resources/com/ardublock/block でこれを実行 )
 bg.DuinoEDU_infrared_compare.description を必要としているモジュールを読み込まないようにするためにskinをslimにしたり
```
 cd ./src/main/resources/com/ardublock/block/
 cp Skins/slim/* .
```
したが、コアダンプが発生するので openjdk-8 に変更した。

```

$  sudo apt-get remove openjdk-9-jdk
$  sudo apt-get remove openjdk-8-jdk
$  sudo apt-get install openjdk-8-jdk
$  sudo apt-get remove openjdk-9-jre
$  sudo apt-get install openjdk-8-jre
$ sudo apt-get remove openjdk-9-jre-headless

```


```
$ mvn compile exec:java -Dexec.mainClass="com.ardublock.Main"
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building ArduBlock 0.1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-install-plugin:2.5:install-file (install-arduino-pde) @ ardublock ---
[INFO] pom.xml not found in pde.jar
[INFO] Installing /home/nanbuwks/Downloads/ardublock-beta-20170414/lib/pde.jar to /home/nanbuwks/.m2/repository/arduino/pde/1.1/pde-1.1.jar
[INFO] Installing /tmp/mvninstall8717455682448856653.pom to /home/nanbuwks/.m2/repository/arduino/pde/1.1/pde-1.1.pom
[INFO] 
[INFO] --- maven-install-plugin:2.5:install-file (install-arduino-core) @ ardublock ---
[INFO] pom.xml not found in arduino-core.jar
[INFO] Installing /home/nanbuwks/Downloads/ardublock-beta-20170414/lib/arduino-core.jar to /home/nanbuwks/.m2/repository/arduino/arduino-core/1.6.12/arduino-core-1.6.12.jar
[INFO] Installing /tmp/mvninstall4011777015751712176.pom to /home/nanbuwks/.m2/repository/arduino/arduino-core/1.6.12/arduino-core-1.6.12.pom
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ ardublock ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 343 resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ ardublock ---
[INFO] Compiling 74 source files to /home/nanbuwks/Downloads/ardublock-beta-20170414/target/classes
[INFO] 
[INFO] --- exec-maven-plugin:1.6.0:java (default-cli) @ ardublock ---

```
通った

参考 https://ja.osdn.net/projects/akbone/wiki/HowToModifyArduBlock



![image](https://qiita-image-store.s3.amazonaws.com/0/139524/07a7df6a-13e7-43f5-1cf7-cca333cba147.png)

```

 mvn package
```
でjarファイルができるので、

Arduinoのスケッチなどを保存するディレクトリ( Ubuntuでは ~/Arduino/ )の ./tools/ArduBlockTool/tool

に /target/ardublock-all.jar をコピーする