Help us understand the problem. What is going on with this article?

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

More than 1 year has passed since last update.

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)"
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした