curlを通してSpring Initializrを利用する

2021.03.07

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

はじめに

Spring Initializrは条件に合わせてSpring Bootプロジェクトの雛形を生成するWebサービスです。Web UIからプロジェクト設定や利用するSpring Projectを指定することで、簡単にSpring Bootプロジェクトの開発を始めることができます。

Springの検証などで何度もプロジェクトをスクラップ&ビルドする場合、Web UIからでは操作の手間がかかります。 そこで、本記事ではcurlを利用してSpring InitializrのAPIにアクセスし、簡単にプロジェクトの雛形を取得します。

curlを使って雛形を作成する

とりあえず https://start.spring.io/ にcurlでアクセスしてみると、利用方法を案内するテキストが取得できます。

$ curl https://start.spring.io/
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
:: Spring Initializr ::  https://start.spring.io

This service generates quickstart projects that can be easily customized.
Possible customizations include a project's dependencies, Java version, and
build system or build structure. See below for further details.

...(snip)...

Examples:

To create a default demo.zip:
    $ curl https://start.spring.io/starter.zip -o demo.zip

To create a web project using Java 11:
    $ curl https://start.spring.io/starter.zip -d dependencies=web \
            -d javaVersion=11 -o demo.zip

To create a web/data-jpa gradle project unpacked:
    $ curl https://start.spring.io/starter.tgz -d dependencies=web,data-jpa \
           -d type=gradle-project -d baseDir=my-dir | tar -xzvf -

To generate a Maven POM with war packaging:
    $ curl https://start.spring.io/pom.xml -d packaging=war -o pom.xml

これに従ってパラメータを設定した状態でAPIにアクセスすれば、レスポンスとしてプロジェクトの雛形を取得することができます。

$ curl -s https://start.spring.io/starter.zip -d dependencies=web -d javaVersion=11 -o demo.zip
$ unzip demo.zip -d sample/
Archive:  demo.zip
  inflating: sample/.gitignore
  inflating: sample/HELP.md
  inflating: sample/mvnw
   creating: sample/src/
   creating: sample/src/main/
...(snip)...

jqを使ってパラメータを調べる

前述のテキストには設定パラメータの一覧や利用できるSpring Projectの一覧などが案内されていますが、詳細な設定値まではわかりません。リクエスト時にapplication/jsonを要求すると設定値の定義などの様々な情報を得ることができます。

例えば_links以下の要素を指定した場合、Spring Initializrがもつ他のAPIの情報を見ることができます。

$ curl -s -H "Accept: application/json" https://start.spring.io | jq ._links
{
  "maven-project": {
    "href": "https://start.spring.io/starter.zip?type=maven-project{&dependencies,packaging,javaVersion,language,bootVersion,groupId,artifactId,version,name,description,packageName}",
    "templated": true
  },
  "maven-build": {
    "href": "https://start.spring.io/pom.xml?type=maven-build{&dependencies,packaging,javaVersion,language,bootVersion,groupId,artifactId,version,name,description,packageName}",
    "templated": true
  },
  "gradle-project": {
    "href": "https://start.spring.io/starter.zip?type=gradle-project{&dependencies,packaging,javaVersion,language,bootVersion,groupId,artifactId,version,name,description,packageName}",
    "templated": true
  },
  "gradle-build": {
    "href": "https://start.spring.io/build.gradle?type=gradle-build{&dependencies,packaging,javaVersion,language,bootVersion,groupId,artifactId,version,name,description,packageName}",
    "templated": true
  },
  "dependencies": {
    "href": "https://start.spring.io/dependencies{?bootVersion}",
    "templated": true
  }
}

javaVersionを見れば選択できるJavaのバージョンやデフォルト値が分かります。

$ curl -s -H "Accept: application/json" https://start.spring.io | jq .javaVersion
{
  "type": "single-select",
  "default": "11",
  "values": [
    {
      "id": "15",
      "name": "15"
    },
    {
      "id": "11",
      "name": "11"
    },
    {
      "id": "1.8",
      "name": "8"
    }
  ]
}

頑張ればSpring Initializrのように選択できる依存パッケージの一覧を表示させることもできます。

$ curl -s -H "Accept: application/json" https://start.spring.io | jq '.dependencies.values[] | .name as $genre | .values[] as $pkg | "\($genre) >>>  \($pkg.name)"' | head -10
"Developer Tools >>>  Spring Boot DevTools"
"Developer Tools >>>  Lombok"
"Developer Tools >>>  Spring Configuration Processor"
"Web >>>  Spring Web"
"Web >>>  Spring Reactive Web"
"Web >>>  Rest Repositories"
"Web >>>  Spring Session"
"Web >>>  Rest Repositories HAL Explorer"
"Web >>>  Spring HATEOAS"
"Web >>>  Spring Web Services"

demoプロジェクトを作成するワンライナー

以上をもとにdemoプロジェクトを作成してIDEを立ち上げるワンライナーを作りました。

$ curl -s https://start.spring.io/starter.zip -d type=gradle-project -d dependencies=web -o demo.zip && unzip demo.zip -d demo && idea demo