LoginSignup
1
0

More than 5 years have passed since last update.

sbtプロジェクト作成手順(Mac限定)

Posted at

ScalaをインストールせずにsbtのみでScalaを実行できる環境手順をメモ書き程度に書きます。

前提条件

  • JDK 8
  • homebrew

sbtのインストール

$ brew install sbt

プロジェクトの作成

$ sbt new scala/scala-seed.g8

以下は「hello-scala」プロジェクトを作成した例

$ sbt new scala/scala-seed.g8
[info] Loading project definition from /Users/yoshikawayuki/workspace/project
[info] Set current project to workspace (in build file:/Users/yoshikawayuki/workspace/)

A minimal Scala project. 

name [Scala Seed Project]: hello-scala <-名前を入力する

Template applied in ./hello-scala

対象クラスの実行方法

上記でプロジェクトを作成した場合にHello.scalaがデフォルトで作成されます。そのファイルを実行したい場合は以下のコマンドを入力すれば、Hello.scalaが実行される

sbt "runMain example.Hello"

Hello.scala

package example

object Hello extends Greeting with App {
  println(greeting)
}

trait Greeting {
  lazy val greeting: String = "hello"
}

実行結果

$ sbt "runMain example.Hello"
[info] Loading project definition from /Users/yoshikawayuki/workspace/hello-scala/project
[info] Updating ProjectRef(uri("file:/Users/yoshikawayuki/workspace/hello-scala/project/"), "hello-scala-build")...
[info] Done updating.
[info] Compiling 1 Scala source to /Users/yoshikawayuki/workspace/hello-scala/project/target/scala-2.12/sbt-1.0/classes ...
[info] Done compiling.
[info] Loading settings from build.sbt ...
[info] Set current project to hello-scala (in build file:/Users/yoshikawayuki/workspace/hello-scala/)
[info] Updating ...
[info] Done updating.
[info] Compiling 1 Scala source to /Users/yoshikawayuki/workspace/hello-scala/target/scala-2.12/classes ...
[info] Done compiling.
[info] Packaging /Users/yoshikawayuki/workspace/hello-scala/target/scala-2.12/hello-scala_2.12-0.1.0-SNAPSHOT.jar ...
[info] Done packaging.
[info] Running example.Hello 
hello
[success] Total time: 1 s, completed Feb 18, 2018 7:22:09 PM

留意点

sbtを既にインストール済みでアップグレードをする場合、brew updateをした方が良いが、Mac OS High Sierra の場合、以下のエラーが発生する。

/usr/local must be writable!

その場合はhomebrew 再インストールすれば良い。

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
1
0
0

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
1
0