SBT(Scala Build Tool)는 Scala 및 Java 프로젝트를 위한 빌드 도구로, 특히 Scala 생태계에서 널리 사용된다. SBT는 병렬 태스크 실행, 증분 컴파일, 플러그인 시스템 등을 지원하여 대규모 프로젝트에서도 효율적인 빌드 환경을 제공한다.
wget https://github.com/sbt/sbt/releases/download/v1.9.7/sbt-1.9.7.tgz
tar -xvzf sbt-1.9.7.tgz
sudo mv sbt /usr/local/sbt
echo 'export PATH=/usr/local/sbt/bin:$PATH' >> ~/.bashrc
source ~/.bashrc
vagrant@slave2:~$ sbt sbtVersion
[info] Updated file /home/vagrant/project/build.properties: set sbt.version to 1.9.7
[info] welcome to sbt 1.9.7 (Ubuntu Java 11.0.26)
[info] loading project definition from /home/vagrant/project
[info] set current project to vagrant (in build file:/home/vagrant/)
[info] 1.9.7
| 명령어 | 설명 |
|---|---|
sbt new | 새로운 프로젝트를 생성 (예: sbt new scala/hello-world.g8) |
sbt compile | 프로젝트를 컴파일 |
sbt run | 애플리케이션 실행 |
sbt package | JAR 파일 생성 |
sbt test | 테스트 실행 |
sbt clean | 빌드 결과물을 삭제 |
sbt assembly | 단일 JAR 파일(쉐이디 JAR) 생성 (플러그인 필요) |
sbt console | 스칼라 REPL 실행 |
sbt publish | 패키지를 로컬 또는 원격 저장소에 배포 |
sbt dependencyTree | 의존성 트리 확인 (플러그인 필요) |
Giter8(G8)은 Scala 프로젝트 템플릿을 생성하는 도구로, 깃허브에서 템플릿을 가져와 자동으로 프로젝트를 생성할 수 있다.
Giter8(G8)은 Scala 프로젝트 템플릿 생성 도구이며, sbt new 명령어에서 사용된다. 다양한 템플릿을 사용하여 빠르게 프로젝트를 시작할 수 있다. sbt new <github_user/repository.g8> 형식으로 원하는 템플릿 사용 가능하다.
vagrant@slave2:~$ sbt new scala/hello-world.g8
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
A template to demonstrate a minimal Scala application
name [Hello World template]:
Template applied in /home/vagrant/./hello-world-template
vagrant@slave2:~/hello-world-template$ tree
.
├── build.sbt
├── project
│ └── build.properties
└── src
└── main
└── scala
└── Main.scala
4 directories, 3 files
SBT 프로젝트에서 build.sbt 파일은 의존성 관리 및 빌드 설정을 담당한다.
scalaVersion := "2.13.12"
name := "hello-world"
organization := "ch.epfl.scala"
version := "1.0"
libraryDependencies += "org.scala-lang.modules" %% "scala-parser-combinators" % "2.3.0"
Hello World 소스 코드이다.
object Main extends App {
println("Hello, World!")
}
vagrant@slave2:~$ cd hello-world-template
vagrant@slave2:~/hello-world-template$ sbt run
[info] welcome to sbt 1.10.11 (Ubuntu Java 11.0.26)
[info] loading project definition from /home/vagrant/hello-world-template/project
[info] loading settings for project hello-world-template from build.sbt...
[info] set current project to hello-world (in build file:/home/vagrant/hello-world-template/)
[info] compiling 1 Scala source to /home/vagrant/hello-world-template/target/scala-2.13/classes ...
[info] running Main
Hello, World!
[success] Total time: 4 s, completed Mar 20, 2025, 12:29:32 AM
sbt는 기본적으로 새로운 프로세스를 실행할 때마다 초기화 과정을 거쳐야 한다. 초기화 과정은 JVM 부팅, 프로젝트 메타데이터 로드, 의존성 확인 및 로드가 있다.
-Dsbt.server.forcestart=true 옵션을 사용하면, sbt 서버 모드가 활성화되어 백그라운드에서 sbt 프로세스를 유지하므로 이후 빌드 실행 속도가 빨라진다.
vagrant@slave2:~/hello-world-template$ sbt -Dsbt.server.forcestart=true
[info] welcome to sbt 1.10.11 (Ubuntu Java 11.0.26)
[info] loading project definition from /home/vagrant/hello-world-template/project
[info] loading settings for project hello-world-template from build.sbt...
[info] set current project to hello-world (in build file:/home/vagrant/hello-world-template/)
[info] sbt server started at local:///home/vagrant/.sbt/1.0/server/f7101bbe4730ae8abf79/sock
[info] started sbt server
sbt:hello-world> run
[info] running Main
Hello, World!
[success] Total time: 1 s, completed Mar 20, 2025, 12:29:51 AM
패키징 시 jar 파일이 생성된다.
vagrant@slave2:~/hello-world-template$ sbt package
[info] welcome to sbt 1.10.11 (Ubuntu Java 11.0.26)
[info] loading project definition from /home/vagrant/hello-world-template/project
[info] loading settings for project hello-world-template from build.sbt...
[info] set current project to hello-world (in build file:/home/vagrant/hello-world-template/)
[info] compiling 1 Scala source to /home/vagrant/hello-world-template/target/scala-2.13/classes ...
[success] Total time: 4 s, completed Mar 20, 2025, 12:50:58 AM
vagrant@slave2:~/hello-world-template$ scala target/scala-2.13/hello-world_2.13-1.0.jar
Hello, World!
build.sbt에 의존성 추가
libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.15" % Test
테스트 실행
vagrant@slave2:~/hello-world-template$ sbt test
...
[info] Fetched artifacts of hello-world_2.13
[info] compiling 1 Scala source to /home/vagrant/hello-world-template/target/scala-2.13/test-classes ...
[info] HelloSpec:
[info] - Hello World test
[info] Run completed in 351 milliseconds.
[info] Total number of tests run: 1
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 1, failed 0, canceled 0, ignored 0, pending 0
[info] All tests passed.
[success] Total time: 11 s, completed Mar 20, 2025, 12:58:53 AM