Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple target directories in sbt project build

Tags:

scala

sbt

I have a sbt project in this structure:

.
├── build.sbt
├── project
│   ├── build.properties
│   └── plugins.sbt
└── src
    ├── main
    │   └── java
    │       └── smcho
    │           └── App.java
    └── test
        └── java
            └── smcho
                └── AppTest.java

With sbt package, I have three target directories, and target2 and target3 have a similar structure.

.
├── build.sbt
├── project
│   ├── build.properties
│   ├── plugins.sbt
│   ├── project
│   │   └── target <---------- TARGET 1
│   └── target     <---------- TARGET 2
│       ├── config-classes
│       ├── resolution-cache
│       ├── scala-2.10
│       └── streams
├── src
│   ├── main
│   │   └── java
│   └── test
│       └── java
└── target  <--------------- TARGET 3
    ├── resolution-cache
    │   ├── com.example
    │   └── reports
    ├── scala-2.11
    │   ├── classes
    │   └── hello_2.11-0.1.0.jar
    └── streams
        ├── $global
        └── compile

Why so? Interestingly, sbt clean does not remove the target directories, is there a way to have one simple target so that I can easily remove them?

This is build.sbt:

lazy val hello = taskKey[Unit]("An example task")
val junit = "junit" % "junit" % "4.11" % "test"

lazy val commonSettings = Seq( 
    organization := "com.example", 
    version := "0.1.0", 
    scalaVersion := "2.11.4"
)

lazy val root = (project in file(".")). 
    settings(
        commonSettings: _*
). 
    settings(
        hello := { println("Hello!") },
        name := "hello",
        libraryDependencies += junit
)
like image 752
prosseek Avatar asked Nov 24 '15 20:11

prosseek


1 Answers

Every project in SBT has a target directory. That's where its compiled classes and other generated things go.

Your root is a project, and TARGET 3 in your diagram is its target.

Your build definition (the project directory) is also a project. With SBT it is possible to write scala code to implement build-related tasks and settings. That compiled code has to go somewhere. It goes in the directory you labeled as TARGET 2 - project/target.

Build definitions in SBT can be recursive, i.e. your build definition can have its own build definition. Since you are using plugins (defined in project/plugins.sbt), your build definition needs a build definition, which ends up being compiled to project/project/target aka TARGET 1 in your diagram.

When you run clean in the SBT console, it will clean files from the current project's target directory. I don't think it's meant to delete the entire directory, but I could be wrong there. In any case, running clean while you have the root project selected should only affect the root project's target.

In the SBT console, you can run reload plugins or reload return to jump into and out of (respectively) the current project's build definition. Calling clean within that context would clean their respective targets.

As for combining them into a single, easily-removed directory, I'm not sure I see the value in that. Having used SBT for several years now, the various target dirs haven't ever really been in the way. I don't think I've even wanted to delete the target directory a single time in the last year or so.

like image 191
Dylan Avatar answered Nov 15 '22 21:11

Dylan