34
32

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Scala初心者の俺が「Hello World」する jar を作るまで。

Last updated at Posted at 2013-07-31

ゴール

IDEの力を借りずに、「Hello World!」を出力するCUIプログラムをScalaで書いて、実行できて配布できるファイル形式 jar を作る。

要件

  • MacOSX で brew コマンドが使える OR もうすでに scalasbt が入っている

Scalaをインストールする

brew update

Scalaが入ってる場合は飛ばしてOK

brew install scala
$ scala -version
Scala code runner version 2.10.0 -- Copyright 2002-2012, LAMP/EPFL

SBT(Simple Build Tool)をインストールする

SBTが入っている場合は飛ばしてOK

brew install sbt
$ sbt --version
sbt launcher version 0.12.2

Hello Worldプロジェクトを作る

おもむろにDesktopにscala_hello_worldプロジェクトを作る:

cd ~/Desktop/
mkdir scala_hello_world
cd scala_hello_world/

sbt コマンドを実行する:

sbt

いろいろダウンロードされる

exit と打って抜ける

sbt update

またいろいろダウンロードされる

build.sbt を作り、下記のように書く。注意として、1行ごとに必ず空行を入れること。

build.sbt
name := "Hello World"

version := "1.0"

scalaVersion := "2.10.0"

SBTでの標準的なディレクトリ構成:

├── project
│   └── target
│       └── config-classes
├── src
│   ├── main
│   │   ├── resources
│   │   └── scala
│   └── test
│       ├── resources
│       └── scala
└── target

とりあえず src の下だけ作る:

mkdir -p src/{main,test}/{resources,scala}

src/main/scala/HelloWorld.scala を作って、中身を下記のように書く:

src/main/scala/HelloWorld.scala
object HelloWorld {
  def main(args: Array[String]) = {
    println("Hello World!")
  }
}

実行してみる

sbt run
[info] Set current project to Hello World (in build file:/Users/suin/Desktop/scala_hello_world/)
[info] Compiling 1 Scala source to /Users/suin/Desktop/scala_hello_world/target/scala-2.10/classes...
[info] Running HelloWorld
Hello World!
[success] Total time: 3 s, completed Jul 31, 2013 9:50:45 PM

Hello World! と出ればOK。

jarを作る

sbt-assembly というSBTのプラグインを使うと楽との情報を得たので使ってみる。ここに書いたセットアップ方法は sbt-assembly 0.9.2 のREADMEから持ってきたものなので、最新の情報はリンク先を参照してほしい。

project/plugins.sbt を作り、下記を追加する:

addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.9.1")

build.sbt に下の2行を追加する:

import AssemblyKeys._ // put this at the top of the file

assemblySettings

さっそく jar を作ってみる:

sbt assembly                                                                                                                                                                                   

jar を実行してみる:

java -jar target/scala-2.10/Hello\ World-assembly-1.0.jar

下のようにHello World!とだけ出れば成功。

Hello World!

jar のファイル名を hello_world.jar に変えたいので、build.sbt に設定を追加する:

jarName in assembly := "hello_world.jar"

jar を作りなおしてみる:

sbt assembly                                                                                                                                                                                   

実行してみる:

java -jar target/scala-2.10/hello_world.jar
Hello World!

hello_world-X.X.jar のようにバージョンつけたいので、 build.sbtjarName in assembly の設定を次のようにかえる。(もっとシンプルに書けるのかもしれないので知っていたら教えてほしい。)

jarName in assembly <<= (version) map { (version) => "hello_world-" + version + ".jar" }

jar を作り直して実行する:

sbt assembly          
java -jar target/scala-2.10/hello_world-1.0.jar                                                                                                                                                                         
34
32
6

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
34
32

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?