Groovyを始めよう!セットアップ手順

会社で後輩くんがGroovyに興味を示したので、
すかさず環境構築の手順を指南する。

とにかく、Groovyはすぐに始められる!
手順はたったこれだけ。

# sdkコマンドのインストール
curl -s "https://get.sdkman.io" | bash

# groovyのインストール
sdk install groovy
sdk install groovyserv

# 試してみる
echo 'println "hello"' > hoge.groovy
groovyclient hoge.groovy

sdkコマンドのインストール後は、
シェルを再起動する事で、.bashrc を再読み込みさせる必要がある。

SDKMANとは

インストールする事で、sdkコマンドが利用可能になる。
Groovyエコシステムのフレームワーク、ライブラリ等をパッケージ管理してくれる。

http://sdkman.io/

sdk list

インストール可能なパッケージをリスト表示してくれる。

sdk install

パッケージをインストールしてくれる。
同じパッケージでも異なるバージョンを選択してインストールする事も可能。

その場合、どのバージョンにパスを通すか聞かれる事になる。

sdk help

Usage: sdk <command> [candidate] [version]
       sdk offline <enable|disable>

   commands:
       install   or i    <candidate> [version]
       uninstall or rm   <candidate> <version>
       list      or ls   [candidate]
       use       or u    <candidate> [version]
       default   or d    <candidate> [version]
       current   or c    [candidate]
       outdated  or o    [candidate]
       version   or v
       broadcast or b
       help      or h
       offline           [enable|disable]
       selfupdate        [force]
       flush             <candidates|broadcast|archives|temp>

   candidate  :  the SDK to install: groovy, scala, grails, gradle, kotlin, etc.
                 use list command for comprehensive list of candidates
                 eg: $ sdk list

   version    :  where optional, defaults to latest stable if not provided
                 eg: $ sdk install groovy

GroovyServ

スクリプトとして動作するGroovyだが、
1つ欠点を挙げるとすると、起動が遅いこと。
JVMの起動とGroovyライブラリの初期解析に時間がかかるところが大きい。

この問題を回避する為に、groovyservというツールを使うのがベター。
JVMをデーモンとして、常時起動して再利用するというものだ。

groovyservをインストールすると以下の2つのコマンドが使えるようになる。
普段はgroovyclientを使えばよい。

groovyserver

JVMデーモンとして常駐するプロセスになる。

usage: groovyserver [options]
options:
  -h,--help                        show this usage
  -k,--kill                        kill the running server
  -r,--restart                     restart the running server
  -t,--timeout <second>            specify a timeout waiting for starting up a server process (default: 20 sec)
  -q,--quiet                       suppress statring messages
  -v,--verbose                     verbose output to a log file
  -p,--port <port>                 specify port to listen
     --allow-from <addresses>      specify optional acceptable client addresses (delimiter: comma)
     --authtoken <authtoken>       specify authtoken (which is automatically generated if not specified)
     --debug                       display console log

groovyclient

groovyコマンドの代わりにこのgroovyclientコマンドを使うと、
デーモンとなっているgroovyservを利用してスクリプト実行する。

初回だけ、groovyservの起動が走る。
次回以降はJVM起動が省略されているので早い。

usage: groovyclient -C[option for groovyclient] [args/options for groovy]
options:
  -Ch,-Chelp                       show this usage
  -Cs,-Chost                       specify host to connect to server
  -Cp,-Cport <port>                specify port to connect to server
  -Ca,-Cauthtoken <authtoken>      specify authtoken
  -Ck,-Ckill-server                kill the running server
  -Cr,-Crestart-server             restart the running server
  -Ct,-Ctimeout <second>           specify a timeout waiting for starting up a
                                   server process (default: 20 sec)
  -Cq,-Cquiet                      suppress statring messages
  -Cenv <substr>                   pass environment variables of which a name
                                   includes specified substr
  -Cenv-all                        pass all environment variables
  -Cenv-exclude <substr>           don't pass environment variables of which a
                                   name includes specified substr
  -Cv,-Cversion                    display the version
  -Ckeep-server-cwd                avoid to change directory to current working
                                   directory of client
  -Cdebug                          display console log

以下は、初回起動しているところ、
この出力が初回でもでるのが嫌な場合は、-Cq,-Cquiet オプションをつけるとよい。

Java home directory: /Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home
Groovy home directory: /Users/tyabuta/.sdkman/candidates/groovy/current
Groovy command path: /Users/tyabuta/.sdkman/candidates/groovy/current/bin/groovy (found at GROOVY_HOME)
GroovyServ home directory: /Users/tyabuta/.sdkman/candidates/groovyserv/1.1.0
GroovyServ work directory: /Users/tyabuta/.groovy/groovyserv
Original classpath: (none)
GroovyServ default classpath: /Users/tyabuta/.sdkman/candidates/groovyserv/1.1.0/lib/*
Starting server..........
Server is successfully started up on 1961 port

おしまい

GroovyはJavaさえ入っていれば、
簡単にインストールができるので試してみてほしい。

今のところインストール関連でつまづいた事はないので、かなり安定している印象。

これで君もGroovyistだ!!

コメントを残す