SBTで自作のライブラリを作って、IntelliJで取り込む



### 自作ライブラリをSBTのプロジェクトで作成します

![スクリーンショット 2016-03-05 15.43.49.png](https://qiita-image-store.s3.amazonaws.com/0/84451/1ffb5410-f5d9-d7af-a305-f4e37ab0e44a.png)


### プロジェクト名は「my-utils」にしました
![スクリーンショット 2016-03-05 15.49.52.png](https://qiita-image-store.s3.amazonaws.com/0/84451/9b93396a-3879-87f6-445f-bd227dfa3272.png)

### パッケージ`com.example.nwtgck`を作ります
![スクリーンショット 2016-03-05 15.51.07.png](https://qiita-image-store.s3.amazonaws.com/0/84451/4762a17d-41ca-dd0b-0e5b-0db48070ab52.png)



### パッケージ`com.example.nwtgck`内にUtils.scalaを作成します

```scala:Utils.scala
package com.example.nwtgck

/**
  * Created by nwtgck on 2016/03/05.
  */
object Utils {
  def sayHello(name: String): Unit = println(s"Hello ${name}!")
  def sayGoodbye(name: String): Unit = println(s"Goodbye ${name}!")
}

```

![スクリーンショット 2016-03-05 15.53.48.png](https://qiita-image-store.s3.amazonaws.com/0/84451/af38de8b-31b7-b912-d702-70be1ef88f68.png)

### ターミナルを開き、プロジェクトのあるディレクトリ（今回はmy-utils）に`cd`してから`sbt publish-local`します

```sh
$ cd ~/IdeaProjects/my-utils/
$ sbt publish-local
```


```sh
# publish-localすると以下のようなものが表示されます
[info] Loading global plugins from /Users/nwtgck/.sbt/0.13/plugins/project
[info] Loading global plugins from /Users/nwtgck/.sbt/0.13/plugins
[info] Loading project definition from /Users/nwtgck/IdeaProjects/my-utils/project
[info] Set current project to my-utils (in build file:/Users/nwtgck/IdeaProjects/my-utils/)
[info] Packaging /Users/nwtgck/IdeaProjects/my-utils/target/scala-2.11/my-utils_2.11-1.0-sources.jar ...
[info] Main Scala API documentation to /Users/nwtgck/IdeaProjects/my-utils/target/scala-2.11/api...
[info] Compiling 1 Scala source to /Users/nwtgck/IdeaProjects/my-utils/target/scala-2.11/classes...
[info] Done packaging.
[info] Wrote /Users/nwtgck/IdeaProjects/my-utils/target/scala-2.11/my-utils_2.11-1.0.pom
[info] :: delivering :: default#my-utils_2.11;1.0 :: 1.0 :: release :: Sat Mar 05 15:58:10 JST 2016
[info] delivering ivy file to /Users/nwtgck/IdeaProjects/my-utils/target/scala-2.11/ivy-1.0.xml
model contains 5 documentable templates
[info] Main Scala API documentation successful.
[info] Packaging /Users/nwtgck/IdeaProjects/my-utils/target/scala-2.11/my-utils_2.11-1.0-javadoc.jar ...
[info] Done packaging.
[info] Packaging /Users/nwtgck/IdeaProjects/my-utils/target/scala-2.11/my-utils_2.11-1.0.jar ...
[info] Done packaging.
[info] published my-utils_2.11 to /Users/nwtgck/.ivy2/local/default/my-utils_2.11/1.0/poms/my-utils_2.11.pom
[info] published my-utils_2.11 to /Users/nwtgck/.ivy2/local/default/my-utils_2.11/1.0/jars/my-utils_2.11.jar
[info] published my-utils_2.11 to /Users/nwtgck/.ivy2/local/default/my-utils_2.11/1.0/srcs/my-utils_2.11-sources.jar
[info] published my-utils_2.11 to /Users/nwtgck/.ivy2/local/default/my-utils_2.11/1.0/docs/my-utils_2.11-javadoc.jar
[info] published ivy to /Users/nwtgck/.ivy2/local/default/my-utils_2.11/1.0/ivys/ivy.xml
[success] Total time: 2 s, completed 2016/03/05 15:58:12
```

すると、`$HOME/.ivy2/local/default`に`my-utils`ディレクトリが作成されます


### 自作ライブラリ(my-utils)を使いたいプロジェクトをSBTのプロジェクト作成して`build.sbt`に以下を追加します

```scala
libraryDependencies ++= Seq(
  "default" %% "my-utils" % "1.0"
)
```

![スクリーンショット 2016-03-05 16.08.21.png](https://qiita-image-store.s3.amazonaws.com/0/84451/bbfe2fa6-ab79-f772-4e8f-276ff1cffde4.png)

あとは「my-utils」を取り込むことができたので
`import com.example.nwtgck.Utils`が使えるようになっています

試しに、`Utils.sayHello`と`Utils.sayGoodbye`を使うと、

```scala
import com.example.nwtgck.Utils

/**
  * Created by nwtgck on 2016/03/05.
  */
object Main {
  def main(args: Array[String]) {
    Utils.sayHello("Jack")
    Utils.sayGoodbye("Jack")
  }
}
```

![スクリーンショット 2016-03-05 16.13.15.png](https://qiita-image-store.s3.amazonaws.com/0/84451/df343fff-d7e0-847c-3e05-85b2297b6efc.png)

IntelliJのコンソールに"Hello Jack!"と"Goodbye Jack!"が表示されうまく実行できました


### とても参考になったところ
* http://d.hatena.ne.jp/lranran123/20140424/1398299416