Bigdata, Scala / SBT

Jeonghak Cho·2025년 3월 20일

Bigdata

목록 보기
5/30

SBT 소개

SBT(Scala Build Tool)는 Scala 및 Java 프로젝트를 위한 빌드 도구로, 특히 Scala 생태계에서 널리 사용된다. SBT는 병렬 태스크 실행, 증분 컴파일, 플러그인 시스템 등을 지원하여 대규모 프로젝트에서도 효율적인 빌드 환경을 제공한다.

주요 특징

  • 증분 컴파일기능은 변경된 코드만 컴파일하여 속도를 최적화한다. 대규모 프로젝트에서 유용하다.
  • 병렬 태스크 실행으로 빌드 시간을 단축한다. 예: test와 compile을 동시에 실행 가능.
  • REPL(인터랙티브 쉘) 제공한다. sbt console 명령어를 통해 즉시 Scala 코드를 실행할 수 있다.
  • 플러그인 시스템을 활용하여 기능을 확장할 수 있다. 예: sbt-assembly(패키징), sbt-native-packager(Docker 지원) 등.

설치

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 packageJAR 파일 생성
sbt test테스트 실행
sbt clean빌드 결과물을 삭제
sbt assembly단일 JAR 파일(쉐이디 JAR) 생성 (플러그인 필요)
sbt console스칼라 REPL 실행
sbt publish패키지를 로컬 또는 원격 저장소에 배포
sbt dependencyTree의존성 트리 확인 (플러그인 필요)

Hello World

프로젝트 생성

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

build.sbt

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"

Main.scala

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

0개의 댓글