11/24 (日)
[Java] Maven の pom.xml のバージョンを Java のソースコードから参照する
pom.xml に書いているバージョン番号などを Java のアプリケーションから参照してログなどに埋め込むには、まずバージョン番号を JAR ファイルの中のマニュフェストファイルに書き込む必要がある。
Maven の一般的なビルドの場合は、maven-jar-plugin プラグインを渡す。 mvn package で JAR 化した後には情報が埋め込まれる。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>GROUPNAME</groupId> <artifactId>ARTIFACTID</artifactId> <packaging>jar</packaging> <version>0.0.1</version> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifest> <addDefaultImplementationEntries>true</addDefaultImplementationEntries> <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries> </manifest> </archive> </configuration> </plugin> </plugins> </build> </project>
Uber JAR を使って依存するライブラリまで含めた巨大な JAR を作る場合は maven-shade-plugin プラグインを用いるが、この場合は少し書き方が異なる。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>GROUPNAME</groupId> <artifactId>ARTIFACTID</artifactId> <packaging>jar</packaging> <version>0.0.1</version> <build> <plugins> <!-- for Uber JAR --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.2.1</version> <configuration> <createDependencyReducedPom>true</createDependencyReducedPom> <filters> <filter> <artifact>*:*</artifact> <excludes> <exclude>META-INF/*.SF</exclude> <exclude>META-INF/*.DSA</exclude> <exclude>META-INF/*.RSA</exclude> </excludes> </filter> </filters> </configuration> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" /> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <manifestEntries> <Main-Class>com.example.Main</Main-Class> <Specification-Title>${project.artifactId}</Specification-Title> <Specification-Version>${project.version}</Specification-Version> <Implementation-Title>${project.artifactId}</Implementation-Title> <Implementation-Version>${project.version}</Implementation-Version> <Implementation-Vendor-Id>${project.groupId}</Implementation-Vendor-Id> </manifestEntries> </transformer> </transformers> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
最終的には以下のような Java コードで取得が可能になる。
String version = com.example.Main.class.getPackage().getImplementationVersion();
11/23 (土)
Kong の設定を YAML でインポート・エクスポートする
Kong は REST API で設定を GET/POST する。 ユーザーが初期設定を与えたい場合、curl のようなコマンドで起動後に設定を書いていくが表現が冗長だ。 外部のユーザーが JSON で設定与えられる Gongfig のようなツールをわざわざ開発している。
Kong の公式もその状況を改善しようと 1.1 から kong config コマンド で db_export と db_import が追加され設定ファイルが YAML 形式でインポート・エクスポートできるようになった。
Kong を使って PostgreSQL のデータベースを初期化
# kong migrations bootstrap [-c /path/to/kong.conf]
まず kong config init を実行して YAML 形式の設定ファイルの雛型を作成する。
$ kong config init
これをいろいろ修正して設定ファイルを作成する。
変更後は kong config parse kong.yml でチェックが行えるが、どこまで検証してくれるか分らない。
$ kong config parse kong.yml
設定ファイルをデータベースに読み込むには、kong config db_import kong.yml を使う。
# kong config db_import kong.yml
設定ファイルをデータベースから書き込むには、kong config db_export output.yml を使う。 出力先のファイルが存在するとエラーになる。
# kong config db_export output.yml
[Movie] アナの雪の女王2
109 シネマズ川崎の『アナの雪の女王2』(原題: Frozen II)の 2D 字幕を観る。
11/13 (水)
Docker レジストリに push がエラーに
GitLab に付属している docker レジストリに外部からイメージを push しようとしたのだが、ファイル転送が終わった後になぜか unauthorized: authentication required とでる現象に苦しめられる。
$ sudo docker push registry.example.com/foo-bar-devel
The push refers to repository [registry.example.com/foo-bar-devel]
9bd5180e8006: Layer already exists
67d201b47fe4: Layer already exists
50e7105478cf: Layer already exists
b1a7534e55fc: Layer already exists
6f0cba13a859: Layer already exists
a51a307f336d: Layer already exists
aa8d150b2ddd: Layer already exists
a310c7027f49: Pushing [==================================================>] 2.317GB
c0677c3d7462: Layer already exists
f98f59599f73: Layer already exists
17948a9dee55: Layer already exists
03ef87ecbef4: Layer already exists
11e7bad206c4: Layer already exists
e1d13f901528: Layer already exists
ded8c689cfc5: Layer already exists
877b494a9f30: Layer already exists
unauthorized: authentication required
~/.docker/config.json の設定や Docker レジストリとクライアントの時刻の差があると失敗するなどいろいろな記事を読むが、結局 unauthorized: authentication required after docker login, then retrying, then unexpected EOF; using main account from cli (#770) Issues GitLab.com / GitLab.com Support Tracker GitLab のイシューを見て、イメージ内のレイヤーのサイズが大きすぎるのが原因だと気づく。 これをレイヤーに分けて 400 MB 以下に切ると、普通に push できるようになった。
Docker イメージをどのようにレイヤーで切ればいいかは
# docker history 4804e611131d IMAGE CREATED CREATED BY SIZE COMMENT 4804e611131d 24 hours ago /bin/sh -c #(nop) CMD ["/usr/sbin/init"] 0B 2ce619fe93a5 24 hours ago /bin/sh -c #(nop) VOLUME [/sys/fs/cgroup] 0B 67712016adc2 24 hours ago /bin/sh -c #(nop) EXPOSE 22 443 80 0B 31dd1b73b657 24 hours ago /bin/sh -c /build.sh 2.18GB e6ca15c60f4a 24 hours ago /bin/sh -c #(nop) COPY file:1c4ea5634ea90c06… 187B b3b6862b7cda 24 hours ago /bin/sh -c #(nop) COPY file:62f2d35ed142d673… 276B d8fadc29de0f 24 hours ago /bin/sh -c #(nop) COPY file:dd7ba865b7343f9d… 544B 88bf4a99b513 24 hours ago /bin/sh -c #(nop) COPY dir:19e4707a5f880d60d… 1.61kB ff2019556a39 24 hours ago /bin/sh -c #(nop) COPY file:55378413d2fe7650… 2.71kB d33d24400636 24 hours ago /bin/sh -c #(nop) COPY multi:1458159f54549a2… 5.24kB f813d693289d 24 hours ago /bin/sh -c (cd /lib/systemd/system/sysinit.t… 0B b1ee4f1f98c8 24 hours ago /bin/sh -c #(nop) ENV container=docker 0B ee6ba5e5e15a 24 hours ago /bin/sh -c #(nop) MAINTAINER nminoru@fujits… 0B 5e35e350aded 39 hours ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B >missing< 39 hours ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B >missing< 39 hours ago /bin/sh -c #(nop) ADD file:45a381049c52b5664… 203MB
11/10 (日)
[Movie] Re:ゼロから始める異世界生活 氷結の絆
TOHO シネマズ川崎の『Re:ゼロから始める異世界生活 氷結の絆』を観る。 前の映画版 Memory Snow からの直接の続編になっている。 エリオール大森林の永久凍土が映像として初めて登場したが、どうももう少し氷河のようなみっしりとした氷の中に埋まっているのだと思っていたよ…
11/2 (土)
[Movie] ジェミニマン
チネチッタで『ジェミニマン』(原題: Gemini Man)を観る。 主演はウィル・スミスだが、歳をとった方も若い方もウィル・スミスだったとは。 てっきり若い方はウィル・スミスの子供のジェイデン・スミスが演じているのではと疑っていた。