少年易酔學難成

IT/技術的な話題について書きます

続・JShellで外部ライブラリを追加しデバッグする方法

概要

JShellで外部ライブラリを追加しデバッグする方法の続きです。

前回の投稿では、以下の手順でApache Mavenを使ってデバッグに必要なライブラリをJShellに追加する方法を書きました。

  1. 使いたいライブラリを記載したpom.xmlを作成する
  2. 依存ライブラリをダウンロードし、クラスパスを出力する
  3. jshellに2で出力したクラスパスを食わせる

しかしサードパーティMavenプラグインを用いれば、2, 3の手順をまとめて実行できることがわかったので、今回はそのやり方を書きます。

Mavenプラグインを使ってライブラリをJShellに追加する方法

手順は以下のステップで行います。

  1. 使いたいライブラリを記載したpom.xmlを作成する
  2. jshell-maven-pluginを使ってJShellを起動する

前回と比較して、クラスパスをファイルに出力する必要がなくなり、ワンステップ短くなりました。

順番に見ていきましょう。

1. 使いたいライブラリを記載したpom.xmlを作成する

こちらは前回と同じなので特にいうことはありません。 必要なライブラリをdependenciesに追加してください。

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
 
  <groupId>example</groupId>
  <artifactId>miniodebug</artifactId>
  <version>1.0</version>
 
  <dependencies>
    <dependency>
      <groupId>io.minio</groupId>
      <artifactId>minio</artifactId>
      <version>8.2.1</version>
    </dependency>
  </dependencies>
</project>

2. jshell-maven-pluginを使ってJShellを起動する

mvn compile com.github.johnpoth:jshell-maven-plugin:1.3:runと打てば、プラグイン及び依存ライブラリのダウンロードが始まり、その後にJShellが起動します。

$ mvn compile com.github.johnpoth:jshell-maven-plugin:1.3:run
[INFO] Scanning for projects...
Downloading from central: https://repo.maven.apache.org/maven2/com/github/johnpoth/jshell-maven-plugin/1.3/jshell-maven-plugin-1.3.pom
Downloaded from central: https://repo.maven.apache.org/maven2/com/github/johnpoth/jshell-maven-plugin/1.3/jshell-maven-plugin-1.3.pom (4.7 kB at 4.1 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/com/github/johnpoth/jshell-maven-plugin/1.3/jshell-maven-plugin-1.3.jar
Downloaded from central: https://repo.maven.apache.org/maven2/com/github/johnpoth/jshell-maven-plugin/1.3/jshell-maven-plugin-1.3.jar (9.9 kB at 21 kB/s)
[INFO]
[INFO] -------------------------< example:miniodebug >-------------------------
[INFO] Building miniodebug 1.0
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ miniodebug ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/tsatow/works/miniodebug/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ miniodebug ---
[INFO] No sources to compile
[INFO]
[INFO] --- jshell-maven-plugin:1.3:run (default-cli) @ miniodebug ---
...
Downloaded from central: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-compiler-javac/2.8.2/plexus-compiler-javac-2.8.2.jar (20 kB at 17 kB/s)
Downloaded from central: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/3.1.0/plexus-utils-3.1.0.jar (262 kB at 187 kB/s)
[WARNING] Removing: /Users/tsatow/works/miniodebug/target/classes from the classpath.
If this is unexpected, please make sure you correctly build the project beforehand by invoking the correct Maven build phase (usually `install`, `test-compile` or `compile`). For example:
mvn test-compile com.github.johnpoth:jshell-maven-plugin:1.3:run
For more information visit https://github.com/johnpoth/jshell-maven-plugin
|  JShellへようこそ -- バージョン11.0.6
|  概要については、次を入力してください: /help intro

jshell> import io.minio.MinioClient

jshell> var minioClient = MinioClient.builder().endpoint("http://xxxxx:9000").credentials("user", "pass").build()
minioClient ==> io.minio.MinioClient@51399530

これで少しだけ手順が簡単になりました。

まとめ

前回に続き、JShellで外部ライブラリを追加しデバッグする方法について書きました。 jshell-maven-pluginを使うことによって、前回より1ステップ手順を減らすことができました。 こういうのはプラグイン化されているものですね。なぜ最初にプラグインを探さなかったのかと反省しています。