SBT Reference
SBT Reference
Contents
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Install . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Features of sbt . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Also . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
1
Exiting sbt shell . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Build definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Directory structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Base directory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Source code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
sbt build definition files . . . . . . . . . . . . . . . . . . . . . . . 31
Build support files . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Build products . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Configuring version control . . . . . . . . . . . . . . . . . . . . . 32
Running . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
sbt shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Batch mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Continuous build and test . . . . . . . . . . . . . . . . . . . . . . 33
Common commands . . . . . . . . . . . . . . . . . . . . . . . . . 33
Tab completion . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
History Commands . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Build definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Specifying the sbt version . . . . . . . . . . . . . . . . . . . . . . 35
What is a build definition? . . . . . . . . . . . . . . . . . . . . . 35
How build.sbt defines settings . . . . . . . . . . . . . . . . . . . . 36
Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Defining tasks and settings . . . . . . . . . . . . . . . . . . . . . 38
Keys in sbt shell . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Imports in build.sbt . . . . . . . . . . . . . . . . . . . . . . . . . 39
Adding library dependencies . . . . . . . . . . . . . . . . . . . . . 39
Task graph . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Terminology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Declaring dependency to other tasks . . . . . . . . . . . . . . . . 41
Inlining .value calls . . . . . . . . . . . . . . . . . . . . . . . . . . 43
What’s the point of the build.sbt DSL? . . . . . . . . . . . . . . 46
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
2
Scopes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
The whole story about keys . . . . . . . . . . . . . . . . . . . . . 48
Scope axes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Referring to scopes in a build definition . . . . . . . . . . . . . . 52
Referring to scoped keys from the sbt shell . . . . . . . . . . . . 53
Examples of scoped key notation . . . . . . . . . . . . . . . . . . 53
Inspecting scopes . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
When to specify a scope . . . . . . . . . . . . . . . . . . . . . . . 55
Build-level settings . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Scope delegation . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
Appending values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Appending to previous values: += and ++= . . . . . . . . . . . . . 57
Appending with dependencies: += and ++= . . . . . . . . . . . . . 58
Scope delegation (.value lookup) . . . . . . . . . . . . . . . . . . . . . 58
Scope delegation rules . . . . . . . . . . . . . . . . . . . . . . . . 59
Rule 1: Scope axis precedence . . . . . . . . . . . . . . . . . . . . 60
Rule 2: The task axis delegation . . . . . . . . . . . . . . . . . . 60
Rule 3: The configuration axis search path . . . . . . . . . . . . . 61
Rule 4: The subproject axis search path . . . . . . . . . . . . . . 61
Inspect command lists the delegates . . . . . . . . . . . . . . . . 63
.value lookup vs dynamic dispatch . . . . . . . . . . . . . . . . . 64
Library dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Unmanaged dependencies . . . . . . . . . . . . . . . . . . . . . . 67
Managed Dependencies . . . . . . . . . . . . . . . . . . . . . . . 68
Multi-project builds . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Multiple subprojects . . . . . . . . . . . . . . . . . . . . . . . . . 71
Dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
Default root project . . . . . . . . . . . . . . . . . . . . . . . . . 74
Navigating projects interactively . . . . . . . . . . . . . . . . . . 74
Common code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Using plugins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
3
What is a plugin? . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Declaring a plugin . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Enabling and disabling auto plugins . . . . . . . . . . . . . . . . 76
Global plugins . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Available Plugins . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
Custom settings and tasks . . . . . . . . . . . . . . . . . . . . . . . . . 78
Defining a key . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
Implementing a task . . . . . . . . . . . . . . . . . . . . . . . . . 79
Execution semantics of tasks . . . . . . . . . . . . . . . . . . . . 80
Turn them into plugins . . . . . . . . . . . . . . . . . . . . . . . . 84
Organizing the build . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
sbt is recursive . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
Tracking dependencies in one place . . . . . . . . . . . . . . . . . 85
When to use .scala files . . . . . . . . . . . . . . . . . . . . . . 86
Defining auto plugins . . . . . . . . . . . . . . . . . . . . . . . . . 86
Getting Started summary . . . . . . . . . . . . . . . . . . . . . . . . . 87
sbt: The Core Concepts . . . . . . . . . . . . . . . . . . . . . . . 87
Advanced Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
Appendix: Bare .sbt build definition . . . . . . . . . . . . . . . . . . . 88
What is a bare .sbt build definition . . . . . . . . . . . . . . . . . 88
(Pre 0.13.7) Settings must be separated by blank lines . . . . . . 88
Appendix: .scala build definition . . . . . . . . . . . . . . . . . . . . . 88
Relating build.sbt to Build.scala . . . . . . . . . . . . . . . . . . 89
The build definition project in interactive mode . . . . . . . . . . 91
Reminder: it’s all immutable . . . . . . . . . . . . . . . . . . . . 91
General Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
Credits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
Community Plugins . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
sbt Organization . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
Community Ivy Repository . . . . . . . . . . . . . . . . . . . . . 93
Available Plugins . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
4
Test plugins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
Community Repository Policy . . . . . . . . . . . . . . . . . . . . . . . 103
Bintray For Plugins . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
Create an Open Source Distribution account on Bintray . . . . . 104
Create a repository for your sbt plugins . . . . . . . . . . . . . . 104
Add the sbt-bintray plugin to your build. . . . . . . . . . . . . . 105
Make a release . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
Linking your package to the sbt organization . . . . . . . . . . . 106
Linking your package to the sbt organization (sbt org admins) . 106
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
Setup Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
Do not put sbt-launch.jar on your classpath. . . . . . . . . . . 107
Terminal encoding . . . . . . . . . . . . . . . . . . . . . . . . . . 107
JVM heap, permgen, and stack sizes . . . . . . . . . . . . . . . . 107
Boot directory . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
HTTP/HTTPS/FTP Proxy . . . . . . . . . . . . . . . . . . . . . 108
Using Sonatype . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
Sonatype setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
SBT setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
Second - Configure Sonatype integration . . . . . . . . . . . . . . 110
Third - Publish to the staging repository . . . . . . . . . . . . . . 112
Fourth - Integrate with the release process . . . . . . . . . . . . . 113
Contributing to sbt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
Changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
Migrating from sbt 0.12.x . . . . . . . . . . . . . . . . . . . . . . . . . 115
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
Migrating simple expressions . . . . . . . . . . . . . . . . . . . . 115
Migrating from the tuple enrichments . . . . . . . . . . . . . . . 116
Migrating when using .dependsOn, .triggeredBy or .runBefore 117
Migrating when you need to set Tasks . . . . . . . . . . . . . . . 117
5
Migrating with InputKey . . . . . . . . . . . . . . . . . . . . . . 118
Migrating from the Build trait . . . . . . . . . . . . . . . . . . . 118
sbt 0.13.5+ Technology Previews . . . . . . . . . . . . . . . . . . . . . 120
sbt 0.13.17 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
Improvements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
Bug fixes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
Internal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
sbt 0.13.16 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
Fixes with compatibility implications . . . . . . . . . . . . . . . . 121
Improvements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
Bug fixes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
sbt-cross-building . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
Improvements and bug fixes to the startup messages . . . . . . . 122
sbt 0.13.15 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
Fixes with compatibility implications . . . . . . . . . . . . . . . . 123
Improvements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
Bug fixes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
Maven version range improvement . . . . . . . . . . . . . . . . . 124
Offline installation . . . . . . . . . . . . . . . . . . . . . . . . . . 124
Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
sbt 0.13.14 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
sbt 0.13.13 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
Fixes with compatibility implications . . . . . . . . . . . . . . . . 125
Improvements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
Bug fixes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
new command and templateResolverInfos . . . . . . . . . . . . . 126
Synthetic subprojects . . . . . . . . . . . . . . . . . . . . . . . . . 127
Deprecate old sbt 0.12 DSL . . . . . . . . . . . . . . . . . . . . . 128
sbt 0.13.12 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Fixes with compatibility implications . . . . . . . . . . . . . . . . 129
Improvements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
6
Bug fixes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
sbt 0.13.11 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
Fixes with compatibility implications . . . . . . . . . . . . . . . . 130
Improvements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
Bug fixes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Configurable Scala compiler bridge . . . . . . . . . . . . . . . . . 132
Dotty awareness . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
Inter-project dependency tracking . . . . . . . . . . . . . . . . . . 133
Def.settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
sbt 0.13.10 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
sbt 0.13.9 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
Fixes with compatibility implications . . . . . . . . . . . . . . . . 134
Improvements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
Bug fixes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
crossScalaVersions default value . . . . . . . . . . . . . . . . . 135
POM files no longer include certain source and javadoc jars . . . 136
retrieveManaged related improvements . . . . . . . . . . . . . . 136
Cached resolution fixes . . . . . . . . . . . . . . . . . . . . . . . . 136
Force GC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
Maven compatibility fix . . . . . . . . . . . . . . . . . . . . . . . 137
sbt 0.13.8 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
Changes with compatibility implications . . . . . . . . . . . . . . 137
Improvements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
Fixes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
Rolling back XML parsing workaround . . . . . . . . . . . . . . . 138
Cross-version support for Scala sources . . . . . . . . . . . . . . . 139
Maven resolver plugin . . . . . . . . . . . . . . . . . . . . . . . . 139
Project-level dependency exclusions . . . . . . . . . . . . . . . . . 140
Sequential tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
Nicer ways of declaring project settings . . . . . . . . . . . . . . 141
Bytecode Enhancers . . . . . . . . . . . . . . . . . . . . . . . . . 141
7
sbt 0.13.7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
Fixes with compatibility implications . . . . . . . . . . . . . . . . 142
Improvements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
Bug fixes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
Natural whitespace handling . . . . . . . . . . . . . . . . . . . . 144
Custom Maven local repository location . . . . . . . . . . . . . . 144
Circular dependency . . . . . . . . . . . . . . . . . . . . . . . . . 144
Cached resolution (minigraph caching) . . . . . . . . . . . . . . . 144
sbt 0.13.6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
Fixes with compatibility implications . . . . . . . . . . . . . . . . 145
Improvements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
Bug fixes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
HTTPS related changes . . . . . . . . . . . . . . . . . . . . . . . 147
enablePlugins/disablePlugins . . . . . . . . . . . . . . . . . . . . 147
Unresolved dependencies error . . . . . . . . . . . . . . . . . . . . 147
Eviction warnings . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
Latest SNAPSHOTs . . . . . . . . . . . . . . . . . . . . . . . . . 148
Consolidated resolution . . . . . . . . . . . . . . . . . . . . . . . 149
sbt 0.13.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
sbt 0.13.0 - 0.13.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
sbt 0.13.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
sbt 0.13.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
sbt 0.13.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
Details of major changes . . . . . . . . . . . . . . . . . . . . . . . 154
sbt 0.12.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
sbt 0.12.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
sbt 0.12.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
sbt 0.12.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
Dependency management fixes: . . . . . . . . . . . . . . . . . . . 161
Three stage incremental compilation: . . . . . . . . . . . . . . . . 161
Miscellaneous fixes and improvements: . . . . . . . . . . . . . . . 162
8
Forward-compatible-only change (not present in 0.12.0): . . . . . 162
sbt 0.12.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
Details of major changes from 0.11.2 to 0.12.0 . . . . . . . . . . . 164
scala-library.jar . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
Older Changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
0.11.3 to 0.12.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
0.11.2 to 0.11.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
0.11.1 to 0.11.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
0.11.0 to 0.11.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
0.10.1 to 0.11.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
0.10.0 to 0.10.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
0.7.7 to 0.10.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
0.7.5 to 0.7.7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
0.7.4 to 0.7.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
0.7.3 to 0.7.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
0.7.2 to 0.7.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
0.7.1 to 0.7.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
0.7.0 to 0.7.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
0.5.6 to 0.7.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
0.5.5 to 0.5.6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
0.5.4 to 0.5.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
0.5.2 to 0.5.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
0.5.1 to 0.5.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
0.4.6 to 0.5/0.5.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
0.4.5 to 0.4.6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
0.4.3 to 0.4.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
0.4 to 0.4.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
0.3.7 to 0.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
0.3.6 to 0.3.7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
0.3.5 to 0.3.6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
0.3.2 to 0.3.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
9
0.3.1 to 0.3.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
0.3 to 0.3.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
0.2.3 to 0.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
0.2.2 to 0.2.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
0.2.1 to 0.2.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
0.2.0 to 0.2.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
0.1.9 to 0.2.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
0.1.8 to 0.1.9 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
0.1.7 to 0.1.8 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
0.1.6 to 0.1.7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
0.1.5 to 0.1.6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
0.1.4 to 0.1.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
0.1.3 to 0.1.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
0.1.2 to 0.1.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
0.1.1 to 0.1.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
0.1 to 0.1.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
Migrating from 0.7 to 0.10+ . . . . . . . . . . . . . . . . . . . . . . . . 191
Why move to 0.13.16? . . . . . . . . . . . . . . . . . . . . . . . . 192
Preserve project/ for 0.7.x project . . . . . . . . . . . . . . . . . 192
Create build.sbt for 0.13.16 . . . . . . . . . . . . . . . . . . . . 192
Run sbt 0.13.16 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
Switching back to sbt 0.7.x . . . . . . . . . . . . . . . . . . . . . 193
Detailed Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
Using sbt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
Command Line Reference . . . . . . . . . . . . . . . . . . . . . . . . . 194
Notes on the command line . . . . . . . . . . . . . . . . . . . . . 194
Project-level tasks . . . . . . . . . . . . . . . . . . . . . . . . . . 194
Configuration-level tasks . . . . . . . . . . . . . . . . . . . . . . . 195
General commands . . . . . . . . . . . . . . . . . . . . . . . . . . 196
Commands for managing the build definition . . . . . . . . . . . 197
Command Line Options . . . . . . . . . . . . . . . . . . . . . . . 198
10
Console Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
Accessing settings . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
Evaluating tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
Cross-building . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
Publishing Conventions . . . . . . . . . . . . . . . . . . . . . . . 201
Using Cross-Built Libraries . . . . . . . . . . . . . . . . . . . . . 202
Cross-Building a Project . . . . . . . . . . . . . . . . . . . . . . . 202
Interacting with the Configuration System . . . . . . . . . . . . . . . . 204
Selecting commands, tasks, and settings . . . . . . . . . . . . . . 204
Discovering Settings and Tasks . . . . . . . . . . . . . . . . . . . 206
Triggered Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
Compile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
Running Multiple Commands . . . . . . . . . . . . . . . . . . . . 210
Scripts, REPL, and Dependencies . . . . . . . . . . . . . . . . . . . . . 210
Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
Understanding Incremental Recompilation . . . . . . . . . . . . . . . . 213
sbt heuristics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
How to take advantage of sbt heuristics . . . . . . . . . . . . . . 215
Implementation of incremental recompilation . . . . . . . . . . . . . . 215
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
Interaction with the Scala compiler . . . . . . . . . . . . . . . . . 217
Name hashing algorithm . . . . . . . . . . . . . . . . . . . . . . . 219
What is included in the interface of a Scala class . . . . . . . . . 225
Further references . . . . . . . . . . . . . . . . . . . . . . . . . . 229
Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
Classpaths, sources, and resources . . . . . . . . . . . . . . . . . . . . 229
11
Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
Compiler Plugin Support . . . . . . . . . . . . . . . . . . . . . . . . . 232
Continuations Plugin Example . . . . . . . . . . . . . . . . . . . 233
Version-specific Compiler Plugin Example . . . . . . . . . . . . . 233
Configuring Scala . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
Automatically managed Scala . . . . . . . . . . . . . . . . . . . . 233
Using Scala from a local directory . . . . . . . . . . . . . . . . . 235
sbt’s Scala version . . . . . . . . . . . . . . . . . . . . . . . . . . 236
Forking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
Enable forking . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
Change working directory . . . . . . . . . . . . . . . . . . . . . . 237
Forked JVM options . . . . . . . . . . . . . . . . . . . . . . . . . 238
Java Home . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
Configuring output . . . . . . . . . . . . . . . . . . . . . . . . . . 239
Configuring Input . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
Direct Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
Global Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240
Basic global configuration file . . . . . . . . . . . . . . . . . . . . 240
Global Settings using a Global Plugin . . . . . . . . . . . . . . . 240
Java Sources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
Known issues in mixed mode compilation . . . . . . . . . . . . . 242
Ignoring the Scala source directories . . . . . . . . . . . . . . . . 242
Mapping Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
Relative to a directory . . . . . . . . . . . . . . . . . . . . . . . . 243
Rebase . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
Flatten . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
Alternatives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
Local Scala . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
Macro Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
12
Defining the Project Relationships . . . . . . . . . . . . . . . . . 246
Common Interface . . . . . . . . . . . . . . . . . . . . . . . . . . 248
Distribution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
Paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
Constructing a File . . . . . . . . . . . . . . . . . . . . . . . . . . 249
Path Finders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
File Filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
Parallel Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
Task ordering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
Practical constraints . . . . . . . . . . . . . . . . . . . . . . . . . 254
Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
Future work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258
External Processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
Running Project Code . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
sbt’s Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
Additional test configurations . . . . . . . . . . . . . . . . . . . . 267
JUnit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
Dependency Management . . . . . . . . . . . . . . . . . . . . . . . . . 272
Artifacts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272
Selecting default artifacts . . . . . . . . . . . . . . . . . . . . . . 272
Modifying default artifacts . . . . . . . . . . . . . . . . . . . . . . 273
Defining custom artifacts . . . . . . . . . . . . . . . . . . . . . . 274
Publishing .war files . . . . . . . . . . . . . . . . . . . . . . . . . 275
Using dependencies with artifacts . . . . . . . . . . . . . . . . . . 275
13
Dependency Management Flow . . . . . . . . . . . . . . . . . . . . . . 276
Background . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
Caching and Configuration . . . . . . . . . . . . . . . . . . . . . 276
General troubleshooting steps . . . . . . . . . . . . . . . . . . . . 277
Plugins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278
Library Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278
Manual Dependency Management . . . . . . . . . . . . . . . . . 278
Automatic Dependency Management . . . . . . . . . . . . . . . . 279
Proxy Repositories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
sbt Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
~/.sbt/repositories . . . . . . . . . . . . . . . . . . . . . . . . 292
Proxying Ivy Repositories . . . . . . . . . . . . . . . . . . . . . . 293
Publishing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
Define the repository . . . . . . . . . . . . . . . . . . . . . . . . . 293
Credentials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
Cross-publishing . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
Published artifacts . . . . . . . . . . . . . . . . . . . . . . . . . . 296
Modifying the generated POM . . . . . . . . . . . . . . . . . . . 296
Publishing Locally . . . . . . . . . . . . . . . . . . . . . . . . . . 296
Resolvers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
Maven . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
Predefined . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
Custom . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
Update Report . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
Filtering a Report and Getting Artifacts . . . . . . . . . . . . . . 301
Cached resolution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304
Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
Dependency as a graph . . . . . . . . . . . . . . . . . . . . . . . 305
14
Cached resolution . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
Caveats and known issues . . . . . . . . . . . . . . . . . . . . . . 306
Motivation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
Tasks and Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . 308
Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308
Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308
Defining a Task . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
Getting values from multiple scopes . . . . . . . . . . . . . . . . 312
Advanced Task Operations . . . . . . . . . . . . . . . . . . . . . 315
Dynamic Computations with Def.taskDyn . . . . . . . . . . . . 316
Using Def.sequential . . . . . . . . . . . . . . . . . . . . . . . . . 317
Handling Failure . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
Input Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322
Input Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322
Basic Input Task Definition . . . . . . . . . . . . . . . . . . . . . 323
Input Task using Parsers . . . . . . . . . . . . . . . . . . . . . . . 323
The InputTask type . . . . . . . . . . . . . . . . . . . . . . . . . 324
Using other input tasks . . . . . . . . . . . . . . . . . . . . . . . 325
Preapplying input . . . . . . . . . . . . . . . . . . . . . . . . . . 326
Get a Task from an InputTask . . . . . . . . . . . . . . . . . . . 327
Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328
What is a “command”? . . . . . . . . . . . . . . . . . . . . . . . 328
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329
Defining a Command . . . . . . . . . . . . . . . . . . . . . . . . . 329
Full Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330
Parsing and tab completion . . . . . . . . . . . . . . . . . . . . . . . . 332
Basic parsers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333
Built-in parsers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333
Combining parsers . . . . . . . . . . . . . . . . . . . . . . . . . . 334
Transforming results . . . . . . . . . . . . . . . . . . . . . . . . . 334
15
Controlling tab completion . . . . . . . . . . . . . . . . . . . . . 335
Dependent parsers . . . . . . . . . . . . . . . . . . . . . . . . . . 336
State and actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336
Command-related data . . . . . . . . . . . . . . . . . . . . . . . . 337
Project-related data . . . . . . . . . . . . . . . . . . . . . . . . . 338
Project data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338
Classpaths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
Running tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
Using State in a task . . . . . . . . . . . . . . . . . . . . . . . . . 340
Tasks/Settings: Motivation . . . . . . . . . . . . . . . . . . . . . . . . 340
Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342
Plugins and Best Practices . . . . . . . . . . . . . . . . . . . . . . . . 343
General Best Practices . . . . . . . . . . . . . . . . . . . . . . . . . . . 343
project/ vs. ~/.sbt/ . . . . . . . . . . . . . . . . . . . . . . . . 343
Local settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343
.sbtrc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344
Generated files . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344
Don’t hard code . . . . . . . . . . . . . . . . . . . . . . . . . . . 344
Don’t “mutate” files . . . . . . . . . . . . . . . . . . . . . . . . . 344
Use absolute paths . . . . . . . . . . . . . . . . . . . . . . . . . . 345
Parser combinators . . . . . . . . . . . . . . . . . . . . . . . . . . 346
Plugins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346
Using an auto plugin . . . . . . . . . . . . . . . . . . . . . . . . . 346
By Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
Plugin dependencies . . . . . . . . . . . . . . . . . . . . . . . . . 348
Creating an auto plugin . . . . . . . . . . . . . . . . . . . . . . . 349
Using a library in a build definition example . . . . . . . . . . . . 354
Best Practices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357
Plugins Best Practices . . . . . . . . . . . . . . . . . . . . . . . . . . . 357
Get your plugins known . . . . . . . . . . . . . . . . . . . . . . . 357
Don’t use default package . . . . . . . . . . . . . . . . . . . . . . 357
16
Follow the naming conventions . . . . . . . . . . . . . . . . . . . 357
Use settings and tasks. Avoid commands. . . . . . . . . . . . . . 358
Use sbt.AutoPlugin . . . . . . . . . . . . . . . . . . . . . . . . . 358
Reuse existing keys . . . . . . . . . . . . . . . . . . . . . . . . . . 358
Avoid namespace clashes . . . . . . . . . . . . . . . . . . . . . . . 359
Provide core feature in a plain old Scala object . . . . . . . . . . 359
Configuration advices . . . . . . . . . . . . . . . . . . . . . . . . 359
Mucking with globalSettings . . . . . . . . . . . . . . . . . . . 362
Setting up Travis CI with sbt . . . . . . . . . . . . . . . . . . . . . . . 363
Set project/build.properties . . . . . . . . . . . . . . . . . . 363
Read the Travis manual . . . . . . . . . . . . . . . . . . . . . . . 363
Basic setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363
Plugin build setup . . . . . . . . . . . . . . . . . . . . . . . . . . 364
Custom JVM options . . . . . . . . . . . . . . . . . . . . . . . . . 365
Caching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366
Build matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367
Notification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368
Dealing with flaky network or tests . . . . . . . . . . . . . . . . . 368
More things . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369
Sample setting . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369
Testing sbt plugins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370
scripted test framework . . . . . . . . . . . . . . . . . . . . . . . 370
step 1: snapshot . . . . . . . . . . . . . . . . . . . . . . . . . . . 370
step 2: scripted-plugin . . . . . . . . . . . . . . . . . . . . . . . . 371
step 3: src/sbt-test . . . . . . . . . . . . . . . . . . . . . . . . . . 371
step 4: write a script . . . . . . . . . . . . . . . . . . . . . . . . . 372
step 5: run the script . . . . . . . . . . . . . . . . . . . . . . . . . 372
step 6: custom assertion . . . . . . . . . . . . . . . . . . . . . . . 373
step 7: testing the test . . . . . . . . . . . . . . . . . . . . . . . . 374
step 8: get inspired . . . . . . . . . . . . . . . . . . . . . . . . . . 374
sbt new and Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . 375
17
Trying new command . . . . . . . . . . . . . . . . . . . . . . . . 375
Giter8 support . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375
How to extend sbt new . . . . . . . . . . . . . . . . . . . . . . . . 376
How to… . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378
Classpaths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378
Include a new type of managed artifact on the classpath, such as
mar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378
Get the classpath used for compilation . . . . . . . . . . . . . . . 378
Get the runtime classpath, including the project’s compiled classes379
Get the test classpath, including the project’s compiled test classes379
Use packaged jars on classpaths instead of class directories . . . . 379
Get all managed jars for a configuration . . . . . . . . . . . . . . 380
Get the files included in a classpath . . . . . . . . . . . . . . . . 380
Get the module and artifact that produced a classpath entry . . 380
Customizing paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
Change the default Scala source directory . . . . . . . . . . . . . 381
Change the default Java source directory . . . . . . . . . . . . . . 381
Change the default resource directory . . . . . . . . . . . . . . . 382
Change the default (unmanaged) library directory . . . . . . . . 382
Disable using the project’s base directory as a source directory . 382
Add an additional source directory . . . . . . . . . . . . . . . . . 382
Add an additional resource directory . . . . . . . . . . . . . . . . 383
Include/exclude files in the source directory . . . . . . . . . . . . 383
Include/exclude files in the resource directory . . . . . . . . . . . 384
Include only certain (unmanaged) libraries . . . . . . . . . . . . . 384
Generating files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384
Generate sources . . . . . . . . . . . . . . . . . . . . . . . . . . . 385
Generate resources . . . . . . . . . . . . . . . . . . . . . . . . . . 386
Inspect the build . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387
Show or search help for a command, task, or setting . . . . . . . 387
List available tasks . . . . . . . . . . . . . . . . . . . . . . . . . . 387
18
List available settings . . . . . . . . . . . . . . . . . . . . . . . . 387
Display the description and type of a setting or task . . . . . . . 388
Display the delegation chain of a setting or task . . . . . . . . . . 389
Show the list of projects and builds . . . . . . . . . . . . . . . . . 389
Show the current session (temporary) settings . . . . . . . . . . . 390
Show basic information about sbt and the current build . . . . . 390
Show the value of a setting . . . . . . . . . . . . . . . . . . . . . 390
Show the result of executing a task . . . . . . . . . . . . . . . . . 390
Show the classpath used for compilation or testing . . . . . . . . 391
Show the main classes detected in a project . . . . . . . . . . . . 391
Show the test classes detected in a project . . . . . . . . . . . . . 391
Interactive mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 392
Use tab completion . . . . . . . . . . . . . . . . . . . . . . . . . . 392
Show more tab completion suggestions . . . . . . . . . . . . . . . 392
Modify the default JLine keybindings . . . . . . . . . . . . . . . . 393
Configure the prompt string . . . . . . . . . . . . . . . . . . . . . 393
Use history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393
Change the location of the interactive history file . . . . . . . . . 393
Use the same history for all projects . . . . . . . . . . . . . . . . 394
Disable interactive history . . . . . . . . . . . . . . . . . . . . . . 394
Run commands before entering interactive mode . . . . . . . . . 394
Configure and use logging . . . . . . . . . . . . . . . . . . . . . . . . . 395
View the logging output of the previously executed command . . 395
View the previous logging output of a specific task . . . . . . . . 396
Show warnings from the previous compilation . . . . . . . . . . . 397
Change the logging level globally . . . . . . . . . . . . . . . . . . 397
Change the logging level for a specific task, configuration, or project398
Configure printing of stack traces . . . . . . . . . . . . . . . . . . 398
Print the output of tests immediately instead of buffering . . . . 399
Add a custom logger . . . . . . . . . . . . . . . . . . . . . . . . . 399
Log messages in a task . . . . . . . . . . . . . . . . . . . . . . . . 399
19
Log messages in a setting . . . . . . . . . . . . . . . . . . . . . . 399
Project metadata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400
Set the project name . . . . . . . . . . . . . . . . . . . . . . . . . 400
Set the project version . . . . . . . . . . . . . . . . . . . . . . . . 400
Set the project organization . . . . . . . . . . . . . . . . . . . . . 400
Set the project’s homepage and other metadata . . . . . . . . . . 401
Configure packaging . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401
Use the packaged jar on classpaths instead of class directory . . . 401
Add manifest attributes . . . . . . . . . . . . . . . . . . . . . . . 401
Change the file name of a package . . . . . . . . . . . . . . . . . 402
Modify the contents of the package . . . . . . . . . . . . . . . . . 402
Running commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402
Pass arguments to a command or task in batch mode . . . . . . . 402
Provide multiple commands to run consecutively . . . . . . . . . 403
Read commands from a file . . . . . . . . . . . . . . . . . . . . . 403
Define an alias for a command or task . . . . . . . . . . . . . . . 403
Quickly evaluate a Scala expression . . . . . . . . . . . . . . . . . 403
Configure and use Scala . . . . . . . . . . . . . . . . . . . . . . . . . . 404
Set the Scala version used for building the project . . . . . . . . 404
Disable the automatic dependency on the Scala library . . . . . . 404
Temporarily switch to a different Scala version . . . . . . . . . . 404
Use a local Scala installation for building a project . . . . . . . . 404
Build a project against multiple Scala versions . . . . . . . . . . 404
Enter the Scala REPL with a project’s dependencies on the class-
path, but not the compiled project classes . . . . . . . . . 405
Enter the Scala REPL with a project’s dependencies and com-
piled code on the classpath . . . . . . . . . . . . . . . . . 405
Enter the Scala REPL with plugins and the build definition on
the classpath . . . . . . . . . . . . . . . . . . . . . . . . . 405
Define the initial commands evaluated when entering the Scala
REPL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405
Define the commands evaluated when exiting the Scala REPL . . 406
20
Use the Scala REPL from project code . . . . . . . . . . . . . . . 406
Generate API documentation . . . . . . . . . . . . . . . . . . . . . . . 407
Select javadoc or scaladoc . . . . . . . . . . . . . . . . . . . . . . 407
Set the options used for generating scaladoc independently of
compilation . . . . . . . . . . . . . . . . . . . . . . . . . . 407
Add options for scaladoc to the compilation options . . . . . . . 407
Set the options used for generating javadoc independently of com-
pilation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407
Add options for javadoc to the compilation options . . . . . . . . 407
Enable automatic linking to the external Scaladoc of managed
dependencies . . . . . . . . . . . . . . . . . . . . . . . . . 408
Enable manual linking to the external Scaladoc of managed de-
pendencies . . . . . . . . . . . . . . . . . . . . . . . . . . 408
Define the location of API documentation for a library . . . . . . 408
Triggered execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408
Run a command when sources change . . . . . . . . . . . . . . . 408
Run multiple commands when sources change . . . . . . . . . . . 409
Configure the sources that are checked for changes . . . . . . . . 409
Set the time interval between checks for changes to sources . . . 409
Define Custom Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410
Define a Task that runs tests in specific sub-projects . . . . . . . 410
Sequencing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410
Defining a sequential task with Def.sequential . . . . . . . . . . . 411
Defining a dynamic task with Def.taskDyn . . . . . . . . . . . . . 412
Doing something after an input task . . . . . . . . . . . . . . . . 413
Defining a dynamic input task with Def.inputTaskDyn . . . . . . 414
How to sequence using commands . . . . . . . . . . . . . . . . . 416
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 416
.sbt build examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 416
.sbt build with .scala files example . . . . . . . . . . . . . . . . . . . . 421
project/Resolvers.scala . . . . . . . . . . . . . . . . . . . . . . . . 421
project/Dependencies.scala . . . . . . . . . . . . . . . . . . . . . 421
21
project/ShellPrompPlugin.scala . . . . . . . . . . . . . . . . . . . 422
build.sbt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 422
Advanced configurations example . . . . . . . . . . . . . . . . . . . . . 424
Advanced command example . . . . . . . . . . . . . . . . . . . . . . . 426
Frequently Asked Questions . . . . . . . . . . . . . . . . . . . . . . . . 427
Project Information . . . . . . . . . . . . . . . . . . . . . . . . . 427
Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427
Build definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . 428
Extending sbt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429
Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 434
Dependency Management . . . . . . . . . . . . . . . . . . . . . . 435
Miscellaneous . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435
0.7 to 0.10+ Migration . . . . . . . . . . . . . . . . . . . . . . . . 436
My tests all run really fast but some are broken that weren’t in
0.7! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 437
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 438
Values and Types . . . . . . . . . . . . . . . . . . . . . . . . . . . 439
Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440
Developer’s Guide (Work in progress) . . . . . . . . . . . . . . . . . . 442
Towards sbt 1.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 442
Modularization . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443
Module summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 443
sbt Coding Guideline . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445
General goal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445
Modular design . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445
Binary resiliency . . . . . . . . . . . . . . . . . . . . . . . . . . . 446
Other public API matters . . . . . . . . . . . . . . . . . . . . . . 447
Style matters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 448
sbt-datatype . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449
Using the plugin . . . . . . . . . . . . . . . . . . . . . . . . . . . 449
Datatype schema . . . . . . . . . . . . . . . . . . . . . . . . . . . 450
22
Using datatype to retain binary compatibility . . . . . . . . . . . 454
JSON codec generation . . . . . . . . . . . . . . . . . . . . . . . 456
Existing parameters for protocols, records, etc. . . . . . . . . . . 457
Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 459
Syntax summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 459
Compiler Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 460
Fetching the most specific sources . . . . . . . . . . . . . . . . . . . . . 460
sbt Launcher . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 461
Getting Started with the sbt launcher . . . . . . . . . . . . . . . . . . 461
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 462
Sbt Launcher Architecture . . . . . . . . . . . . . . . . . . . . . . . . . 466
Module Resolution . . . . . . . . . . . . . . . . . . . . . . . . . . 466
Classloader Caching and Isolation . . . . . . . . . . . . . . . . . . 467
Caching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 467
Locking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 468
Service Discovery and Isolation . . . . . . . . . . . . . . . . . . . 468
sbt Launcher Configuration . . . . . . . . . . . . . . . . . . . . . . . . 468
Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469
Variable Substitution . . . . . . . . . . . . . . . . . . . . . . . . . 472
Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472
Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474
Core Principles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474
Introduction to build state . . . . . . . . . . . . . . . . . . . . . . 474
Settings Architecture . . . . . . . . . . . . . . . . . . . . . . . . . 475
Task Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . 477
Settings Core . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477
Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477
sbt Settings Discussion . . . . . . . . . . . . . . . . . . . . . . . . 480
Setting Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . 481
Controlling Initialization . . . . . . . . . . . . . . . . . . . . . . . 484
Build Loaders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485
23
Custom Resolver . . . . . . . . . . . . . . . . . . . . . . . . . . . 485
Custom Builder . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487
Custom Transformer . . . . . . . . . . . . . . . . . . . . . . . . . 489
The BuildDependencies type . . . . . . . . . . . . . . . . . . . . 490
Creating Command Line Applications Using sbt . . . . . . . . . . . . 491
Hello World Example . . . . . . . . . . . . . . . . . . . . . . . . . 491
Nightly Builds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493
Preface
sbt is a build tool for Scala, Java, and more. It requires Java 1.6 or later.
Install
Getting Started
To get started, please read the Getting Started Guide. You will save yourself a
lot of time if you have the right understanding of the big picture up-front. All
documentation may be found via the table of contents included at the end of
every page.
Use Stack Overflow for questions. Use the sbt-dev mailing list for discussing sbt
development. Use [@scala_sbt](https://twitter.com/scala_sbt) for questions
and discussions.
Features of sbt
24
• Starts the Scala REPL with project classes and dependencies on the class-
path
• Modularization supported with sub-projects
• External project support (list a git repository as a dependency!)
• Parallel task execution, including parallel test execution
• Library management support: inline declarations, external Ivy or Maven
configuration files, or manual management
Also
Installing sbt
• Install sbt.
• Setup a simple hello world project
25
– Create a project directory with source files in it.
– Create your build definition.
• Move on to running to learn how to run sbt.
• Then move on to .sbt build definition to learn more about build definitions.
Ultimately, the installation of sbt boils down to a launcher JAR and a shell
script, but depending on your platform, we provide several ways to make the
process less tedious. Head over to the installation steps for Mac, Windows, or
Linux.
If you have any trouble running sbt, see Setup Notes on terminal encodings,
HTTP proxies, and JVM options.
Homebrew
Macports
26
Windows installer
27
Figure 1: Ubuntu Software & Updates Screenshot
28
sbt binaries are published to Bintray, and conveniently Bintray provides an
RPM repository. You just have to add the repository to the places your package
manager will check.
Gentoo
The official tree contains ebuilds for sbt. To install the latest available version
do:
emerge dev-java/sbt
Hello, World
If you’re using sbt 0.13.13 or later, you can use sbt new command to quickly
setup a simple Hello world build. Type the following command to the terminal.
Now from inside the hello directory, start sbt and type run at the sbt shell.
On Linux or OS X the commands might look like this:
29
$ cd hello
$ sbt
...
> run
...
[info] Compiling 1 Scala source to /xxx/hello/target/scala-2.12/classes...
[info] Running example.Hello
hello
To leave sbt shell, type exit or use Ctrl+D (Unix) or Ctrl+Z (Windows).
> exit
Build definition
The build definition goes in a file called build.sbt, located in the project’s base
directory. You can take a look at the file, but don’t worry if the details of this
build file aren’t clear yet. In .sbt build definition you’ll learn more about how
to write a build.sbt file.
Directory structure
This page assumes you’ve installed sbt and seen the Hello, World example.
Base directory
In sbt’s terminology, the “base directory” is the directory containing the project.
So if you created a project hello containing hello/build.sbt as in the Hello,
World example, hello is your base directory.
Source code
sbt uses the same directory structure as Maven for source files by default (all
paths are relative to the base directory):
30
src/
main/
resources/
<files to include in main jar here>
scala/
<main Scala sources>
java/
<main Java sources>
test/
resources
<files to include in test jar here>
scala/
<test Scala sources>
java/
<test Java sources>
The build definition is described in build.sbt (actually any files named *.sbt)
in the project’s base directory.
build.sbt
In addition to build.sbt, project directory can contain .scala files that de-
fines helper objects and one-off plugins. See organizing the build for more.
build.sbt
project/
Dependencies.scala
You may see .sbt files inside project/ but they are not equivalent to .sbt files
in the project’s base directory. Explaining this will come later, since you’ll need
some background information first.
31
Build products
Generated files (compiled classes, packaged jars, managed files, caches, and
documentation) will be written to the target directory by default.
Your .gitignore (or equivalent for other version control systems) should con-
tain:
target/
Note that this deliberately has a trailing / (to match only directories) and it
deliberately has no leading / (to match project/target/ in addition to plain
target/).
Running
This page describes how to use sbt once you have set up your project. It assumes
you’ve installed sbt and created a Hello, World or other project.
sbt shell
$ sbt
Running sbt with no command line arguments starts sbt shell. sbt shell has a
command prompt (with tab completion and history!).
For example, you could type compile at the sbt shell:
> compile
32
Batch mode
You can also run sbt in batch mode, specifying a space-separated list of sbt
commands as arguments. For sbt commands that take arguments, pass the
command and arguments as one argument to sbt by enclosing them in quotes.
For example,
In this example, testOnly has arguments, TestA and TestB. The commands
will be run in sequence (clean, compile, then testOnly).
Note: Running in batch mode requires JVM spinup and JIT each time, so your
build will run much slower. For day-to-day coding, we recommend using
the sbt shell or Continuous build and test feature described below.
To speed up your edit-compile-test cycle, you can ask sbt to automatically re-
compile or run tests whenever you save a source file.
Make a command run when one or more source files change by prefixing the
command with ~. For example, in sbt shell try:
> ~testQuick
Common commands
Here are some of the most common sbt commands. For a more complete list,
see Command Line Reference.
clean
Deletes all generated files (in the target directory).
compile
Compiles the main sources (in src/main/scala and src/main/java directories).
test
Compiles and runs all tests.
33
console
Starts the Scala interpreter with a classpath including the compiled sources
and all dependencies. To return to sbt, type :quit, Ctrl+D (Unix), or Ctrl+Z
(Windows).
run <argument>*
Runs the main class for the project in the same virtual machine as sbt.
package
Creates a jar file containing the files in src/main/resources and the classes com-
piled from src/main/scala and src/main/java.
help <command>
Displays detailed help for the specified command. If no command is provided,
displays brief descriptions of all commands.
reload
Reloads the build definition (build.sbt, project/.scala, project/.sbt files). Needed
if you change the build definition.
Tab completion
sbt shell has tab completion, including at an empty prompt. A special sbt
convention is that pressing tab once may show only a subset of most likely
completions, while pressing it more times shows more verbose choices.
History Commands
sbt shell remembers history, even if you exit sbt and restart it. The simplest
way to access history is with the up arrow key. The following commands are
also supported:
!
Show history command help.
!!
Execute the previous command again.
!:
Show all previous commands.
!:n
Show the last n commands.
!n
34
Execute the command with index n, as shown by the !: command.
!-n
Execute the nth command before this one.
!string
Execute the most recent command starting with ‘string.’
!?string
Execute the most recent command containing ‘string.’
Build definition
This page describes sbt build definitions, including some “theory” and the syntax
of build.sbt. It assumes you have installed a recent version of sbt, such as sbt
0.13.13, know how to use sbt, and have read the previous pages in the Getting
Started Guide.
This page discusses the build.sbt build definition.
As part of your build definition you will specify the version of sbt that your
build uses. This allows people with different versions of the sbt launcher to
build the same projects with consistent results. To do this, create a file named
project/build.properties that specifies the sbt version as follows:
sbt.version=0.13.16
If the required version is not available locally, the sbt launcher will download
it for you. If this file is not present, the sbt launcher will choose an arbitrary
version, which is discouraged because it makes your build non-portable.
35
lazy val root = (project in file("."))
.settings(
name := "Hello",
scalaVersion := "2.12.2"
)
36
1. Left-hand side is a key.
2. Operator, which in this case is :=
3. Right-hand side is called the body, or the setting body.
On the left-hand side, name, version, and scalaVersion are keys. A key is
an instance of SettingKey[T], TaskKey[T], or InputKey[T] where T is the
expected value type. The kinds of key are explained below.
Because key name is typed to SettingKey[String], the := operator on name
is also typed specifically to String. If you use the wrong value type, the build
definition will not compile:
build.sbt may also be interspersed with vals, lazy vals, and defs. Top-level
objects and classes are not allowed in build.sbt. Those should go in the
project/ directory as Scala source files.
Keys
Built-in Keys The built-in keys are just fields in an object called Keys. A
build.sbt implicitly has an import sbt.Keys._, so sbt.Keys.name can be
referred to as name.
Custom Keys Custom keys may be defined with their respective creation
methods: settingKey, taskKey, and inputKey. Each method expects the type
of the value associated with the key as well as a description. The name of the
key is taken from the val the key is assigned to. For example, to define a key
for a new task called hello,
37
Here we have used the fact that an .sbt file can contain vals and defs in
addition to settings. All such definitions are evaluated before settings regardless
of where they are defined in the file.
Note: Typically, lazy vals are used instead of vals to avoid initial-
ization order problems.
Using :=, you can assign a value to a setting and a computation to a task. For
a setting, the value will be computed once at project load time. For a task, the
computation will be re-run each time the task is executed.
For example, to implement the hello task from the previous section:
38
Types for tasks and settings From a type-system perspective, the Setting
created from a task key is slightly different from the one created from a setting
key. taskKey := 42 results in a Setting[Task[T]] while settingKey := 42
results in a Setting[T]. For most purposes this makes no difference; the task
key still creates a value of type T when the task executes.
The T vs. Task[T] type difference has this implication: a setting can’t depend
on a task, because a setting is evaluated only once on project load and is not
re-run. More on this in task graph.
In sbt shell, you can type the name of any task to execute that task. This is
why typing compile runs the compile task. compile is a task key.
If you type the name of a setting key rather than a task key, the value of
the setting key will be displayed. Typing a task key name executes the task
but doesn’t display the resulting value; to see a task’s result, use show <task
name> rather than plain <task name>. The convention for keys names is to
use camelCase so that the command line name and the Scala identifiers are the
same.
To learn more about any key, type inspect <keyname> at the sbt interactive
prompt. Some of the information inspect displays won’t make sense yet, but
at the top it shows you the setting’s value type and a brief description of the
setting.
Imports in build.sbt
You can place import statements at the top of build.sbt; they need not be
separated by blank lines.
There are some implied default imports, as follows:
import sbt._
import Process._
import Keys._
(In addition, if you have auto plugins, the names marked under autoImport will
be imported.)
To depend on third-party libraries, there are two options. The first is to drop
jars in lib/ (unmanaged dependencies) and the other is to add managed depen-
dencies, which will look like this in build.sbt:
39
val derby = "org.apache.derby" % "derby" % "10.4.1.3"
This is how you add a managed dependency on the Apache Derby library, version
10.4.1.3.
The libraryDependencies key involves two complexities: += rather than :=,
and the % method. += appends to the key’s old value rather than replacing it,
this is explained in Task Graph. The % method is used to construct an Ivy
module ID from strings, explained in Library dependencies.
We’ll skip over the details of library dependencies until later in the Getting
Started Guide. There’s a whole page covering it later on.
Task graph
Continuing from build definition, this page explains build.sbt definition in
more detail.
Rather than thinking of settings as key-value pairs, a better analogy would be
to think of it as a directed acyclic graph (DAG) of tasks where the edges denote
happens-before. Let’s call this the task graph.
Terminology
40
Declaring dependency to other tasks
scalacOptions := {
val ur = update.value // update task happens-before scalacOptions
val x = clean.value // clean task happens-before scalacOptions
// ---- scalacOptions begins here ----
ur.allConfigurations.take(3)
}
41
log.info("456")
ur.allConfigurations.take(3)
}
)
> scalacOptions
[info] Updating {file:/xxx/}root...
[info] Resolving jline#jline;2.14.1 ...
[info] Done updating.
[info] 123
[info] 456
[success] Total time: 0 s, completed Jan 2, 2017 10:38:24 PM
> run
[info] Updating {file:/xxx/}root...
[info] Resolving jline#jline;2.14.1 ...
[info] Done updating.
[info] Compiling 1 Scala source to /Users/eugene/work/quick-test/task-graph/target/scala-2.12/
[info] Running example.Hello
hello
[success] Total time: 0 s, completed Jan 2, 2017 10:45:19 PM
42
> scalacOptions
[info] Updating {file:/xxx/}root...
[info] Resolving jline#jline;2.14.1 ...
[info] Done updating.
[success] Total time: 0 s, completed Jan 2, 2017 10:45:23 PM
scalacOptions := {
val x = clean.value
update.value.allConfigurations.take(3)
}
Note whether .value calls are inlined, or placed anywhere in the task body,
they are still evaluated before entering the task body.
43
[info] *:update
....
This is how sbt knows which tasks depend on which other tasks.
For example, if you inspect tree compile you’ll see it depends on an-
other key incCompileSetup, which it in turn depends on other keys like
dependencyClasspath. Keep following the dependency chains and magic
happens.
When you type compile sbt automatically performs an update, for example. It
Just Works because the values required as inputs to the compile computation
require sbt to do the update computation first.
In this way, all build dependencies in sbt are automatic rather than explicitly
declared. If you use a key’s value in another computation, then the computation
depends on that key.
44
lazy val root = (project in file("."))
.settings(
name := "Hello",
organization := "com.example",
scalaVersion := "2.12.2",
version := "0.1.0-SNAPSHOT",
scalacOptions := List("-encoding", "utf8", "-Xfatal-warnings", "-deprecation", "-uncheck
scalacOptions := {
val old = scalacOptions.value
scalaBinaryVersion.value match {
case "2.12" => old
case _ => old filterNot (Set("-Xfatal-warnings", "-deprecation").apply)
}
}
)
45
// The scalacOptions task may be defined in terms of the checksums setting
scalacOptions := checksums.value
There is no way to go the other direction. That is, a setting key can’t depend
on a task key. That’s because a setting key is only computed once on project
load, so the task would not be re-run every time, and tasks expect to re-run
every time.
// Bad example: The checksums setting cannot be defined in terms of the scalacOptions task!
checksums := scalacOptions.value
scalaSource in Compile := {
val old = (scalaSource in Compile).value
scalaBinaryVersion.value match {
case "2.11" => baseDirectory.value / "src-2.11" / "main" / "scala"
case _ => old
}
}
Intro to Make The basic Makefile syntax looks like the following:
target: dependencies
[tab] system command1
[tab] system command2
46
Given a target (the default target is named all),
1. Make checks if the target’s dependencies have been built, and builds any
of the dependencies that hasn’t been built yet.
2. Make runs the system commands in order.
CC=g++
CFLAGS=-Wall
all: hello
%.o: %.cpp
$(CC) $(CFLAGS) -c $< -o $@
Running make, it will by default pick the target named all. The target lists
hello as its dependency, which hasn’t been built yet, so Make will build hello.
Next, Make checks if the hello target’s dependencies have been built yet. hello
lists two targets: main.o and hello.o. Once those targets are created using
the last pattern matching rule, only then the system command is executed to
link main.o and hello.o to hello.
If you’re just running make, you can focus on what you want as the target, and
the exact timing and commands necessary to build the intermediate products
are figured out by Make. We can think of this as dependency-oriented pro-
gramming, or flow-based programming. Make is actually considered a hybrid
system because while the DSL describes the task dependencies, the actions are
delegated to system commands.
Rake This hybridity is continued for Make successors such as Ant, Rake, and
sbt. Take a look at the basic syntax for Rakefile:
The breakthrough made with Rake was that it used a programming language
to describe the actions instead of the system commands.
47
Benefits of hybrid flow-based programming There are several motivation
to organizing the build this way.
First is de-duplication. With flow-based programming, a task is executed only
once even when it is depended by multiple tasks. For example, even when multi-
ple tasks along the task graph depend on compile in Compile, the compilation
will be executed exactly once.
Second is parallel processing. Using the task graph, the task engine can schedule
mutually non-dependent tasks in parallel.
Third is the separation of concern and the flexibility. The task graph lets the
build user wire the tasks together in different ways, while sbt and plugins can
provide various features such as compilation and library dependency manage-
ment as functions that can be reused.
Summary
The core data structure of the build definition is a DAG of tasks, where the
edges denote happens-before relationships. build.sbt is a DSL designed to
express dependency-oriented programming, or flow-based programming, similar
to Makefile and Rakefile.
The key motivation for the flow-based programming is de-duplication, parallel
processing, and customizability.
Scopes
This page describes scopes. It assumes you’ve read and understood the previous
pages, build definition and task graph.
Previously we pretended that a key like name corresponded to one entry in sbt’s
map of key-value pairs. This was a simplification.
In truth, each key can have an associated value in more than one context, called
a scope.
Some concrete examples:
• if you have multiple projects (also called subprojects) in your build defini-
tion, a key can have a different value in each project.
• the compile key may have a different value for your main sources and
your test sources, if you want to compile them differently.
48
• the packageOptions key (which contains options for creating jar pack-
ages) may have different values when packaging class files (packageBin)
or packaging source code (packageSrc).
There is no single value for a given key name, because the value may differ
according to scope.
However, there is a single value for a given scoped key.
If you think about sbt processing a list of settings to generate a key-value map
describing the project, as discussed earlier, the keys in that key-value map
are scoped keys. Each setting defined in the build definition (for example in
build.sbt) applies to a scoped key as well.
Often the scope is implied or has a default, but if the defaults are wrong, you’ll
need to mention the desired scope in build.sbt.
Scope axes
If you’re not familiar with the notion of axis, we can think of the RGB color
cube as an example:
In the RGB color model, all colors are represented by a point in the cube
whose axes correspond to red, green, and blue components encoded by a number.
Similarly, a full scope in sbt is formed by a tuple of a subproject, a configuration,
and a task value:
49
Figure 2: color cube
By default, all the keys associated with compiling, packaging, and running are
50
scoped to a configuration and therefore may work differently in each configu-
ration. The most obvious examples are the task keys compile, package, and
run; but all the keys which affect those keys (such as sourceDirectories or
scalacOptions or fullClasspath) are also scoped to the configuration.
Another thing to note about a configuration is that it can extend other configu-
rations. The following figure shows the extension relationship among the most
common configurations.
Scoping by Task axis Settings can affect how a task works. For example,
the packageSrc task is affected by the packageOptions setting.
To support this, a task key (such as packageSrc) can be a scope for another
key (such as packageOptions).
The various tasks that build a package (packageSrc, packageBin, packageDoc)
can share keys related to packaging, such as artifactName and packageOptions.
Those keys can have distinct values for each packaging task.
Global scope component Each scope axis can be filled in with an instance
of the axis type (for example the task axis can be filled in with a task), or the
axis can be filled in with the special value Global, which is also written as *.
So we can think of Global as None.
* is a universal fallback for all scope axes, but its direct use should be reserved
to sbt and plugin authors in most cases.
To the make the matter confusing, someKey in Global appearing in build
definition implicitly converts to someKey in (Global, Global, Global).
51
Referring to scopes in a build definition
If you create a setting in build.sbt with a bare key, it will be scoped to (current
subproject, configuration Global, task Global):
Run sbt and inspect name to see that it’s provided by {file:/home/hp/checkout/hello/}default-aea33a/*:
that is, the project is {file:/home/hp/checkout/hello/}default-aea33a,
the configuration is * (means Global), and the task is not shown (which also
means Global).
A bare key on the right hand side is also scoped to (current subproject, config-
uration Global, task Global):
organization := name.value
Keys have an overloaded method called .in that is used to set the scope. The
argument to .in(...) can be an instance of any of the scope axes. So for
example, though there’s no real reason to do this, you could set the name scoped
to the Compile configuration:
or you could set the name scoped to the packageBin task (pointless! just an
example):
or you could set the name with multiple scope axes, for example in the
packageBin task in the Compile configuration:
52
(concurrentRestrictions in Global implicitly converts to concurrentRestrictions
in (Global, Global, Global), setting all axes to Global scope component;
the task and configuration are already Global by default, so here the effect is to
make the project Global, that is, define */*:concurrentRestrictions rather
than {file:/home/hp/checkout/hello/}default-aea33a/*:concurrentRestrictions)
On the command line and in the sbt shell, sbt displays (and parses) scoped keys
like this:
{<build-uri>}<project-id>/config:intask::key
• fullClasspath specifies just a key, so the default scopes are used: current
project, a key-dependent configuration, and global task scope.
• test:fullClasspath specifies the configuration, so this is fullClasspath
in the test configuration, with defaults for the other two scope axes.
• *:fullClasspath specifies Global for the configuration, rather than the
default configuration.
• doc::fullClasspath specifies the fullClasspath key scoped to the doc
task, with the defaults for the project and configuration axes.
• {file:/home/hp/checkout/hello/}default-aea33a/test:fullClasspath
specifies a project, {file:/home/hp/checkout/hello/}default-aea33a,
where the project is identified with the build {file:/home/hp/checkout/hello/}
and then a project id inside that build default-aea33a. Also specifies
configuration test, but leaves the default task axis.
53
• {file:/home/hp/checkout/hello/}/test:fullClasspath sets the
project axis to “entire build” where the build is {file:/home/hp/checkout/hello/}.
• {.}/test:fullClasspath sets the project axis to “entire build” where
the build is {.}. {.} can be written ThisBuild in Scala code.
• {file:/home/hp/checkout/hello/}/compile:doc::fullClasspath
sets all three scope axes.
Inspecting scopes
In sbt shell, you can use the inspect command to understand keys and their
scopes. Try inspect test:fullClasspath:
$ sbt
> inspect test:fullClasspath
[info] Task: scala.collection.Seq[sbt.Attributed[java.io.File]]
[info] Description:
[info] The exported classpath, consisting of build products and unmanaged and managed, internal
[info] Provided by:
[info] {file:/home/hp/checkout/hello/}default-aea33a/test:fullClasspath
[info] Dependencies:
[info] test:exportedProducts
[info] test:dependencyClasspath
[info] Reverse dependencies:
[info] test:runMain
[info] test:run
[info] test:testLoader
[info] test:console
[info] Delegates:
[info] test:fullClasspath
[info] runtime:fullClasspath
[info] compile:fullClasspath
[info] *:fullClasspath
[info] {.}/test:fullClasspath
[info] {.}/runtime:fullClasspath
[info] {.}/compile:fullClasspath
[info] {.}/*:fullClasspath
[info] */test:fullClasspath
[info] */runtime:fullClasspath
[info] */compile:fullClasspath
[info] */*:fullClasspath
[info] Related:
[info] compile:fullClasspath
[info] compile:fullClasspath(for doc)
[info] test:fullClasspath(for doc)
[info] runtime:fullClasspath
54
On the first line, you can see this is a task (as opposed to a setting, as ex-
plained in .sbt build definition). The value resulting from the task will have
type scala.collection.Seq[sbt.Attributed[java.io.File]].
“Provided by” points you to the scoped key that defines the value, in this case
{file:/home/hp/checkout/hello/}default-aea33a/test:fullClasspath
(which is the fullClasspath key scoped to the test configuration and the
{file:/home/hp/checkout/hello/}default-aea33a project).
“Dependencies” was discussed in detail in the previous page.
We’ll discuss “Delegates” later.
Try inspect fullClasspath (as opposed to the above example, inspect
test:fullClasspath) to get a sense of the difference. Because the configuration
is omitted, it is autodetected as compile. inspect compile:fullClasspath
should therefore look the same as inspect fullClasspath.
Try inspect *:fullClasspath for another contrast. fullClasspath is not
defined in the Global scope by default.
Again, for more details, see Interacting with the Configuration System.
You need to specify the scope if the key in question is normally scoped. For
example, the compile task, by default, is scoped to Compile and Test configu-
rations, and does not exist outside of those scopes.
To change the value associated with the compile key, you need to write compile
in Compile or compile in Test. Using plain compile would define a new
compile task scoped to the current project, rather than overriding the standard
compile tasks which are scoped to a configuration.
If you get an error like “Reference to undefined setting”, often you’ve failed to
specify a scope, or you’ve specified the wrong scope. The key you’re using may
be defined in some other scope. sbt will try to suggest what you meant as part
of the error message; look for “Did you mean compile:compile?”
One way to think of it is that a name is only part of a key. In reality, all keys
consist of both a name, and a scope (where the scope has three axes). The
entire expression packageOptions in (Compile, packageBin) is a key name,
in other words. Simply packageOptions is also a key name, but a different one
(for keys with no in, a scope is implicitly assumed: current project, global config,
global task).
Build-level settings
55
If a key that is scoped to a particular subproject is not found, sbt will look for
it in ThisBuild as a fallback. Using the mechanism, we can define a build-level
default setting for frequently used keys such as version, scalaVersion, and
organization.
For convenience, there is inThisBuild(...) function that will scope both
the key and the body of the setting expression to ThisBuild. Putting setting
expressions in there would be equivalent to appending in ThisBuild where
possible.
Due to the nature of scope delegation that we will cover later, we do not recom-
mend using build-level settings beyond simple value assignments.
Scope delegation
A scoped key may be undefined, if it has no value associated with it in its scope.
For each scope axis, sbt has a fallback search path made up of other scope values.
Typically, if a key has no associated value in a more-specific scope, sbt will try
to get a value from a more general scope, such as the ThisBuild scope.
56
This feature allows you to set a value once in a more general scope, allowing mul-
tiple more-specific scopes to inherit the value. We will disscuss scope delegation
in detail later.
Appending values
Assignment with := is the simplest transformation, but keys have other methods
as well. If the T in SettingKey[T] is a sequence, i.e. the key’s value type is a
sequence, you can append to the sequence rather than replacing it.
Or, using the file() function from the sbt package for convenience:
When settings are undefined Whenever a setting uses :=, +=, or ++= to
create a dependency on itself or another key’s value, the value it depends on
must exist. If it does not, sbt will complain. It might say “Reference to undefined
setting”, for example. When this happens, be sure you’re using the key in the
scope that defines it.
It’s possible to create cycles, which is an error; sbt will tell you if you do this.
57
Tasks based on other keys’ values You can compute values of some tasks
or settings to define or append a value for another task. It’s done by using
Def.task and taskValue as an argument to :=, +=, or ++=.
As a first example, consider appending a source generator using the project base
directory and compilation classpath.
Other keys can be used when appending to an existing setting or task, just like
they can for assigning with :=.
For example, say you have a coverage report named after the project, and you
want to add it to the files removed by clean:
This page describes scope delegation. It assumes you’ve read and understood
the previous pages, build definition and scopes.
Now that we’ve covered all the details of scoping, we can explain the .value
lookup in detail. It’s ok to skip this section if this is your first time reading this
page.
Because the term Global is used for both a scope component *, and as shorthand
for the scope (Global, Global, Global), in this page we will use the symbol
* when we mean it as the scope component.
To summarize what we’ve learned so far:
58
• A key can be further scoped using .in(...) method.
Inside of foo’s setting body a dependency on the scoped key (bar in Test) is
declared. However, despite bar in Test being undefined in projX, sbt is still
able to resolve (bar in Test) to another scoped key, resulting in foo initialized
as 2.
sbt has a well-defined fallback search path called scope delegation. This feature
allows you to set a value once in a more general scope, allowing multiple more-
specific scopes to inherit the value.
• Rule 1: Scope axes have the following precedence: the subproject axis, the
configuration axis, and then the task axis.
• Rule 2: Given a scope, delegate scopes are searched by substituting the
task axis in the following order: the given task scoping, and then *
(Global), which is non-task scoped version of the scope.
• Rule 3: Given a scope, delegate scopes are searched by substituting the
configuration axis in the following order: the given configuration, its par-
ents, their parents and so on, and then * (Global, same as unscoped
configuration axis).
• Rule 4: Given a scope, delegate scopes are searched by substituting the
subproject axis in the following order: the given subproject, ThisBuild,
and then * (Global).
• Rule 5: A delegated scoped key and its dependent settings/tasks are eval-
uated without carrying the original context.
59
Rule 1: Scope axis precedence
• Rule 1: Scope axes have the following precedence: the subproject axis, the
configuration axis, and then the task axis.
In other words, given two scopes candidates, if one has more specific value on
the subproject axis, it will always win regardless of the configuration or the
task scoping. Similarly, if subprojects are the same, one with more specific
configuration value will always win regardless of the task scoping. We will see
more rules to define more specific.
Here we have a concrete rule for how sbt will generate delegate scopes given a
key. Remember, we are trying to show the search path given an arbitrary (xxx
in yyy).value.
Exercise A: Given the following build definition:
1. "foo-2.11.11"
2. "foo-2.12.2"
3. something else?
60
Rule 3: The configuration axis search path
If we write out the full scope again, it’s (projX, Test, *). Also recall that
Test extends Runtime, and Runtime extends Compile.
(bar in Test) is undefined, but due to Rule 3 sbt will look for bar scoped in
(projX, Test, *), (projX, Runtime, *), and then (projX, Compile, *).
The last one is found, which is bar in Compile.
61
1. "abc-com.example"
2. "abc-org.tempuri"
3. something else?
Scope axis precedence, again Exercise C: Given the following build defi-
nition:
1. "foo-2.12.2"
2. "foo-2.11.11"
3. something else?
62
What would you see if you ran projD/test?
1. List()
2. List(-Ywarn-unused-import)
3. something else?
You might want to look up quickly what is going on. This is where inspect
can be used.
63
Note how “Provided by” shows that projD/compile:console::scalacOptions
is provided by projD/compile:scalacOptions. Also under “Delegates”, all of
the possible delegate candidates listed in the order of precedence!
• All the scopes with projD scoping on the subproject axis are listed first,
then ThisBuild ({.}), and *.
• Within a subproject, scopes with Compile scoping on the configuration
axis are listed first, then falls back to *.
• Finally, the task axis scoping lists the given task scoping console:: and
the one without.
• Rule 5: A delegated scoped key and its dependent settings/tasks are eval-
uated without carrying the original context.
64
1. "2.12.2_0.1.0"
2. "2.11.11_0.1.0"
3. something else?
1. "bippy-D4"
2. "bippy-D2-D4"
3. "bippy-D0-D3-D4"
4. something else?
someKey += {
val old = someKey.value
old :+ "x"
}
Retrieving the old value would cause delegation, and due to Rule 5, it will go
to another scoped key. Let’s get rid of += first, and annotate the delegates for
old values:
65
scalacOptions in ThisBuild := {
// scalacOptions in Global <- Rule 4
val old = (scalacOptions in ThisBuild).value
old :+ "-D0"
}
scalacOptions := {
// scalacOptions in ThisBuild <- Rule 4
val old = scalacOptions.value
old :+ "-D1"
}
This becomes:
scalacOptions in ThisBuild := {
Nil :+ "-D0"
}
scalacOptions := {
List("-D0") :+ "-D1"
}
66
.settings(
scalacOptions in compile := List("-D0") :+ "-D2",
scalacOptions in Compile := List("-D0") :+ "-D3",
scalacOptions in (Compile, compile) := List("-D0", "-D3") :+ "-D4",
test := {
println("bippy" + (scalacOptions in (Compile, compile)).value.mkString)
}
)
Library dependencies
This page assumes you’ve already read the earlier Getting Started pages, in
particular build definition, scopes, and task graph.
Library dependencies can be added in two ways:
Unmanaged dependencies
67
There’s also an unmanagedJars task which lists the jars from the unmanagedBase
directory. If you wanted to use multiple directories or do something else complex,
you might need to replace the whole unmanagedJars task with one that does
something else, e.g. empty the list for Compile configuration regardless of the
files in lib directory:
Managed Dependencies
The libraryDependencies key Most of the time, you can simply list your
dependencies in the setting libraryDependencies. It’s also possible to write
a Maven POM file or Ivy configuration file to externally configure your depen-
dencies, and have sbt use those external configuration files. You can learn more
about that here.
Declaring a dependency looks like this, where groupId, artifactId, and
revision are strings:
The % methods create ModuleID objects from strings, then you add those
ModuleID to libraryDependencies.
Of course, sbt (via Ivy) has to know where to download the module. If your
module is in one of the default repositories sbt comes with, this will just work.
For example, Apache Derby is in the standard Maven2 repository:
If you type that in build.sbt and then update, sbt should download Derby to
~/.ivy2/cache/org.apache.derby/. (By the way, update is a dependency of
compile so there’s no need to manually type update most of the time.)
Of course, you can also use ++= to add a list of dependencies all at once:
68
libraryDependencies ++= Seq(
groupID % artifactID % revision,
groupID % otherID % otherRevision
)
Assuming the scalaVersion for your build is 2.11.1, the following is identical
(note the double %% after "org.scala-tools"):
The idea is that many dependencies are compiled for multiple Scala versions,
and you’d like to get the one that matches your project to ensure binary com-
patibility.
The complexity in practice is that often a dependency will work with a slightly
different Scala version; but %% is not smart about that. So if the dependency is
available for 2.10.1 but you’re using scalaVersion := "2.10.4", you won’t
be able to use %% even though the 2.10.1 dependency likely works. If %%
stops working, just go see which versions the dependency is really built for, and
hardcode the one you think will work (assuming there is one).
See Cross Building for some more detail on this.
Resolvers Not all packages live on the same server; sbt uses the standard
Maven2 repository by default. If your dependency isn’t on one of the default
repositories, you’ll have to add a resolver to help Ivy find it.
To add an additional repository, use
69
resolvers += name at location
resolvers += Resolver.mavenLocal
Overriding default resolvers resolvers does not contain the default re-
solvers; only additional ones added by your build definition.
sbt combines resolvers with some default repositories to form externalResolvers.
Therefore, to change or remove the default resolvers, you would need to override
externalResolvers instead of resolvers.
You may also use the type-safe version of Test configuration as follows:
70
Now, if you type show compile:dependencyClasspath at the sbt inter-
active prompt, you should not see the derby jar. But if you type show
test:dependencyClasspath, you should see the derby jar in the list.
Typically, test-related dependencies such as ScalaCheck, Specs2, and ScalaTest
would be defined with % "test".
There are more details and tips-and-tricks related to library dependencies on
this page.
Multi-project builds
Multiple subprojects
The name of the val is used as the subproject’s ID, which is used to refer to the
subproject at the sbt shell.
Optionally the base directory may be omitted if it’s the same as the name of
the val.
71
lazy val commonSettings = Seq(
organization := "com.example",
version := "0.1.0-SNAPSHOT",
scalaVersion := "2.12.2"
)
Now we can bump up version in one place, and it will be reflected across
subprojects when you reload the build.
Build-wide settings Another a bit advanced technique for factoring out com-
mon settings across subprojects is to define the settings scoped to ThisBuild.
(See Scopes)
Dependencies
Projects in the build can be completely independent of one another, but usually
they will be related to one another by some kind of dependency. There are two
types of dependencies: aggregate and classpath.
In the above example, the root project aggregates util and core. Start up sbt
with two subprojects as in the example, and try compile. You should see that
all three projects are compiled.
72
In the project doing the aggregating, the root project in this case, you can control
aggregation per-task. For example, to avoid aggregating the update task:
[...]
aggregate in update is the aggregate key scoped to the update task. (See
scopes.)
Note: aggregation will run the aggregated tasks in parallel and with no defined
ordering between them.
Now code in core can use classes from util. This also creates an ordering
between the projects when compiling them; util must be updated and compiled
before core can be compiled.
To depend on multiple projects, use multiple arguments to dependsOn, like
dependsOn(bar, baz).
73
Default root project
If a project is not defined for the root directory in the build, sbt creates a default
one that aggregates all other projects in the build.
Because project hello-foo is defined with base = file("foo"), it will be
contained in the subdirectory foo. Its sources could be directly under foo, like
foo/Foo.scala, or in foo/src/main/scala. The usual sbt directory structure
applies underneath foo with the exception of build definition files.
Any .sbt files in foo, say foo/build.sbt, will be merged with the build defi-
nition for the entire build, but scoped to the hello-foo project.
If your whole project is in hello, try defining a different version (version :=
"0.6") in hello/build.sbt, hello/foo/build.sbt, and hello/bar/build.sbt.
Now show version at the sbt interactive prompt. You should get something
like this (with whatever versions you defined):
At the sbt interactive prompt, type projects to list your projects and project
<projectname> to select a current project. When you run a task like compile,
74
it runs on the current project. So you don’t necessarily have to compile the root
project, you could compile only a subproject.
You can run a task in another project by explicitly specifying the project ID,
such as subProjectID/compile.
Common code
The definitions in .sbt files are not visible in other .sbt files. In order to share
code between .sbt files, define one or more Scala files in the project/ directory
of the build root.
See organizing the build for details.
Using plugins
Please read the earlier pages in the Getting Started Guide first, in particular you
need to understand build.sbt, task graph, library dependencies, before reading
this page.
What is a plugin?
A plugin extends the build definition, most commonly by adding new settings.
The new settings could be new tasks. For example, a plugin could add a
codeCoverage task which would generate a test coverage report.
Declaring a plugin
If your project is in directory hello, and you’re adding sbt-site plugin to the
build definition, create hello/project/site.sbt and declare the plugin depen-
dency by passing the plugin’s Ivy module ID to addSbtPlugin:
Not every plugin is located on one of the default repositories and a plugin’s
documentation may instruct you to also add the repository where it can be
found:
75
resolvers += Resolver.sonatypeRepo("public")
Plugins usually provide settings that get added to a project to enable the plugin’s
functionality. This is described in the next section.
A plugin can declare that its settings be automatically added to the build defi-
nition, in which case you don’t have to do anything to add them.
As of sbt 0.13.5, there is a new auto plugins feature that enables plugins to
automatically, and safely, ensure their settings and dependencies are on a project.
Many auto plugins should have their default settings automatically, however
some may require explicit enablement.
If you’re using an auto plugin that requires explicit enablement, then you have
to add the following to your build.sbt:
The enablePlugins method allows projects to explicitly define the auto plugins
they wish to consume.
Projects can also exclude plugins using the disablePlugins method. For ex-
ample, if we wish to remove the IvyPlugin settings from util, we modify our
build.sbt as follows:
76
> plugins
In file:/home/jsuereth/projects/sbt/test-ivy-issues/
sbt.plugins.IvyPlugin: enabled in scala-sbt-org
sbt.plugins.JvmPlugin: enabled in scala-sbt-org
sbt.plugins.CorePlugin: enabled in scala-sbt-org
sbt.plugins.JUnitXmlReportPlugin: enabled in scala-sbt-org
Here, the plugins output is showing that the sbt default plugins are all enabled.
sbt’s default settings are provided via three plugins:
site.settings
Global plugins
Plugins can be installed for all your projects at once by declaring them in
~/.sbt/0.13/plugins/. ~/.sbt/0.13/plugins/ is an sbt project whose class-
path is exported to all sbt build definition projects. Roughly speaking, any
.sbt or .scala files in ~/.sbt/0.13/plugins/ behave as if they were in the
project/ directory for all projects.
77
You can create ~/.sbt/0.13/plugins//build.sbt and put addSbtPlugin()
expressions in there to add plugins to all your projects at once. Because doing
so would increase the dependency on the machine environment, this feature
should be used sparingly. See Best Practices.
Available Plugins
• those for IDEs (to import an sbt project into your IDE)
• those supporting web frameworks, such as xsbt-web-plugin.
For more details, including ways of developing plugins, see Plugins. For best
practices, see Plugins-Best-Practices.
This page gets you started creating your own settings and tasks.
To understand this page, be sure you’ve read earlier pages in the Getting Started
Guide, especially build.sbt and task graph.
Defining a key
Keys is packed with examples illustrating how to define keys. Most of the keys
are implemented in Defaults.
Keys have one of three types. SettingKey and TaskKey are described in .sbt
build definition. Read about InputKey on the Input Tasks page.
Some examples from Keys:
The key constructors have two string parameters: the name of the key
("scalaVersion") and a documentation string ("The version of scala used
for building.").
Remember from .sbt build definition that the type parameter T in
SettingKey[T] indicates the type of value a setting has. T in TaskKey[T]
indicates the type of the task’s result. Also remember from .sbt build definition
that a setting has a fixed value until project reload, while a task is re-computed
78
for every “task execution” (every time someone types a command at the sbt
interactive prompt or in batch mode).
Keys may be defined in an .sbt file, a .scala file, or in an auto plugin. Any vals
found under autoImport object of an enabled auto plugin will be imported
automatically into your .sbt files.
Implementing a task
Once you’ve defined a key for your task, you’ll need to complete it with a task
definition. You could be defining your own task, or you could be planning to
redefine an existing task. Either way looks the same; use := to associate some
code with the task key:
If the task has dependencies, you’d reference their value using value, as dis-
cussed in task graph.
The hardest part about implementing tasks is often not sbt-specific; tasks are
just Scala code. The hard part could be writing the “body” of your task that
does whatever you’re trying to do. For example, maybe you’re trying to format
HTML in which case you might want to use an HTML library (you would add a
library dependency to your build definition and write code based on the HTML
library, perhaps).
sbt has some utility libraries and convenience functions, in particular you can
often use the convenient APIs in IO to manipulate files and directories.
79
Execution semantics of tasks
When depending on other tasks from a custom task using value, an important
detail to note is the execution semantics of the tasks. By execution semantics,
we mean exactly when these tasks are evaluated.
If we take sampleIntTask for instance, each line in the body of the task should
be strictly evaluated one after the other. That is sequential semantics:
sampleIntTask := {
val sum = 1 + 2 // first
println("sum: " + sum) // second
sum // third
}
In reality JVM may inline the sum to 3, but the observable effect of the task
will remain identical as if each line were executed one after the other.
Now suppose we define two more custom tasks startServer and stopServer,
and modify sampleIntTask as follows:
80
stopServer.value // THIS WON'T WORK
sum
},
sampleStringTask := {
startServer.value
val s = sampleIntTask.value.toString
println("s: " + s)
s
}
)
> sampleIntTask
stopping...
starting...
sum: 3
[success] Total time: 1 s, completed Dec 22, 2014 5:00:00 PM
Figure 4: task-dependency
Unlike plain Scala method calls, invoking value method on tasks will not
be evaluated strictly. Instead, they simply act as placeholders to denote
that sampleIntTask depends on startServer and stopServer tasks. When
sampleIntTask is invoked by you, sbt’s tasks engine will:
81
Deduplication of task dependencies To demonstrate the last point, we
can run sampleStringTask from sbt interactive prompt.
> sampleStringTask
stopping...
starting...
sum: 3
s: 3
[success] Total time: 1 s, completed Dec 22, 2014 5:30:00 PM
Figure 5: task-dependency
If we did not deduplicate the task dependencies, we will end up compiling test
source code many times when test task is invoked since compile in Test
appears many times as a task dependency of test in Test.
Cleanup task How should one implement stopServer task? The notion
of cleanup task does not fit into the execution model of tasks because tasks
are about tracking dependencies. The last operation should become the task
that depends on other intermediate tasks. For instance stopServer should
depend on sampleStringTask, at which point stopServer should be the
sampleStringTask.
82
sampleIntTask := {
startServer.value
val sum = 1 + 2
println("sum: " + sum)
sum
},
sampleStringTask := {
startServer.value
val s = sampleIntTask.value.toString
println("s: " + s)
s
},
sampleStringTask := {
val old = sampleStringTask.value
println("stopping...")
Thread.sleep(500)
old
}
)
> sampleStringTask
starting...
sum: 3
s: 3
stopping...
[success] Total time: 1 s, completed Dec 22, 2014 6:00:00 PM
Figure 6: task-dependency
Use plain Scala Another way of making sure that something happens
after some other thing is to use Scala. Implement a simple function in
project/ServerUtil.scala for example, and you can write:
sampleIntTask := {
ServerUtil.startServer
83
try {
val sum = 1 + 2
println("sum: " + sum)
} finally {
ServerUtil.stopServer
}
sum
}
If you find you have a lot of custom code, consider moving it to a plugin for
re-use across multiple builds.
It’s very easy to create a plugin, as teased earlier and discussed at more length
here.
This page has been a quick taste; there’s much much more about custom tasks
on the Tasks page.
sbt is recursive
build.sbt conceals how sbt really works. sbt builds are defined with Scala code.
That code, itself, has to be built. What better way than with sbt?
The project directory is another build inside your build, which knows how to
build your build. To distinguish the builds, we sometimes use the term proper
build to refer to your build, and meta-build to refer to the build in project.
The projects inside the metabuild can do anything any other project can do.
Your build definition is an sbt project.
And the turtles go all the way down. If you like, you can tweak the build defini-
tion of the build definition project, by creating a project/project/ directory.
Here’s an illustration.
84
hello/ # your build's root project's base directory
Don’t worry! Most of the time you are not going to need all that. But under-
standing the principle can be helpful.
By the way: any time files ending in .scala or .sbt are used, naming them
build.sbt and Dependencies.scala are conventions only. This also means
that multiple files are allowed.
One way of using the fact that .scala files under project becomes part of the
build definition is to create project/Dependencies.scala to track dependen-
cies in one place.
import sbt._
object Dependencies {
// Versions
lazy val akkaVersion = "2.3.8"
// Libraries
85
val akkaActor = "com.typesafe.akka" %% "akka-actor" % akkaVersion
val akkaCluster = "com.typesafe.akka" %% "akka-cluster" % akkaVersion
val specs2core = "org.specs2" %% "specs2-core" % "2.4.17"
// Projects
val backendDeps =
Seq(akkaActor, specs2core % Test)
}
The Dependencies object will be available in build.sbt. To use the vals under
it easier, import Dependencies._.
import Dependencies._
This technique is useful when you have a multi-project build that’s getting large,
and you want to make sure that subprojects to have consistent dependencies.
In .scala files, you can write any Scala code, including top-level classes and
objects.
The recommended approach is to define most settings in a multi-project
build.sbt file, and using project/*.scala files for task implementations or
to share values, such as keys. The use of .scala files also depends on how
comfortable you or your team are with Scala.
For more advanced users, another way of organizing your build is to define
one-off auto plugins in project/*.scala. By defining triggered plugins, auto
plugins can be used as a convenient way to inject custom tasks and commands
across all subprojects.
86
Getting Started summary
• the basics of Scala. It’s undeniably helpful to be familiar with Scala syn-
tax. Programming in Scala written by the creator of Scala is a great
introduction.
• .sbt build definition
• your build definition is a big DAG of tasks and their dependencies.
• to create a Setting, call one of a few methods on a key: :=, +=, or ++=.
• each setting has a value of a particular type, determined by the key.
• tasks are special settings where the computation to produce the key’s value
will be re-run each time you kick off a task. Non-tasks compute the value
once, when first loading the build definition.
• Scopes
• each key may have multiple values, in distinct scopes.
• scoping may use three axes: configuration, project, and task.
• scoping allows you to have different behaviors per-project, per-task, or
per-configuration.
• a configuration is a kind of build, such as the main one (Compile) or the
test one (Test).
• the per-project axis also supports “entire build” scope.
• scopes fall back to or delegate to more general scopes.
• put most of your configuration in build.sbt, but use .scala build defi-
nition files for defining classes and larger task implementations.
• the build definition is an sbt project in its own right, rooted in the project
directory.
• Plugins are extensions to the build definition
• add plugins with the addSbtPlugin method in project/plugins.sbt
(NOT build.sbt in the project’s base directory).
If any of this leaves you wondering rather than nodding, please ask for help, go
back and re-read, or try some experiments in sbt’s interactive mode.
Good luck!
87
Advanced Notes
Since sbt is open source, don’t forget you can check out the source code too!
This page describes an old style of .sbt build definition. The current recom-
mendation is to use Multi-project .sbt build definition.
Unlike Multi-project .sbt build definition and .scala build definition that explic-
itly define a Project definition, bare build definition implicitly defines one based
on the location of the .sbt file.
Instead of defining Projects, bare .sbt build definition consists of a list of
Setting[_] expressions.
name := "hello"
version := "1.0"
scalaVersion := "2.12.2"
Note: This blank line delimitation will no longer be needed after 0.13.7.
You can’t write a bare build.sbt like this:
sbt needs some kind of delimiter to tell where one expression stops and the next
begins.
This page describes an old style of .scala build definition. In the previous
versions of sbt, .scala was the only way to create multi-project build definition,
88
but sbt 0.13 added multi-project .sbt build definition, which is the recommended
style.
We assume you’ve read previous pages in the Getting Started Guide, especially
.sbt build definition.
To mix .sbt and .scala files in your build definition, you need to understand
how they relate.
The following two files illustrate. First, if your project is in hello, create
hello/project/Build.scala as follows:
import sbt._
import Keys._
Start up the sbt interactive prompt. Type inspect sampleKeyA and you should
see (among other things):
89
[info] Setting: java.lang.String = A: in Build.settings in Build.scala
[info] Provided by:
[info] {file:/home/hp/checkout/hello/}/*:sampleKeyA
Note that the “Provided by” shows the same scope for the two values. That is,
sampleKeyC in ThisBuild in a .sbt file is equivalent to placing a setting in
the Build.settings list in a .scala file. sbt takes build-scoped settings from
both places to create the build definition.
Now, inspect sampleKeyB:
sbt appends the settings from .sbt files to the settings from Build.settings
and Project.settings which means .sbt settings take precedence. Try
changing Build.scala so it sets key sampleC or sampleD, which are also
set in build.sbt. The setting in build.sbt should “win” over the one in
Build.scala.
One other thing you may have noticed: sampleKeyC and sampleKeyD were
available inside build.sbt. That’s because sbt imports the contents of your
Build object into your .sbt files. In this case import HelloBuild._ was
implicitly done for the build.sbt file.
In summary:
• In .scala files, you can add settings to Build.settings for sbt to find,
and they are automatically build-scoped.
90
• In .scala files, you can add settings to Project.settings for sbt to find,
and they are automatically project-scoped.
• Any Build object you write in a .scala file will have its contents imported
and available to .sbt files.
• The settings in .sbt files are appended to the settings in .scala files.
• The settings in .sbt files are project-scoped unless you explicitly specify
another scope.
You can switch the sbt interactive prompt to have the build definition project
in project/ as the current project. To do so, type reload plugins.
As shown above, you use reload return to leave the build definition project
and return to your regular project.
It would be wrong to think that the settings in build.sbt are added to the
settings fields in Build and Project objects. Instead, the settings list from
Build and Project, and the settings from build.sbt, are concatenated into
another immutable list which is then used by sbt. The Build and Project
objects are “immutable configuration” forming only part of the complete build
definition.
In fact, there are other sources of settings as well. They are appended in this
order:
91
• Settings from .sbt files in the project.
• Build definition projects (i.e. projects inside project) have settings from
global plugins (~/.sbt/0.13/plugins/) added. Using plugins explains
this more.
Later settings override earlier ones. The entire list of settings forms the build
definition.
General Information
Credits
See the sbt contributors on GitHub and sbt GitHub organization members.
Additionally, these people have contributed ideas, documentation, or code to
sbt but are not recorded in either of the above:
• Josh Cough
• Nolan Darilek
• Nathan Hamblen
• Ismael Juma
• Viktor Klang
• David R. MacIver
• Ross McDonald
• Andrew O’Malley
• Jorge Ortiz
• Mikko Peltonen
• Ray Racine
• Stuart Roebuck
• Harshad RJ
• Tony Sloane
• Seth Tisue
• Francisco Treacy
• Vesa Vilhonen
92
Community Plugins
sbt Organization
The sbt organization is available for use by any sbt plugin. Developers who
contribute their plugins into the community organization will still retain control
over their repository and its access. The goal of the sbt organization is to
organize sbt software into one central location.
A side benefit to using the sbt organization for projects is that you can use
gh-pages to host websites under the http://scala-sbt.org domain.
Lightbend has provided a freely available Ivy Repository for sbt projects to
use. This Ivy repository is mirrored from the freely available Bintray service. If
you’d like to submit your plugin, please follow these instructions: Bintray For
Plugins.
Available Plugins
Please feel free to submit a pull request that adds your plugin to the list.
• IntelliJ IDEA
– sbt Plugin to generate IDEA project configuration: https://github.
com/mpeltonen/sbt-idea
• Netbeans (no support to create a new sbt project yet)
– sbt-netbeans-plugin (older): https://github.com/remeniuk/
sbt-netbeans-plugin
– sbt plugin to generate NetBeans configuration: https://github.com/
dcaoyuan/nbsbt
– sbt plugin to add scala support to NetBeans: https://github.com/
dcaoyuan/nbscala
• Eclipse: https://github.com/typesafehub/sbteclipse
• Sublime Text: https://github.com/orrsella/sbt-sublime
• Ensime: https://github.com/aemoncannon/ensime-sbt-cmd
• sbt-mode for Emacs: https://github.com/hvesalai/sbt-mode
• sbt-ctags (manage library dependency sources for vim, emacs, sublime):
https://github.com/kalmanb/sbt-ctags
• sbt-scuggest (generates Scuggest import entries for Sublime Text): https:
//github.com/ssanj/sbt-scuggest
93
Test plugins
• junit_xml_listener: https://github.com/ijuma/junit_xml_listener
• sbt-growl-plugin: https://github.com/softprops/sbt-growl-plugin
• sbt-teamcity-test-reporting-plugin: https://github.com/guardian/
sbt-teamcity-test-reporting-plugin
• xsbt-cucumber-plugin: https://github.com/skipoleschris/xsbt-cucumber-plugin
• sbt-multi-jvm: https://github.com/sbt/sbt-multi-jvm
• sbt-testng-interface: https://github.com/sbt/sbt-testng-interface
• sbt-doctest: https://github.com/tkawachi/sbt-doctest
• sbt-cassandra-plugin: https://github.com/hochgi/sbt-cassandra-plugin
• sbt-tabular-test-reporter: https://github.com/programmiersportgruppe/
sbt-tabular-test-reporter
• sbt-notifications (sends notifications when test run is finished): https://
github.com/PavelPenkov/sbt-notifications
• sbt-dynamodb (downloads and runs DynamoDB Local for testing): https:
//github.com/localytics/sbt-dynamodb
• sbt-sqs (downloads and runs ElasticMQ for testing): https://github.com/
localytics/sbt-sqs
• sbt-s3 (downloads and runs S3Proxy for testing): https://github.com/
localytics/sbt-s3
• sbt-scripted-scalatest (test SBT plugins with ScalaTest): https://github.
com/daniel-shuy/scripted-scalatest-sbt-plugin
• sbt-flaky (detects flaky tests by running tests multiple times): https://
github.com/otrebski/sbt-flaky
• sbt-jupiter-interface (test framework for JUnit Jupiter) https://github.
com/maichler/sbt-jupiter-interface
• sbt-scct: https://github.com/sqality/sbt-scct
• sbt-scoverage: https://github.com/scoverage/sbt-scoverage
• jacoco4sbt: https://github.com/sbt/jacoco4sbt
• sbt-coveralls: https://github.com/scoverage/sbt-coveralls
• sbt-clover: https://github.com/shanbin/sbt-clover
94
• sbt-findbugs-plugin (FindBugs - static analysis for Java code): https://
github.com/lenioapp/sbt-findbugs-plugin
• findbugs4sbt (FindBugs only supports Java projects atm): https://github.
com/sbt/findbugs4sbt
• scalastyle (Scalastyle - static code checker for Scala): https://github.com/
scalastyle/scalastyle-sbt-plugin
• sbt-scapegoat (Scapegoat - Scala static code analysis): https://github.
com/sksamuel/sbt-scapegoat
• sbt-stats (simple, extensible source code statistics): https://github.com/
orrsella/sbt-stats
• sbt-checkstyle-plugin (Checkstyle - static analysis for Java code): https:
//github.com/etsy/sbt-checkstyle-plugin
• sbt-jcheckstyle (handy checkstyle runner for Java projects): https:
//github.com/xerial/sbt-jcheckstyle
• sbt-verify-plugin (statically verify the integrity of downloaded dependen-
cies): https://github.com/lenioapp/sbt-verify-plugin
• sbt-dependency-check (check project dependencies for publicly known vul-
nerabilities/CVEs): https://github.com/albuch/sbt-dependency-check
• sbt-assembly: https://github.com/sbt/sbt-assembly
– sbt-assembly-log4j2 (sbt-assembly MergeStrategy for log4j2 plugins):
https://github.com/idio/sbt-assembly-log4j2
• xsbt-proguard-plugin: https://github.com/adamw/xsbt-proguard-plugin
• sbt-deploy: https://github.com/reaktor/sbt-deploy
• sbt-appbundle (os x standalone): https://github.com/sbt/sbt-appbundle
• sbt-onejar (Packages your project using One-JAR™): https://github.
com/sbt/sbt-onejar
Release plugins
• sbt-native-packager: https://github.com/sbt/sbt-native-packager
• sbt-ghpages (publishes generated site and api): https://github.com/sbt/
sbt-ghpages
• sbt-pgp (PGP signing plugin, can generate keys too): https://github.com/
sbt/sbt-pgp
• sbt-release (customizable release process): https://github.com/sbt/
sbt-release
• sbt-sonatype-plugin (releases to Sonatype Nexus repository): https:
//github.com/xerial/sbt-sonatype
95
• sbt-aether-plugin (Published artifacts using Sonatype Aether): https://
github.com/arktekk/sbt-aether-deploy
• posterous-sbt: https://github.com/n8han/posterous-sbt
• sbt-signer-plugin: https://github.com/rossabaker/sbt-signer-plugin
• sbt-izpack (generates IzPack an installer): http://software.clapper.org/
sbt-izpack/
• sbt-unique-version (emulates unique snapshots): https://github.com/sbt/
sbt-unique-version
• sbt-install4j: https://github.com/jpsacha/sbt-install4j
• sbt-pack (generates packages with dependent jars and launch scripts):
https://github.com/xerial/sbt-pack
• sbt-start-script: https://github.com/sbt/sbt-start-script
• xitrum-package (collects dependency .jar files for standalone Scala pro-
grams): https://github.com/ngocdaothanh/xitrum-package
• sbt-dist-zip (Create a distributable zip file): https://github.com/timt/
sbt-dist-zip
• sbt-hadoop (Publish artifacts to the Hadoop Distributed File System,
HDFS): https://github.com/Tapad/sbt-hadoop-oss
• sbt-appengine: https://github.com/sbt/sbt-appengine
• sbt-cloudbees-plugin: https://github.com/timperrett/sbt-cloudbees-plugin
• sbt-jelastic-deploy: https://github.com/casualjim/sbt-jelastic-deploy
• sbt-elasticbeanstalk (Deploy WAR files to AWS Elastic Beanstalk): https:
//github.com/sqs/sbt-elasticbeanstalk
• sbt-cloudformation (AWS CloudFormation templates and stacks manage-
ment): https://github.com/tptodorov/sbt-cloudformation
• sbt-codedeploy: https://github.com/gilt/sbt-codedeploy
• sbt-heroku: https://github.com/heroku/sbt-heroku
• sbt-heroku-deploy: https://github.com/earldouglas/sbt-heroku-deploy
• sbt-aws-fun (Deploy Jar files to AWS Lambda): https://github.com/
TailrecIO/sbt-aws-fun
• sbt-aws-serverless (Deploy code to AWS Serverless Architecture): https:
//github.com/yoshiyoshifujii/sbt-aws-serverless
• sbt-marathon (Deploy applications on Apache Mesos using the Marathon
framework): https://github.com/Tapad/sbt-marathon
• sbt-newrelic: https://github.com/gilt/sbt-newrelic
96
Web and frontend development plugins
• xsbt-web-plugin: https://github.com/earldouglas/xsbt-web-plugin
• xsbt-webstart: https://github.com/ritschwumm/xsbt-webstart
• sbt-gwt-plugin: https://github.com/cdietze/sbt-gwt-plugin
• coffeescripted-sbt: https://github.com/softprops/coffeescripted-sbt
• less-sbt (for less-1.3.0): https://github.com/softprops/less-sbt
• sbt-less-plugin (it uses less-1.3.0): https://github.com/btd/sbt-less-plugin
• sbt-emberjs: https://github.com/stefri/sbt-emberjs
• sbt-closure: https://github.com/eltimn/sbt-closure
• sbt-imagej: https://github.com/jpsacha/sbt-imagej
• sbt-yui-compressor: https://github.com/indrajitr/sbt-yui-compressor
• sbt-requirejs: https://github.com/scalatra/sbt-requirejs
• sbt-vaadin-plugin: https://github.com/henrikerola/sbt-vaadin-plugin
• sbt-purescript: https://github.com/eamelink/sbt-purescript
• sbt-elm: https://github.com/choucrifahed/sbt-elm
• sbt-js-test (Run javascript tests on the JVM with browser APIs): https:
//github.com/joescii/sbt-js-test
• sbt-javafx (Package JavaFX applications): https://github.com/kavedaa/
sbt-javafx
• sbt-phantomjs (Automated installer and configurator for PhantomJS):
https://github.com/saturday06/sbt-phantomjs
• sbt-web-scalajs: https://github.com/vmunier/sbt-web-scalajs
• scalatra-sbt: https://github.com/scalatra/scalatra-sbt
• sbt-scala-js-map (Configures source mapping for Scala.js projects hosted
on GitHub): https://github.com/ThoughtWorksInc/sbt-scala-js-map
• Naptime API framework plugin (http://coursera.github.io/naptime):
https://github.com/coursera/naptime
• sbt-npm (Run npm tasks as part of sbt build process): https://github.
com/timt/sbt-npm
Documentation plugins
97
• sbt-api-mappings (Resolves external links in ScalaDoc for common Scala
libraries): https://github.com/ThoughtWorksInc/sbt-api-mappings
• sbt-scaliterate (generates source code from a programming book written
in Markdown): https://github.com/wookietreiber/sbt-scaliterate
• sbt-dash (Creates a Dash docset from Scaladoc) https://bintray.com/
jastice/sbt-plugins/sbt-dash
• sbt-gherkin-converter (Create html from gherkin feature files directly from
sbt): https://github.com/randomcoder/sbt-gherkin-converter
• ant4sbt: https://github.com/sbt/ant4sbt
• sbt-pom-reader: https://github.com/sbt/sbt-pom-reader
98
Utility and system plugins
99
• cronish-sbt (interval sbt / shell command execution): https://github.com/
philcali/cronish-sbt
• git (executes git commands): https://github.com/sbt/sbt-git
• svn (execute svn commands): https://github.com/xuwei-k/sbtsvn
• sbt-groll (sbt plugin to navigate the Git history): https://github.com/sbt/
sbt-groll
• sbt-twt (twitter processor for sbt): https://github.com/sbt/sbt-twt
• sbt-compile-quick-plugin (compile and package a single file): https:
//github.com/etsy/sbt-compile-quick-plugin
• sbt-meow (display ascii-fied random cat pictures): https://github.com/
thricejamie/sbt-meow
• sbt-build-files-watcher (show message on build files changed): https://
github.com/tototoshi/sbt-build-files-watcher
• sbt-backup (compress and scp a directory): https://github.com/sensatus/
sbt-backup
• sbt-project-graph (visualise inter-project dependencies): https://github.
com/dwijnand/sbt-project-graph
• solr-plugin (start solr search engine from sbt): https://github.com/
sgrouples/sbt-solr-plugin
• sbt-todolist (find TODOs in source files and print them to console): https:
//github.com/fedragon/sbt-todolist
• sbt-ortho (simple spell and English style checker): https://github.com/
henrikengstrom/sbt-ortho
• sbt-write-output-to-file (redirect the output of run to a file): https://
github.com/cb372/sbt-write-output-to-file
• sbt-jol (OpenJDK JOL (Java Object Layout) integration for sbt and
Scala): https://github.com/ktoso/sbt-jol
• sbt-hocon (operations on HOCON data against reference configuration
aggregated from project dependencies): https://github.com/aalleexxeeii/
sbt-hocon
• sbt-gitlab (SBT plugin for interacting with GitLab and GitLabCI through
the available API’s): https://gitlab.com/kpmeen/sbt-gitlab
• sbt-tmpfs (SBT plugin that automatically “tmpfsifies” directories to speed
up development.) https://github.com/cuzfrog/sbt-tmpfs
• sbt-check (compile up to, and including, the typer phase): https://github.
com/jeffreyolchovy/sbt-check
Database plugins
100
• sbt-liquibase-slick-codegen (Generate Slick database schema code
from Liquibase changelog file): https://github.com/daniel-shuy/
liquibase-slick-codegen-sbt-plugin
• sbt-dbdeploy (dbdeploy, a database change management tool): https://
github.com/mr-ken/sbt-dbdeploy
• sbt-pillar (tasks for managing Cassandra DB migrations using Pillar li-
brary): https://github.com/henders/sbt-pillar-plugin
101
• sbt-scalashim (Generate Scala shim like sys.error): https://github.com/
sbt/sbt-scalashim
• sbtend (Generate Java source code from xtend ): https://github.com/
xuwei-k/sbtend
• sbt-boilerplate (generating scala.Tuple/Function related boilerplate code):
https://github.com/sbt/sbt-boilerplate
• sbt-fxml (Generates controller classes for JavaFX FXML files): https://
bitbucket.org/phdoerfler/sbt-fxml
• sbt-clojure (Compiling Clojure code): https://github.com/Geal/
sbt-clojure
• sbt-build-info-conf (Generates resources.conf file with build information):
https://github.com/Sensatus/sbt-build-info-conf
• sbt-frege (Build Frege code): https://github.com/earldouglas/sbt-frege
• sbt-swagger-codegen (Models, Client and Server code generation inte-
grated as an SBT plugin. Generate code from your Swagger(https://github.com/swagger-
api) files): https://github.com/unicredit/sbt-swagger-codegen
• scavro (Code generation from Avro schema): https://github.com/oedura/
scavro
• sbt-spi-plugin (Generates provider-configuration files in the resource di-
rectory META-INF/services for later use with ServiceLoader): https://
github.com/nyavro/spi-plugin
• sbt-dsl-platform (DSL Platform compiler plugin. Code/library generation
from various languages. Postgres/Oracle database migrations): https://
github.com/ngs-doo/dsl-compiler-client
• sbt-json (Generates Scala case classes from JSON documents): https://
github.com/battermann/sbt-json
Android plugins
• android-plugin: https://github.com/jberkel/android-plugin
• android-sdk-plugin: https://github.com/pfn/android-sdk-plugin
• sbt-crashlytics (Provides crashlytics support for android applications):
https://github.com/seroperson/sbt-crashlytics
iOS plugins
102
• sbt-robovm (Compiling Scala using RoboVM for iOS or native OSX):
https://github.com/roboscala/sbt-robovm
OSGi plugin
• sbtosgi: https://github.com/sbt/sbt-osgi
• sbt-javacpp (JavaCPP is the missing bridge between Java and native C++;
this lib helps you download platform-specific presets): https://github.
com/lloydmeta/sbt-javacpp
• sbt-jni (Suite of sbt plugins for simplifying creation and distribution of
JNI programs): https://github.com/jodersky/sbt-jni
Benchmarking plugins
Plugin bundles
The community repository has the following guideline for artifacts published to
it:
103
1. All published artifacts are the authors own work or have an appropriate
license which grants distribution rights.
2. All published artifacts come from open source projects, that have an open
patch acceptance policy.
3. All published artifacts are placed under an organization in a DNS domain
for which you have the permission to use or are an owner (scala-sbt.org is
available for sbt plugins).
4. All published artifacts are signed by a committer of the project (coming
soon).
Now, we’ll create a repository to host our personal sbt plugins. In Bintray,
create a generic repository called sbt-plugins.
First, go to your user page and click on the new repository link:
You should see the following dialog:
Fill it out similarly to the above image, the settings are:
• Name: sbt-plugins
• Type: Generic
104
• Desc: My sbt plugins
• Tags: sbt
Once this is done, you can begin to configure your sbt-plugins to publish to
Bintray.
Next, make sure your build.sbt file has the following settings
Make sure your project has a valid license specified, as well as unique name and
organization.
Make a release
Once your build is configured, open the sbt console in your build and run
105
sbt> publish
The plugin will need your credentials. If you don’t know where they are, you
can find them on Bintray.
This will get you your password. You can create a credentials file with the
bintrayChangeCredentials task. The sbt-bintray plugin will save your API
key for future use.
NOTE: We have to do this before we can link our package to the sbt org.
Now that your plugin is packaged on Bintray, you can include it in the commu-
nity sbt repository. To do so, go to the Community sbt repository screen.
1. Click the green include my package button and select your plugin.
2. Search for your plugin by name and click on the link.
3. Your request should be automatically filled out, just click send
4. Shortly, one of the sbt repository admins will approve your link request.
From here on, any releases of your plugin will automatically appear in the
community sbt repository. Congratulations and thank you so much for your
contributions!
If you’re a member of the sbt organization on Bintray, you can link your package
to the sbt organization, but via a different means. To do so, first navigate to
the plugin you wish to include and click on the link button:
After clicking this you should see a link like the following:
Click on the sbt/sbt-plugin-releases repository and you’re done! Any future
releases will be included in the sbt-plugin repository.
106
Summary
After setting up the repository, all new releases will automatically be included
the sbt-plugin-releases repository, available for all users. When you create a
new plugin, after the initial release you’ll have to link it to the sbt community
repository, but the rest of the setup should already be completed. Thanks for
you contributions and happy hacking.
Setup Notes
Terminal encoding
The character encoding used by your terminal may differ from Java’s default
encoding for your platform. In this case, you will need to add the option
-Dfile.encoding=<encoding> in your sbt script to set the encoding, which
might look like:
java -Dfile.encoding=UTF8
If you find yourself running out of permgen space or your workstation is low on
memory, adjust the JVM configuration as you would for any application. For
example a common set of memory-related options is:
Boot directory
sbt-launch.jar is just a bootstrap; the actual meat of sbt, and the Scala
compiler and standard library, are downloaded to the shared directory
$HOME/.sbt/boot/.
To change the location of this directory, set the sbt.boot.directory system
property in your sbt script. A relative path will be resolved against the current
107
working directory, which can be useful if you want to avoid sharing the boot
directory between projects. For example, the following uses the pre-0.11 style
of putting the boot directory in project/boot/:
java -Dsbt.boot.directory=project/boot/
HTTP/HTTPS/FTP Proxy
On Unix, sbt will pick up any HTTP, HTTPS, or FTP proxy settings from the
standard http_proxy, https_proxy, and ftp_proxy environment variables. If
you are behind a proxy requiring authentication, your sbt script must also
pass flags to set the http.proxyUser and http.proxyPassword properties
for HTTP, ftp.proxyUser and ftp.proxyPassword properties for FTP, or
https.proxyUser and https.proxyPassword properties for HTTPS.
For example,
On Windows, your script should set properties for proxy host, port, and if
applicable, username and password. For example, for HTTP:
Replace http with https or ftp in the above command line to configure HTTPS
or FTP.
Using Sonatype
Sonatype setup
The OSSRH Guide walks you through the required process of setting up the
account with Sonatype. It’s as simple as creating a Sonatype’s JIRA account
and then a New Project ticket. When creating the account, try to use the same
108
domain in your email address that the project is hosted on. It makes it easier for
Sonatype to validate the relationship with the groupId requested in the ticket,
but it is not the only method used to confirm the ownership.
Creation of the New Project ticket is as simple as:
After creating your Sonatype account on JIRA, you can log in to the Nexus
Repository Manager using the same credentials, although this is not required in
the guide, it can be helpful later to check on published artifacts.
SBT setup
First - PGP Signatures With the PGP key you want to use, you can sign the
artifacts you want to publish to the Sonatype repository with the sbt-pgp plugin.
Follow the instructions for the plugin and you’ll have PGP signed artifacts in
no time.
In short, add the following line to your ~/.sbt/0.13/plugins/gpg.sbt file to
enable it globally for SBT projects:
If you don’t have the PGP keys to sign your code with, one of the ways to
achieve that is to install the GNU Privacy Guard and:
109
• use it to generate the keypair you will use to sign your library,
• publish your certificate to enable remote verification of the signatures,
• make sure that the gpg command is in PATH available to the sbt,
• add useGpg := true to your build.sbt to make the plugin gpg-aware
PGP Tips’n’tricks If the command to generate your key fails, execute the
following commands and remove the displayed files:
If your PGP key has not yet been distributed to the keyserver pool, e.g., you’ve
just generated it, you’ll need to publish it. You can do so using the sbt-pgp
plugin:
Where keyname is the name or email address used when creating the key or
hexadecimal identifier for the key.
If you see no output from sbt-pgp then the key name specified was not found.
If it fails to run the SendKey command you can try another server (for example:
hkp://keyserver.ubuntu.com). A list of servers can be found at the status page
of sks-keyservers.net.
110
Now, we want to control what’s available in the pom.xml file. This file describes
our project in the maven repository and is used by indexing services for search
and discover. This means it’s important that pom.xml should have all informa-
tion we wish to advertise as well as required info!
First, let’s make sure no repositories show up in the POM file. To publish
on maven-central, all required artifacts must also be hosted on maven central.
However, sometimes we have optional dependencies for special features. If that’s
the case, let’s remove the repositories for optional dependencies in our artifact:
homepage := Some(url("http://example.com"))
scmInfo := Some(
ScmInfo(
url("https://github.com/your-account/your-project"),
"scm:[email protected]:your-account/your-project.git"
)
)
developers := List(
Developer(
id = "Your identifier",
name = "Your Name",
email = "your@email",
url = url("http://your.url")
)
)
publishMavenStyle := true
111
Setting repositories to publish to:
publishTo := {
val nexus = "https://oss.sonatype.org/"
if (isSnapshot.value)
Some("snapshots" at nexus + "content/repositories/snapshots")
else
Some("releases" at nexus + "service/local/staging/deploy/maven2")
}
To simplify the usage of the Sonatype’s Nexus, add the following line to
build.sbt to import the sbt-sonatype plugin to your project:
This plugin will facilitate the publishing process, but in short, these are the
main steps for publishing the libraries to the repository:
112
Publishing tips’n’tricks Use staged releases to test across large projects of
independent releases before pushing the full project.
To automate the publishing approach above with the sbt-release plugin, you
should simply add the publishing commands as steps in the releaseProcess
task:
...
ReleaseStep(action = Command.process("sonatypeOpen \"your groupId\" \"Some staging name\"", _))
...
ReleaseStep(action = Command.process("publishSigned", _)),
...
ReleaseStep(action = Command.process("sonatypeRelease", _)),
...
Contributing to sbt
Below is a running list of potential areas of contribution. This list may become
out of date quickly, so you may want to check on the sbt-dev mailing list if you
are interested in a specific topic.
113
• ‘update’ produces an Update Report mapping Configura-
tion/ModuleID/Artifact to the retrieved File
• Ivy produces more detailed XML reports on dependencies. These
come with an XSL stylesheet to view them, but this does not scale to
large numbers of dependencies. Working on this is pretty straightfor-
ward: the XML files are created in ~/.ivy2 and the .xsl and .css
are there as well, so you don’t even need to work with sbt. Other
approaches described in the email thread
• Tasks are a combination of static and dynamic graphs and it would
be useful to view the graph of a run
• Settings are a static graph and there is code to generate the dot files,
but isn’t hooked up anywhere.
2. There is support for dependencies on external projects, like on GitHub. To
be more useful, this should support being able to update the dependencies.
It is also easy to extend this to other ways of retrieving projects. Support
for svn and hg was a recent contribution, for example.
3. Dependency management: see adept
4. If you like parsers, sbt commands and input tasks are written using cus-
tom parser combinators that provide tab completion and error handling.
Among other things, the efficiency could be improved.
5. The javap task hasn’t been reintegrated
6. Implement enhanced 0.11-style warn/debug/info/error/trace commands.
Currently, you set it like any other setting:
warn test:run
Also, trace is currently an integer, but should really be an abstract data type.
7. Each sbt version has more aggressive incremental compilation and reproduc-
ing bugs can be difficult. It would be helpful to have a mode that generates a
diff between successive compilations and records the options passed to scalac.
This could be replayed or inspected to try to find the cause.
Documentation
1. There’s a lot to do with this documentation. If you check it out from git,
there’s a directory called Dormant with some content that needs going
through.
114
2. the main page mentions external project references (e.g. to a git repo) but
doesn’t have anything to link to that explains how to use those.
3. API docs are much needed.
4. Find useful answers or types/methods/values in the other docs, and pull
references to them up into /faq or /Name-Index so people can find the
docs. In general the /faq should feel a bit more like a bunch of pointers
into the regular docs, rather than an alternative to the docs.
5. A lot of the pages could probably have better names, and/or little 2-4
word blurbs to the right of them in the sidebar.
Changes
Introduction
Before sbt 0.13 (sbt 0.9 to 0.12) it was very common to see in builds the usage
of three aspects of sbt:
The release of sbt 0.13 (which was over 3 years ago!) introduced the .value DSL
which allowed for much easier to read and write code, effectively making the first
two aspects redundant and they were removed from the official documentation.
Similarly, sbt 0.13’s introduction of multi-project build.sbt made the Build
trait redundant. In addition, the auto plugin feature that’s now standard in sbt
0.13 enabled automatic sorting of plugin settings and auto import feature, but
it made Build.scala more difficult to maintain.
As they will be removed in upcoming release of sbt 1.0.0 we’ve deprecated them
in sbt 0.13.13, and here we’ll help guide you to how to migrate your code.
115
a <<= aTaskDef
b <+= bTaskDef
c <++= cTaskDefs
a := aTaskDef.value
b += bTaskDef.value
c ++= cTaskDefs.value
As mentioned above, there are two tuple enrichments .apply and .map. The
difference used to be for whether you’re defining a setting for a SettingKey or
a TaskKey, you use .apply for the former and .map for the latter:
sett1 := "s1"
sett2 := "s2"
sett3 <<= (sett1, sett2)(_ + _)
(Remember you can define tasks in terms of settings, but not the other way
round)
With the .value DSL you don’t have to know or remember if your key is a
SettingKey or a TaskKey:
sett1 := "s1"
sett2 := "s2"
sett3 := sett1.value + sett2.value
116
task1 := { println("t1"); "t1" }
task2 := { println("t2"); "t2" }
task3 := { println(task1.value + task2.value); task1.value + task2.value }
task4 := { println(sett1.value + sett2.value); sett1.value + sett2.value }
a <<= a dependsOn b
define it as:
a := (a dependsOn b).value
Note: You’ll need to use the <<= operator with .triggeredBy and .runBefore
in sbt 0.13.13 and earlier due to issue #1444.
val sourceGenerators =
settingKey[Seq[Task[Seq[File]]]]("List of tasks that generate sources")
val resourceGenerators =
settingKey[Seq[Task[Seq[File]]]]("List of tasks that generate resources")
or in general,
117
Migrating with InputKey
run := docsRunSetting.evaluated
import sbt._
import Keys._
import xyz.XyzPlugin.autoImport._
118
You can migrate to build.sbt:
119
Note: Build traits is deprecated, but you can still use project/*.scala file
to organize your build and/or define ad-hoc plugins. See Organizing the build.
sbt 0.13.5+ releases of sbt are technology previews of what’s to come to sbt
1.0 with enhancements like auto plugins, launcher enhacements for sbt server,
defined in the sbt-remote-control project, and other necessary API changes.
These releases maintain binary compatibility with plugins that are published
against sbt 0.13.0, but add new features in preparation for sbt 1.0. The tech
previews allow us to test new ideas like auto plugins and performance improve-
ments on dependency resolution; the build users can try new features without
losing the existing plugin resources; and plugin authors can gradually migrate
to the new plugin system before sbt 1.0 arrives.
sbt 0.13.17
Improvements
Bug fixes
Internal
120
sbt 0.13.16
Improvements
Bug fixes
sbt-cross-building
^^ 1.0.0-RC2
Your plugin will now build with sbt 1.0.0-RC2 (and its Scala version 2.12.2).
121
If you need to make changes specific to a sbt version, you can now include
them into src/main/scala-sbt-0.13, and src/main/scala-sbt-1.0, where
the binary sbt version number is used as postfix.
To run a command across multiple sbt versions, set:
Then, run:
^ compile
#3133 by [@eed3si9n][@eed3si9n]
After:
[warn] Found version conflict(s) in library dependencies; some are suspected to be binary incomp
[warn]
[warn] * com.typesafe.akka:akka-actor_2.12:2.5.0 is selected over 2.4.17
[warn] +- de.heikoseeberger:akka-log4j_2.12:1.4.0 (depends on 2.5.0)
[warn] +- com.typesafe.akka:akka-parsing_2.12:10.0.6 (depends on 2.4.17)
[warn] +- com.typesafe.akka:akka-stream_2.12:2.4.17 () (depends on 2.4.17)
[warn]
[warn] Run 'evicted' to see detailed eviction warnings
#3202 by [@eed3si9n][@eed3si9n]
122
and there is a significant performance improvement gained for your compilation.
The startup message that we added in sbt 0.13.15 was a bit too aggressive, so
we are toning it down in 0.13.16. It will only be triggered for sbt compile, and
it can also be supressed with suppressSbtShellNotification := true.
#3091/#3097/#3147 by [@dwijnand][@dwijnand]
sbt 0.13.15
• sbt 0.13.15 removes the Maven version range when possible. See below.
Improvements
Bug fixes
123
• Fixes Ctrl-C not working on Windows by bumping up JLine. #1855 by
[@eed3si9n][@eed3si9n]
• Fixes regressions in sbt 0.13.11 - 0.13.13 that processed build-level keys
incorrectly. #2851/#2460 by [@eed3si9n][@eed3si9n]
• Fixes a regression in sbt 0.13.12 that was misfiring Scala version enforce-
ment when configuration does not extend Compile. #2827/#2786 by
[@eed3si9n][@eed3si9n]
• Fixes Scala binary version checking misfiring on configurations that do not
extend Compile. #2828/#1466 by [@eed3si9n][@eed3si9n]
• Fixes script mode ignoring quotation. #2551 by [@ekrich][@ekrich]
• Fixes IllegalStateException that Ivy gets into sometimes. #2827/#2015
by [@eed3si9n][@eed3si9n]
• Fixes null sourceFile causing NPE. #2766 by [@avdv][@avdv]
• Fixes version parsing in validation. #3011 by [@eed3si9n][@eed3si9n]
• Fixes Package.makeJar deleting directory recursively. #3014 by
[@larsrh][@larsrh]
Offline installation
sbt 0.13.15 adds two new repositories called “local-preloaded-ivy” and “local-
preloaded” that point to ~/.sbt/preloaded/. The purpose for the repositories
is to preload them with sbt artifacts so the installation of sbt will not require
access to the Internet.
This also improves the startup time of sbt when you first run it since the reso-
lution happens off of a local-preloaded repository.
#2993/#145 by [@eed3si9n][@eed3si9n]
124
Notes
No changes should be necessary to your project definition and all plugins pub-
lished for sbt 0.13.{x|x<14} should still work.
See Migrating from sbt 0.12.x for details on the old operator deprecation.
Special thanks to the contributors for making this release a success. Accord-
ing to git shortlog -sn --no-merges v0.13.13..0.13.15, compared to
0.13.13, there were 64 (non-merge) commits, by eleven contributors: Eugene
Yokota, Dale Wijnand, Guillaume Martres, Jason Zaugg, Lars Hupel, Petro
Verkhogliad, Eric Richardson, Claudio Bley, Haochi Chen, Paul Draper, Ashley
Mercer. Thank you!
sbt 0.13.14
sbt 0.13.14 did not happen due a bug that was found after the artifact was
published.
sbt 0.13.13
• Deprecates the old sbt 0.12 DSL, to be removed in sbt 1.0. See below for
more details.
• The .value method is deprecated for input tasks. Calling .value on
an input key returns an InputTask[A], which is completely unintuitive
and often results in a bug. In most cases .evaluated should be called,
which returns A by evaluating the task. Just in case InputTask[A]
is needed, .inputTaskValue method is now provided. #2709 by
[@eed3si9n][@eed3si9n]
• sbt 0.13.13 renames the early command --<command> that was added in
0.13.1 to early(<command>). This fixes the regression #1041. For back-
ward compatibility --error, --warn, --info, and --debug will continue
to function during the 0.13 series, but it is strongly encouraged to migrate
to the single hyphen options: -error, -warn, -info, and -debug. #2742
by [@eed3si9n][@eed3si9n]
• Improve show when key returns a Seq by showing the elements one
per line. Disable with -Dsbt.disable.show.seq=true. #2755 by
[@eed3si9n][@eed3si9n]
• Recycles classloaders to be anti-hostile to JIT. Disable with -Dsbt.disable.interface.classloader.cac
#2754 by [@retronym][@retronym]
125
Improvements
Bug fixes
sbt 0.13.13 adds a new command, which helps create new build definitions. The
new command is extensible via a mechanism called the template resolver. A
template resolver pattern matches on the passed in arguments after new, and if
it’s a match it will apply the template.
As a reference implementation, template resolver for Giter8 is provided. For
instance:
126
sbt new eed3si9n/hello.g8
Synthetic subprojects
127
#2532/#2717/#2738 by [@eed3si9n][@eed3si9n]
The no-longer-documented operators <<=, <+=, and <++= and tuple enrichments
are deprecated, and will be removed in sbt 1.0.
Generally,
task3 <<= (task1, task2) map { (t1, t2) => println(t1 + t2); t1 + t2 }
should migrate to
task3 := {
println(task1.value + task2.value)
task1.value + task2.value
}
This becomes:
This becomes:
run := docsRunSetting.evaluated
128
sbt 0.13.12
Improvements
Bug fixes
129
• Fixes incremental compilation relying on filename of package objects.
#2438 by [@Duhemm][@Duhemm]
• Provides a workaround flag incOptions := incOptions.value.withIncludeSynthToNameHashing(true
for name hashing not including synthetic methods. This will not be
enabled by default in sbt 0.13. It can also enabled by passing
sbt.inc.include_synth=true to JVM. #2537 by [@eed3si9n][@eed3si9n]
• Fixes tab completion for tasks defined in AutoPlugin’s buildSettings
#2460/#2469 by [@Duhemm][@Duhemm]
• Fixes configuration merging during cached resolution. #2435/#2513 by
[@Duhemm][@Duhemm]
sbt 0.13.11
Improvements
130
• sbt Serialization is updated to 0.1.2. #2117 by [@dwijnand][@dwijnand]
• Hides the stack trace on compilation error in build definition.
#2071/#2091 by [@Duhemm][@Duhemm]
• Makes the dummy Logger.Null public. #2094 by [@pdalpra][@pdalpra]
• Uses diagnostic classes to get lines contents in local Java compiler.
#2108/#2201 by [@fkorotkov][@fkorotkov]
• Adds logging of javaOptions. #2087/#2103 by [@pdalpra][@pdalpra]
• Warns when javaOptions are defined but fork is set to false. #2041/#2103
by [@pdalpra][@pdalpra]
• Adds an Append.Sequence instance for List to allow +=/++= on
developers setting. #2107/#2114 by [@pdalpra][@pdalpra]
• Drops sealed from the typeclasses in Append. #2322 by [@dwij-
nand][@dwijnand]
• Fixes compilation warnings in sbt’s codebase, and other clean ups.
#2112/#2137/#2139/#2142 by [@pdalpra][@pdalpra]
• Adds localIfFile to MavenRepository, to force artifacts to be copied
to the cache. #2172 by [@dwijnand][@dwijnand]
• Adds Resolver.bintrayIvyRepo(owner, repo). #2285 by [@dwij-
nand][@dwijnand]
• Non-static annotation changes are no longer tracked by the incremental
compiler. #2343 by [@romanowski][@romanowski]
• Reduces the memory usage of API info extraction in the incremental com-
piler. #2343 by [@adriaanm][@adriaanm]
• Memory-related options can now be overridden individually via the -J
options. sbt/sbt-launcher-package#105
Bug fixes
131
to account for the fact that Java compilers may inline constant fields in
downstream classes. #1967/#2085 by [@stuhood][@stuhood]
• Fixes classfile location detection. #2214 by [@stuhood][@stuhood]
• Fixes a few typos in keys descriptions. #2092 by [@pdalpra][@pdalpra]
• Avoids the use of ListBuffer#readOnly. #2095 by [@adriaanm][@adriaanm]
• Expands transitive dependency exclusions when using sbt-maven-resolver-
plugin #2109 by [@jsuereth][@jsuereth]
• Fixes incremental compilation of traits by including private members into
the API hash. #2155/#2160 by [@Duhemm][@Duhemm]
• Fixes name hashing by removing class private members from the hash.
#2324/#2325 by [@gkossakowski][@gkossakowski]
• Fixes name hashing error messages. #2158 by [@stuhood][@stuhood]
• Adds more robustness to tasks and settings command. #2192 by
[@DavidPerezIngeniero][@DavidPerezIngeniero]
• Fixes Java compilation inconsistencies between sbt and javac by always
failing if the local Java compiler reported errors. #2228/#2271 by
[@Duhemm][@Duhemm]
• Fixes JavaErrorParser to parse non-compile-errors #2256/#2272 by
[@Duhemm][@Duhemm]
• Fixes launcher configuration to add sbt-ivy-snapshots repository to re-
solve nightly builds. [@eed3si9n][@eed3si9n]
• Fixes performance issues during tree traversal in the incremental compiler.
#2343 by [@adriaanm][@adriaanm]
• Fixes the tracking of self types and F-bounded existential types in the
incremental compiler. #2343 by [@adriaanm][@adriaanm]
• Avoid CCE when scalac internally uses compileLate. #2453 by
[@retronym][@retronym]
• Fixes the memory-related options overriding SBT_OPTS. sbt/sbt-launcher-
package#101 by [@eed3si9n][@eed3si9n]
Dotty awareness
sbt 0.13.11 will assume that Dotty is used when scalaVersion starts with 0..
The built-in compiler bridge in sbt does not support Dotty, but a separate
132
compiler bridge is being developed at smarter/dotty-bridge and an example
project that uses it is available at smarter/dotty-example-project.
#2344 by [@smarter][@smarter]
The exportToInternal setting allows the dependee subprojects to opt out of the
internal tracking, which might be useful if you want to track most subprojects
except for a few. The intersection of the trackInternalDependencies and
exportToInternal settings will be used to determine the actual track level.
Here’s an example to opt-out one project:
#2266/#2354 by [@eed3si9n][@eed3si9n]
133
Def.settings
#2151 by [@dwijnand][@dwijnand]
sbt 0.13.10
sbt 0.13.10 did not happen due a bug that was found after the artifact was
published.
sbt 0.13.9
Improvements
134
• Adds help message for inspect actual. #1651/#1990 by [@dwij-
nand][@dwijnand]
• Supports excluding tests in testOnly/testQuick with -, for example
-MySpec. #1970 by [@matthewfarwell][@matthewfarwell]
• Adds more diagnostic info for underfined settings. #2008/#2009 by
[@DavidPerezIngeniero][@DavidPerezIngeniero]
• Adds an Extracted.runInputTask helper to assist with imperatively ex-
ecuting input tasks. #2006 by [@jroper][@jroper]
• Renames distinct method on PathFinder to distinctName. #1973 by
[@eed3si9n][@eed3si9n]
• Adds distinctPath method on PathFinder. #1973 by [@eed3si9n][@eed3si9n]
Bug fixes
135
POM files no longer include certain source and javadoc jars
sbt 0.13.9 adds retrieveManagedSync key that, when set to true, enables syn-
chronizing retrieved to the current build by removed unneeded files.
It also adds configurationsToRetrieve key, that takes values of Option[Set[Configuration]].
If set, when retrieveManaged is true only artifacts in the specified configura-
tions will be retrieved to the current build.
#1950/#1987 by [@ajsquared][@ajsquared]
On a larger dependency graph, the JSON file growing to be 100MB+ with 97%
of taken up by caller information. To make the matter worse, these large JSON
files were never cleaned up.
sbt 0.13.9 filters out artificial or duplicate callers, which fixes OutOfMemoryException
seen on some builds. This generally shrinks the size of JSON, so it should
make the IO operations faster. Dynamic graphs will be rotated with directories
named after yyyy-mm-dd, and stale JSON files will be cleaned up after few
days.
sbt 0.13.9 also fixes a correctness issue that was found in the earlier releases.
Under some circumstances, libraries that shouldn’t have been evicted was being
evicted. This occured when library A1 depended on B2, but a newer A2 dropped
the dependency, and A2 and B1 are also is in the graph. This is fixed by sorting
the graph prior to eviction.
#2030/#1721/#2014/#2046/#2097 by [@eed3si9n][@eed3si9n]
Force GC
136
Maven compatibility fix
To resolve dynamic versions such as SNAPSHOT and version ranges, the depen-
dency resolution engine queries for the list of available versions. For Maven
repositories, it was supposed read maven-metadata.xml first, but because sbt
customizes the repository layout for cross building, it has been falling back to
screen scraping of the Apache directory listing. This problem surfaced as:
sbt 0.13.9 fixes this by relaxing the Maven compatiblity check, so it will read
maven-metadata.xml. #2075 by [@eed3si9n][@eed3si9n]
sbt 0.13.8
Improvements
137
Fixes
val a = <x/><y/>
val b = 0
138
At the time, we thought adding parentheses around XML nodes could work
around this behavior. However, the workaround has caused more issues, and
since then we have realized that this is a compiler issue SI-9027, so we have
decided to roll back our workaround. In the meantime, if you have consecutive
XML elements in your build.sbt, enclose them in <xml:group> tag, or parenthe-
ses.
val a = <xml:group><x/><y/></xml:group>
val b = 0
#1765 by [@ajozwik][@ajozwik]
When crossPaths setting is set to true (it is true by default), sbt 0.13.8
will include src/main/scala-<scalaBinaryVersion>/ to the Compile
compilation in addition to src/main/scala. For example, it will include
src/main/scala-2.11/ for Scala 2.11.5, and src/main/scala-2.9.3 for Scala
2.9.3. #1799 by [@indrajitr][@indrajitr]
addMavenResolverPlugin
139
• The current implementation does not support Ivy-style dynamic revisions,
such as “2.10.+” or “latest.snapshot”. This is a fixable situation, but the
version range query and Ivy -> Maven version range translation code has
not been migrated.
• The current implementation does not support Maven-style range revisions
if found on transitive dependencies. #1921
#1793 by [@jsuereth][@jsuereth]
excludeDependencies += "org.apache.logging.log4j"
excludeDependencies += "com.example" %% "foo"
Sequential tasks
sbt 0.13.8 adds a new Def.sequential function to run tasks under semi-
sequential semantics. Here’s an example usage:
140
Normally sbt’s task engine will reorder tasks based on the dependencies among
the tasks, and run as many tasks in parallel (See Custom settings and tasks
for more details on this). Def.sequential instead tries to run the tasks in
the specified order. However, the task engine will still deduplicate tasks. For
instance, when foo is executed, it will only compile once, even though test in
Test depends on compile. #1817/#1001 by [@eed3si9n][@eed3si9n]
#1902 by [@dwijnand][@dwijnand]
Bytecode Enhancers
sbt 0.13.8 adds an extension point whereby users can effectively manipulate java
bytecode (.class files) before the incremental compiler attempts to cache the
classfile hashes. This allows libraries like ebean to function with sbt without
corrupting the compiler cache and rerunning compile every few seconds.
This splits the compile task into several subTasks:
141
2. compileIncremental: This is the core logic of compiling Scala/Java files
together. This task actually does the work of compiling a project incremen-
tally, including ensuring a minimum number of source files are compiled.
After this method, all .class files that would be generated by scalac + javac
will be available.
3. manipulateByteCode: This is a stub task which takes the compileIncremental
result and returns it. Plugins which need to manipulate bytecode are
expected to override this task with their own implementation, ensuring
to call the previous behavior.
4. compile: This task depends on manipulateBytecode and then persists
the Analysis object containing all incremental compiler information.
manipulateBytecode in Compile := {
val previous = (manipulateBytecode in Compile).value
doManipulateBytecode(previous) // Note: This must return a new Compiler.CompileResult with ou
}
sbt 0.13.7
Improvements
142
• Adds support for publishing to a custom Maven local repository. See
below. #1589/#1600 by [@topping][@topping]
• Adds circular dependency check. See below. #1601 by [@eed3si9n][@eed3si9n]
• Adds cached resolution (minigraph caching). See below. #1631 by
[@eed3si9n][@eed3si9n]
• Allows the “-bin” Scala version suffix to specify a bincompat version.
#1573 by [@cunei][@cunei]
• Adds support for publishing to file repositories specified in ~/.sbt/repositories.
#1579 by [@copumpkin][@copumpkin]
• Adds support for publishing to a Maven repository with file URLs.
#1618 by [@jsuereth][@jsuereth]
• Don’t hardcode existing relations in TextAnalysisFormat. #1572 by
[@Duhemm][@Duhemm]
• Adds developers key. #1590 by [@jedesah][@jedesah]
• Will warn when none or multiple main classes detected. #1648 by
[@kretes][@kretes]
Bug fixes
143
Natural whitespace handling
Starting sbt 0.13.7, build.sbt will be parsed using a customized Scala parser.
This eliminates the requirement to use blank line as the delimiter between each
settings, and also allows blank lines to be inserted at arbitrary position within
a block.
This feature can be disabled, if necessary, via the -Dsbt.parser.simple=true flag.
This feature was contributed by [Andrzej Jozwik (@ajozwik)](https://github.com/ajozwik),
[Rafał Krzewski (@rkrzewski)][@rkrzewski] and others at [@WarsawScala][@WarsawScala]
inspired by Typesafe’s [@gkossakowski][@gkossakowski] organizing multiple
meetups and hackathons on how to patch sbt with the focus on this blank line
issue. Dziękujemy! #1606
Circular dependency
By default circular dependencies are warned, but they do not halt the depen-
dency resolution. Using the following setting, circular dependencies can be
treated as an error.
updateOptions := updateOptions.value.withCircularDependencyLevel(CircularDependencyLevel.Erro
#1601 by [@eed3si9n][@eed3si9n]
sbt 0.13.7 adds a new experimental update option called cached resolution,
which replaces consolidated resolution:
144
updateOptions := updateOptions.value.withCachedResolution(true)
sbt 0.13.6
Improvements
145
• Enables tab completion for scripted task. #1383 by [@xuwei-k][@xuwei-k]
• Allows project reference to to a branch of a local git repository. #1409 by
[@vn971][@vn971]
• Triggered Execution is now aware of rename or move of files. #1401 by
[@xuwei-k][@xuwei-k]
• No longer updates classifiers of projectDependencies. #1366/#1367 by
[@dansanduleac][@dansanduleac]
• Selects the first test fingerprint for a test name for forked tests. #1450 by
[@pvlugter][@pvlugter]
• Allows default auto plugins to be disabled. #1451 by [@jsuereth][@jsuereth]
• Allows keys defined inside build.sbt to be used from sbt shell.
#1059/#1456
• Updates internal Ivy instance to cache the results of dependency exclusion
rules. #1476 by [@eed3si9n][@eed3si9n]
• Adds Resolver.jcenterRepo and Resolver.bintrayRepo(owner,
repo) to add Bintray easier. #1405 by [@evgeny-goldin][@evgeny-goldin]
• AutoPlugins with no requirements enabled by allRequirements can now
be disable dby the user. #1516 by [@jsuereth][@jsuereth]
Bug fixes
146
• Fixes config-classes leak in loading build files. #1524 by [@jsuereth][@jsuereth]
• Fixes name-conflicts in hashed settings class files. #1465 by
[@jsuereth][@jsuereth]
• Fixes the pom conversion of dynamic revisions like 1.1+. #1275 by
[@eed3si9n][@eed3si9n]
• Fixes NullPointerError in tab completion by FileExamples. #1530 by
[@eed3si9n][@eed3si9n]
• Fixes metabuild downloading unused Scala 2.10.2. #1439 by
[@eed3si9n][@eed3si9n]
-Dsbt.repository.secure=false
Java.net Maven 2 repository, Typesafe repository, and sbt Plugin repository also
defaults to HTTPS.
#1494 by [@rtyley][@rtyley], #1536 by [@benmccann][@benmccann], and
#1541 by [@eed3si9n][@eed3si9n].
enablePlugins/disablePlugins
sbt 0.13.6 will try to reconstruct dependencies tree when it fails to resolve a
managed dependency. This is an approximation, but it should help you figure
out where the problematic dependency is coming from. When possible sbt will
display the source position next to the modules:
[warn] ::::::::::::::::::::::::::::::::::::::::::::::
[warn] :: UNRESOLVED DEPENDENCIES ::
[warn] ::::::::::::::::::::::::::::::::::::::::::::::
[warn] :: foundrylogic.vpp#vpp;2.2.1: not found
[warn] ::::::::::::::::::::::::::::::::::::::::::::::
[warn]
[warn] Note: Unresolved dependencies path:
147
[warn] foundrylogic.vpp:vpp:2.2.1
[warn] +- org.apache.cayenne:cayenne-tools:3.0.2
[warn] +- org.apache.cayenne.plugins:maven-cayenne-plugin:3.0.2 (/foo/some-test/build.sb
[warn] +- d:d_2.10:0.1-SNAPSHOT
#528/#1422/#1447 by [@eed3si9n][@eed3si9n]
Eviction warnings
sbt 0.13.6 displays eviction warnings when it resolves your project’s managed de-
pendencies via update task. Currently the eviction warnings are categorized into
three layers: scalaVersion eviction, direct evictions, and transitive evictions.
By default eviction warning on update task will display only scalaVersion
evictin and direct evictions.
scalaVersion eviction warns you when scalaVersion is no longer effecitive.
This happens when one of your dependency depends on a newer release of scala-
library than your scalaVersion. Direct evctions are evictions related to your
direct dependencies. Warnings are displayed only when API incompatibility is
suspected. For Java libraries, Semantic Versioning is used for guessing, and for
Scala libraries Second Segment versioning (second segment bump makes API
incompatible) is used.
To display all eviction warnings with caller information, run evicted task.
#1200/#1467 by [@eed3si9n][@eed3si9n]
Latest SNAPSHOTs
sbt 0.13.6 adds a new setting key called updateOptions for customizing the
details of managed dependency resolution with update task. One of its flags is
called lastestSnapshots, which controls the behavior of the chained resolver.
Up until 0.13.6, sbt was picking the first -SNAPSHOT revision it found along the
chain. When latestSnapshots is enabled (default: true), it will look into all
resolvers on the chain, and compare them using the publish date.
The tradeoff is probably a longer resolution time if you have many remote repos-
itories on the build or you live away from the severs. So here’s how to disable
it:
148
updateOptions := updateOptions.value.withLatestSnapshots(false)
#1514 by [@eed3si9n][@eed3si9n]
Consolidated resolution
updateOptions := updateOptions.value.withConsolidatedResolution(true)
This feature is specifically targeted to address Ivy resolution is beging slow for
multi-module projects #413. Consolidated resolution aims to fix this issue by
artificially constructing an Ivy dependency graph for the unique managed de-
pendencies. If two subprojects introduce identical external dependencies, both
subprojects should consolidate to the same graph, and therefore resolve imme-
diately for the second update. #1454 by [@eed3si9n][@eed3si9n]
sbt 0.13.5
sbt 0.13.5 is a technology preview of what’s to come to sbt 1.0 with enhance-
ments like auto plugins and the necessary APIs changes and launcher for “sbt
as a server.”, defined in the sbt-remote-control project.
• The Scala version for sbt and sbt plugins is now 2.10.4. This is a compat-
ible version bump.
• Added a new setting testResultLogger to allow customisation of logging
of test results. (#1225)
• When test is run and there are no tests available, omit logging output. Es-
pecially useful for aggregate modules. test-only et al unaffected. (#1185)
• sbt now uses minor-patch version of ivy 2.3 (org.scala-sbt.ivy:ivy:2.3.0-sbt-
)
• sbt.Plugin deprecated in favor of sbt.AutoPlugin
• name-hashing incremental compiler now supports scala macros.
• testResultLogger is now configured.
• sbt-server hooks for task cancellation.
• Add JUnitXmlReportPlugin which generates junit-xml-reports for all
tests.
• Optionally enable forced garbage collection after tasks (-Dsbt.task.forcegc=true).
149
sbt 0.13.0 - 0.13.2
sbt 0.13.2
sbt 0.13.1
• The Scala version for sbt and sbt plugins is now 2.10.3. This is a compat-
ible version bump.
• New method toTask on Initialize[InputTask[T]] to apply the full
input and get a plain task out.
• Improved performance of inspect tree
• Work around various issues with Maven local repositories, including re-
solving -SNAPSHOTs from them. (#321)
• Better representation of no cross-version suffix in suffix conflict error mes-
sage: now shows <none> instead of just _
150
• TrapExit support for multiple, concurrent managed applications. Now
enabled by default for all run-like tasks. (#831)
• Add minimal support for class file formats 51.0, 52.0 in incremental com-
piler. (#842)
• Allow main class to be non-public. (#883)
• Convert -classpath to CLASSPATH when forking on Windows and length
exceeds a heuristic maximum. (#755)
• scalacOptions for .scala build definitions are now also used for .sbt
files
• error, warn, info, debug commands to set log level and --error, … to
set the level before the project is loaded. (#806)
• sLog settings that provides a Logger for use by settings. (#806)
• Early commands: any command prefixed with -- gets moved before other
commands on startup and doesn’t force sbt into batch mode.
• Deprecate internal -, --, and --- commands in favor of onFailure,
sbtClearOnFailure, and resumeFromFailure.
• makePom no longer generates <type> elements for standard classifiers.
(#728)
• Fix many instances of the Turkish i bug.
• Read https+ftp proxy environment variables into system properties where
Java will use them. (#886)
• The Process methods that are redirection-like no longer discard the exit
code of the input. This addresses an inconsistency with Fork, where using
the CustomOutput OutputStrategy makes the exit code always zero.
• Recover from failed reload command in the scripted sbt handler.
• Parse external pom.xml with CustomPomParser to handle multiple defini-
tions. (#758)
• Improve key collision error message (#877)
• Display the source position of an undefined setting.
• Respect the -nowarn option when compiling Scala sources.
• Improve forked test debugging by listing tests run by sbt in debug output.
(#868)
• Fix scaladoc cache to track changes to -doc-root-content (#837)
• Incremental compiler: Internal refactoring in preparation for name-
hashing (#936)
• Incremental compiler: improved cache loading/saving speed by internal
file names (#931)
• Docs: many contributed miscellaneous fixes and additions
• Docs: link to page source now at the bottom of the page
• Docs: sitemap now automatically generated
• Docs: custom :key: role enables links from a key name in the docs to the
val in sxr/sbt/Keys.scala
• Docs: restore sxr support and fix links to sxr’d sources. (#863)
151
sbt 0.13.0
Features
152
• Automatically link to external API scaladocs of dependencies by setting
autoAPIMappings := true. This requires at least Scala 2.10.1 and for
dependencies to define apiURL for their scaladoc location. Mappings may
be manually added to the apiMappings task as well.
• Support setting Scala home directory temporary using the switch com-
mand: ++ scala-version=/path/to/scala/home. The scala-version
part is optional, but is used as the version for any managed dependencies.
• Add publishM2 task for publishing to ~/.m2/repository. (gh-485)
• Use a default root project aggregating all projects if no root is defined.
(gh-697)
• New API for getting tasks and settings from multiple projects and config-
urations. See the new section getting values from multiple scopes.
• Enhanced test interface for better support of test framework features. (De-
tails pending.)
• export command
Fixes
Improvements
• Run the API extraction phase after the compiler’s pickler phase instead
of typer to allow compiler plugins after typer. (Adriaan M., gh-609)
• Record defining source position of settings. inspect shows the definition
location of all settings contributing to a defined value.
• Allow the root project to be specified explicitly in Build.rootProject.
• Tasks that need a directory for storing cache information can now
use the cacheDirectory method on streams. This supersedes the
cacheDirectory setting.
153
• The environment variables used when forking run and test may be set
via envVars, which is a Task[Map[String,String]]. (gh-665)
• Restore class files after an unsuccessful compilation. This is useful when an
error occurs in a later incremental step that requires a fix in the originally
changed files.
• Better auto-generated IDs for default projects. (gh-554)
• Fork run directly with ‘java’ to avoid additional class loader from ‘scala’
command. (gh-702)
• Make autoCompilerPlugins support compiler plugins defined in a internal
dependency (only if exportJars := true due to scalac limitations)
• Track ancestors of non-private templates and use this information to re-
quire fewer, smaller intermediate incremental compilation steps.
• autoCompilerPlugins now supports compiler plugins defined in a
internal dependency. The plugin project must define exportJars
:= true. Depend on the plugin with ...dependsOn(... %
Configurations.CompilerPlugin).
• Add utilities for debugging API representation extracted by the incremen-
tal compiler. (Grzegorz K., gh-677, gh-793)
• consoleProject unifies the syntax for getting the value of a setting and
executing a task. See Console Project.
Other
• The source layout for the sbt project itself follows the package name to
accommodate to Eclipse users. (Grzegorz K., gh-613)
camelCase Key names The convention for key names is now camelCase
only instead of camelCase for Scala identifiers and hyphenated, lower-case on
the command line. camelCase is accepted for existing hyphenated key names
and the hyphenated form will still be accepted on the command line for those
existing tasks and settings declared with hyphenated names. Only camelCase
will be shown for tab completion, however.
New key definition methods There are new methods that help avoid du-
plicating key names by declaring keys as:
The name will be picked up from the val identifier by the implementation of the
taskKey macro so there is no reflection needed or runtime overhead. Note that
154
a description is mandatory and the method taskKey begins with a lowercase
t. Similar methods exist for keys for settings and input tasks: settingKey and
inputKey.
New task/setting syntax First, the old syntax is still supported with the
intention of allowing conversion to the new syntax at your leisure. There may
be some incompatibilities and some may be unavoidable, but please report any
issues you have with an existing build.
The new syntax is implemented by making :=, +=, and ++= macros and making
these the only required assignment methods. To refer to the value of other
settings or tasks, use the value method on settings and tasks. This method is
a stub that is removed at compile time by the macro, which will translate the
implementation of the task/setting to the old syntax.
For example, the following declares a dependency on scala-reflect using the
value of the scalaVersion setting:
The value method is only allowed within a call to :=, +=, or ++=. To construct
a setting or task outside of these methods, use Def.task or Def.setting. For
example,
libraryDependencies += reflectDep.value
myInputTask := {
// Define the parser, which is the standard space-delimited arguments parser.
val args = Def.spaceDelimited("<args>").parsed
// Demonstrates using a setting value and a task result:
println("Project name: " + name.value)
println("Classpath: " + (fullClasspath in Compile).value.map(_.file))
println("Arguments:")
for(arg <- args) println(" " + arg)
}
155
To expect a task to fail and get the failing exception, use the failure method
instead of value. This provides an Incomplete value, which wraps the excep-
tion. To get the result of a task whether or not it succeeds, use result, which
provides a Result[T].
Dynamic settings and tasks (flatMap) have been cleaned up. Use the
Def.taskDyn and Def.settingDyn methods to define them (better name
suggestions welcome). These methods expect the result to be a task and
setting, respectively.
.sbt format enhancements vals and defs are now allowed in .sbt files. They
must follow the same rules as settings concerning blank lines, although multiple
definitions may be grouped together. For example,
val n = "widgets"
val o = "org.example"
name := n
organization := o
All definitions are compiled before settings, but it will probably be best practice
to put definitions together. Currently, the visibility of definitions is restricted
to the .sbt file it is defined in. They are not visible in consoleProject or the
set command at this time, either. Use Scala files in project/ for visibility in
all .sbt files.
vals of type Project are added to the Build so that multi-project builds can
be defined entirely in .sbt files now. For example,
Currently, it only makes sense to defines these in the root project’s .sbt files.
A shorthand for defining Projects is provided by a new macro called project.
This requires the constructed Project to be directly assigned to a val. The name
of this val is used for the project ID and base directory. The base directory can
be changed with the in method. The previous example can also be written as:
156
lazy val b = project in file("sub") settings(
version := "1.0"
)
Control over automatically added settings sbt loads settings from a few
places in addition to the settings explicitly defined by the Project.settings
field. These include plugins, global settings, and .sbt files. The new
Project.autoSettings method configures these sources: whether to include
them for the project and in what order.
Project.autoSettings accepts a sequence of values of type AddSettings. In-
stances of AddSettings are constructed from methods in the AddSettings com-
panion object. The configurable settings are per-user settings (from ~/.sbt,
for example), settings from .sbt files, and plugin settings (project-level only).
The order in which these instances are provided to autoSettings determines
the order in which they are appended to the settings explicitly provided in
Project.settings.
For .sbt files, AddSettings.defaultSbtFiles adds the settings from all
.sbt files in the project’s base directory as usual. The alternative method
AddSettings.sbtFiles accepts a sequence of Files that will be loaded
according to the standard .sbt format. Relative files are resolved against the
project’s base directory.
Plugin settings may be included on a per-Plugin basis by using the
AddSettings.plugins method and passing a Plugin => Boolean. The
settings controlled here are only the automatic per-project settings. Per-build
and global settings will always be included. Settings that plugins require to be
manually added still need to be added manually.
For example,
import AddSettings._
157
Resolving Scala dependencies Scala dependencies (like scala-library and
scala-compiler) are now resolved via the normal update task. This means:
sbt still needs access to the compiler and its dependencies in order to run
compile, console, and other Scala-based tasks. So, the Scala compiler jar and
dependencies (like scala-reflect.jar and scala-library.jar) are defined and resolved
in the scala-tool configuration (unless scalaHome is defined). By default, this
configuration and the dependencies in it are automatically added by sbt. This
occurs even when dependencies are configured in a pom.xml or ivy.xml and so
it means that the version of Scala defined for your project must be resolvable
by the resolvers configured for your project.
If you need to manually configure where sbt gets the Scala compiler and library
used for compilation, the REPL, and other Scala tasks, do one of the following:
sbt 0.12.4
158
• Pull sbtDependency version from sbtVersion to facilitate cross-building
plugins.
• Proper support for stashing on-failure handlers. (gh-732)
• Include files with zip extension in unmanaged jars. (gh-750)
• Only add automatically detected plugins to options once. (gh-757)
• Properly handle failure in a multi-command that includes reload. (gh-
732)
• Fix unsynchronized caching of Scala class loaders that could result in Scala
classes being loaded in multiple class loaders.
• Incremental compiler: remove resident compiler code (wasn’t used and
was a compatibility liability)
• Incremental compiler: properly track abstract override modifier. (gh-
726)
• Incremental compiler: do not normalize types in the API extraction phase.
(gh-736)
• Ivy cache: account for localOnly when cache subclass overrides
isChanging
• Ivy cache: fix corruption when developing sbt or sbt plugins. (gh-768)
• Ivy cache: invalidate when artifact download fails to avoid locking into
bad resolver. (gh-760)
• Ivy cache: use publication date from metadata instead of original file’s
last modified time when deleting out of date artifacts. (gh-764)
sbt 0.12.3
159
configurations work.
• Include jars from java.ext.dirs in incremental classpath. (gh-678)
• Multi-line prompt text offset issue (Jibbers42, gh-625)
• Added xml:space="preserve" attribute to extraDependencyAttributes
XML Block for publishing poms for plugins dependent on other plugins
(Brendan M., gh-645)
• Tag the actual test task and not a later task. (gh-692)
• Make exclude-classifiers per-user instead of per-build. (gh-634)
• Load global plugins in their own class loader and replace the base loader
with that. (gh-272)
• Demote the default conflict warnings to the debug level. These will be
removed completely in 0.13. (gh-709)
• Fix Ivy cache issues when multiple resolvers are involved. (gh-704)
sbt 0.12.2
160
• API extraction: handle any type that is annotated, not just the spec’d
simple type. (gh-559)
• Don’t try to look up the class file for a package. (gh-620)
sbt 0.12.1
• Merge multiple dependency definitions for the same ID. Workaround for
gh-468, gh-285, gh-419, gh-480.
• Don’t write section of pom if scope is ‘compile’.
• Ability to properly match on artifact type. Fixes gh-507 (Thomas).
• Force update to run on changes to last modified time of artifacts or cached
descriptor (part of fix for gh-532). It may also fix issues when working
with multiple local projects via ‘publish-local’ and binary dependencies.
• Per-project resolution cache that deletes cached files before update. Notes:
• The resolution cache differs from the repository cache and does
not contain dependency metadata or artifacts.
• The resolution cache contains the generated ivy files, properties,
and resolve reports for the project.
• There will no longer be individual files directly in ~/.ivy2/cache/
• Resolve reports are now in target/resolution-cache/reports/,
viewable with a browser.
• Cache location includes extra attributes so that cross builds of
a plugin do not overwrite each other. Fixes gh-532.
• As before, the first step recompiles sources that were edited (or otherwise
directly invalidated).
• The second step recompiles sources from the first step whose API has
changed, their direct dependencies, and sources forming a cycle with these
sources.
• The third step recompiles transitive dependencies of sources from the sec-
ond step whose API changed.
• Code relying mainly on composition should see decreased compilation
times with this approach.
• Code with deep inheritance hierarchies and large cycles between sources
may take longer to compile.
• last compile will show cycles that were processed in step 2. Reducing
large cycles of sources shown here may decrease compile times.
161
Miscellaneous fixes and improvements:
sbt 0.12.0
• The cross versioning convention has changed for Scala versions 2.10 and
later as well as for sbt plugins.
• When invoked directly, ‘update’ will always perform an update (gh-335)
• The sbt plugins repository is added by default for plugins and plugin
definitions. gh-380
• Plugin configuration directory precedence has changed (see details section
below)
• Source dependencies have been fixed, but the fix required changes (see
details section below)
• Aggregation has changed to be more flexible (see details section below)
• Task axis syntax has changed from key(for task) to task::key (see details
section below)
• The organization for sbt has to changed to org.scala-sbt (was: org.scala-
tools.sbt). This affects users of the scripted plugin in particular.
162
• artifactName type has changed to (ScalaVersion, Artifact,
ModuleID) => String
• javacOptions is now a task
• session save overwrites settings in build.sbt (when appropriate). gh-
369
• scala-library.jar is now required to be on the classpath in order to compile
Scala code. See the scala-library.jar section at the bottom of the page for
details.
Features
Fixes
• Delete a symlink and not its contents when recursively deleting a directory.
• Fix detection of ancestors for java sources
• Fix the resolvers used for update-sbt-classifiers (gh-304)
• Fix auto-imports of plugins (gh-412)
• Argument quoting (see details section below)
• Properly reset JLine after being stopped by Ctrl+z (unix only). gh-394
Improvements
• The launcher can launch all released sbt versions back to 0.7.0.
• A more refined hint to run ‘last’ is given when a stack trace is suppressed.
• Use java 7 Redirect.INHERIT to inherit input stream of subprocess (gh-
462,gh-327). This should fix issues when forking interactive programs.
(@vigdorchik)
• Mirror ivy ‘force’ attribute (gh-361)
• Various improvements to help and tasks commands as well as new set-
tings command (gh-315)
• Bump jsch version to 0.1.46. (gh-403)
163
• Improved help commands: help, tasks, settings.
• Bump to JLine 1.0 (see details section below)
• Global repository setting (see details section below)
• Other fixes/improvements: gh-368, gh-377, gh-378, gh-386, gh-387, gh-
388, gh-389
Experimental or In-progress
1. Ideally, a project should ensure there is never a conflict. Both styles are
still supported; only the behavior when there is a conflict has changed.
2. In practice, switching from an older branch of a project to a new branch
would often leave an empty project/plugins/ directory that would cause
the old style to be used, despite there being no configuration there.
3. Therefore, the intention is that this change is strictly an improvement for
projects transitioning to the new style and isn’t noticed by other projects.
Parsing task axis There is an important change related to parsing the task
axis for settings and tasks that fixes gh-202
164
Aggregation Aggregation has been made more flexible. This is along the
direction that has been previously discussed on the mailing list.
1. Before 0.12, a setting was parsed according to the current project and only
the exact setting parsed was aggregated.
2. Also, tab completion did not account for aggregation.
3. This meant that if the setting/task didn’t exist on the current project,
parsing failed even if an aggregated project contained the setting/task.
4. Additionally, if compile:package existed for the current project, *:package
existed for an aggregated project, and the user requested ‘package’ to run
(without specifying the configuration), *:package wouldn’t be run on the
aggregated project (because it isn’t the same as the compile:package key
that existed on the current project).
5. In 0.12, both of these situations result in the aggregated settings being
selected. For example,
1. Consider a project root that aggregates a subproject sub.
2. root defines *:package.
3. sub defines compile:package and compile:compile.
4. Running root/package will run root/*:package and sub/compile:package
5. Running root/compile will run sub/compile:compile
6. This change was made possible in part by the change to task axis parsing.
1. The default behavior should be the same as before, including the paral-
lelExecution settings.
2. The new capabilities of the system should otherwise be considered exper-
imental.
3. Therefore, parallelExecution won’t be deprecated at this time.
1. The version of a plugin is fixed by the first build to load it. In particular,
the plugin version used in the root build (the one in which sbt is started
in) always overrides the version used in dependencies.
2. Plugins from all builds are loaded in the same class loader.
Additionally, Sanjin’s patches to add support for hg and svn URIs are included.
165
1. sbt uses Subversion to retrieve URIs beginning with svn or svn+ssh. An
optional fragment identifies a specific revision to checkout.
2. Because a URI for Mercurial doesn’t have a Mercurial-specific scheme, sbt
requires the URI to be prefixed with hg: to identify it as a Mercurial
repository.
3. Also, URIs that end with .git are now handled properly.
Cross building The cross version suffix is shortened to only include the major
and minor version for Scala versions starting with the 2.10 series and for sbt
versions starting with the 0.12 series. For example, sbinary_2.10 for a normal
library or sbt-plugin_2.10_0.12 for an sbt plugin. This requires forward and
backward binary compatibility across incremental releases for both Scala and
sbt.
1. This change has been a long time coming, but it requires everyone publish-
ing an open source project to switch to 0.12 to publish for 2.10 or adjust
the cross versioned prefix in their builds appropriately.
2. Obviously, using 0.12 to publish a library for 2.10 requires 0.12.0 to be
released before projects publish for 2.10.
3. There is now the concept of a binary version. This is a subset of the full
version string that represents binary compatibility. That is, equal binary
versions implies binary compatibility. All Scala versions prior to 2.10 use
the full version for the binary version to reflect previous sbt behavior. For
2.10 and later, the binary version is <major>.<minor>.
4. The cross version behavior for published artifacts is configured by the
crossVersion setting. It can be configured for dependencies by using the
cross method on ModuleID or by the traditional %% dependency con-
struction variant. By default, a dependency has cross versioning disabled
when constructed with a single % and uses the binary Scala version when
constructed with %%.
5. The artifactName function now accepts a type ScalaVersion as its first
argument instead of a String. The full type is now (ScalaVersion, Mod-
uleID, Artifact) => String. ScalaVersion contains both the full Scala
version (such as 2.10.0) as well as the binary Scala version (such as 2.10).
6. The flexible version mapping added by Indrajit has been merged into the
cross method and the %% variants accepting more than one argument
have been deprecated. See Cross Build for details.
166
test-quick test-quick (gh-393) runs the tests specified as arguments (or all
tests if no arguments are given) that:
scala-library.jar
Older Changes
0.11.3 to 0.12.0
The changes for 0.12.0 are listed on a separate page. See sbt 0.12.0 changes.
0.11.2 to 0.11.3
Dropping scala-tools.org:
167
• The sbt group ID is changed to org.scala-sbt (from org.scala-tools.sbt).
This means you must use a 0.11.3 launcher to launch 0.11.3.
• The convenience objects ScalaToolsReleases and ScalaToolsSnapshots
now point to https://oss.sonatype.org/content/repositories/releases and
…/snapshots
• The launcher no longer includes scala-tools.org repositories by default
and instead uses the Sonatype OSS snapshots repository for Scala snap-
shots.
• The scala-tools.org releases repository is no longer included as an appli-
cation repository by default. The Sonatype OSS repository is not included
by default in its place.
Other fixes:
0.11.1 to 0.11.2
• The local Maven repository has been removed from the launcher’s list of de-
fault repositories, which is used for obtaining sbt and Scala dependencies.
This is motivated by the high probability that including this repository
was causing the various problems some users have with the launcher not
finding some dependencies (gh-217).
Fixes:
168
• Fix forking git on windows (Stefan, Josh)
• gh-261 Fix whitespace handling for semicolon-separated commands
• gh-263 Fix handling of dependencies with an explicit URL
• gh-272 Show deprecation message for project/plugins/
0.11.0 to 0.11.1
Breaking change:
• The scripted plugin is now in the sbt package so that it can be used from
a named package
• By default, there is more logging during update: one line per dependency
resolved and two lines per dependency downloaded. This is to address the
appearance that sbt hangs on larger ’update’s.
169
• gh-222 Generate section in make-pom. (Jan)
• Build resolvers, loaders, and transformers.
• Allow project dependencies to be modified by a setting (buildDependen-
cies) but with the restriction that new builds cannot be introduced.
• gh-174, gh-196, gh-201, gh-204, gh-207, gh-208, gh-226, gh-224, gh-253
0.10.1 to 0.11.0
Major Improvements:
• Display all undefined settings at once, instead of only the first one
• Deprecate separate classpathFilter, defaultExcludes, and source-
Filter keys in favor of includeFilter and excludeFilter explicitly scoped
by unmanagedSources, unmanagedResources, or unmanagedJars as
appropriate (Indrajit)
• Default to using shared boot directory in ~/.sbt/boot/
• Can put contents of project/plugins/ directly in project/ instead. Will
likely deprecate plugins/ directory
• Key display is context sensitive. For example, in a single project, the build
and project axes will not be displayed
• gh-114, gh-118, gh-121, gh-132, gh-135, gh-157: Various settings and error
message improvements
• gh-115: Support configuring checksums separately for publish and
update
• gh-118: Add about command
• gh-118, gh-131: Improve last command. Aggregate last <task> and
display all recent output for last
170
• gh-120: Support read-only external file projects (Fred)
• gh-128: Add skip setting to override recompilation change detection
• gh-139: Improvements to pom generation (Indrajit)
• gh-140, gh-145: Add standard manifest attributes to binary and source
jars (Indrajit)
• Allow sources used for doc generation to be different from sources for
compile
• gh-156: Made package an alias for package-bin
• gh-162: handling of optional dependencies in pom generation
0.10.0 to 0.10.1
0.7.7 to 0.10.0
171
• New task engine: Tasks
• New multiple project support: .scala build defnition
• More aggressive incremental recompilation for both Java and Scala sources
• Merged plugins and processors into improved plugins system: Plugins
• Web application and webstart support moved to plugins instead of core
features
• Fixed all of the issues in (Google Code) issue #44
• Managed dependencies automatically updated when configuration changes
• update-sbt-classifiers and update-classifiers tasks for retrieving
sources and/or javadocs for dependencies, transitively
• Improved [artifact handling and configuration][Attifacts]
• Tab completion parser combinators for commands and input tasks: Com-
mands
• No project creation prompts anymore
• Moved to GitHub: https://github.com/harrah/xsbt
0.7.5 to 0.7.7
0.7.4 to 0.7.5
172
0.7.3 to 0.7.4
• prefix continuous compilation with run number for better feedback when
logging level is ‘warn’
• Added pomIncludeRepository(repo: MavenRepository): Boolean
that can be overridden to exclude local repositories by default
• Added pomPostProcess(pom: Node): Node to make advanced manipu-
lation of the default pom easier (pomExtra already covers basic cases)
• Added reset command to reset JLine terminal. This needs to be run
after suspending and then resuming sbt.
• Installer plugin is now a proper subproject of sbt.
• Plugins can now only be Scala sources. BND should be usable in a plugin
now.
• More accurate detection of invalid test names. Invalid test names now
generate an error and prevent the test action from running instead of just
logging a warning.
• Fix issue with using 2.8.0.RC1 compiler in tests.
• Precompile compiler interface against 2.8.0.RC2
• Add consoleOptions for specifying options to the console. It defaults to
compileOptions.
• Properly support sftp/ssh repositories using key-based authentication. See
the updated section of the Resolvers page.
• def ivyUpdateLogging = UpdateLogging.DownloadOnly | Full |
Quiet. Default is DownloadOnly. Full will log metadata resolution and
provide a final summary.
• offline property for disabling checking for newer dynamic revisions (like
-SNAPSHOT). This allows working offline with remote snapshots. Not
honored for plugins yet.
• History commands: !!, !?string, !-n, !n, !string, !:n, !: Run
! to see help.
• New section in launcher configuration [ivy] with a single label cache-
directory. Specify this to change the cache location used by the launcher.
• New label classifiers under [app] to specify classifiers of additional
artifacts to retrieve for the application.
• Honor -Xfatal-warnings option added to compiler in 2.8.0.RC2.
• Make scaladocTask a fileTask so that it runs only when index.html is
older than some input source.
• Made it easier to create default test-* tasks with different options
• Sort input source files for consistency, addressing scalac’s issues with
source file ordering.
• Derive Java source file from name of class file when no SourceFile attribute
is present in the class file. Improves tracking when -g:none option is used.
• Fix FileUtilities.unzip to be tail-recursive again.
173
0.7.2 to 0.7.3
0.7.1 to 0.7.2
174
and will not be packaged by package. For example, to exclude the GAE
datastore directory:
0.7.0 to 0.7.1
0.5.6 to 0.7.0
• Unifed batch and interactive commands. All commands that can be exe-
cuted at interactive prompt can be run from the command line. To run
commands and then enter interactive prompt, make the last command
‘shell’.
• Properly track certain types of synthetic classes, such as for comprehension
with >30 clauses, during compilation.
• Jetty 7 support
• Allow launcher in the project root directory or the lib directory. The jar
name must have the form ‘sbt-launch.jar’ in order to be excluded from the
classpath.
• Stack trace detail can be controlled with 'on', 'off', ‘nosbt’, or an integer
level. ‘nosbt’ means to show stack frames up to the first sbt method. An
integer level denotes the number of frames to show for each cause. This
feature is courtesy of Tony Sloane.
175
• New action ‘test-run’ method that is analogous to ‘run’, but for test
classes.
• New action ‘clean-plugins’ task that clears built plugins (useful for plugin
development).
• Can provide commands from a file with new command: <filename
• Can provide commands over loopback interface with new command: <port
• Scala version handling has been completely redone.
• The version of Scala used to run sbt (currently 2.7.7) is decoupled from
the version used to build the project.
• Changing between Scala versions on the fly is done with the command:
++<version>
• Cross-building is quicker. The project definition does not need to be re-
compiled against each version in the cross-build anymore.
• Scala versions are specified in a space-delimited list in the build.scala.versions
property.
• Dependency management:
• make-pom task now uses custom pom generation code instead of Ivy’s pom
writer.
• Basic support for writing out Maven-style repositories to the pom
• Override the ‘pomExtra’ method to provide XML (scala.xml.NodeSeq)
to insert directly into the generated pom.
• Complete control over repositories is now possible by overriding ivyRepos-
itories.
• The interface to Ivy can be used directly.
• Test framework support is now done through a uniform test interface.
Implications:
• New versions of specs, ScalaCheck, and ScalaTest are supported as soon
as they are released.
• Support is better, since the test framework authors provide the implemen-
tation.
• Arguments can be passed to the test framework. For example: {{{ >
test-only your.test – -a -b -c }}}
• Can provide custom task start and end delimiters by defining the system
properties sbt.start.delimiter and sbt.end.delimiter.
• Revamped launcher that can launch Scala applications, not just sbt
• Provide a configuration file to the launcher and it can download the ap-
plication and its dependencies from a repository and run it.
• sbt’s configuration can be customized. For example,
• The sbt version to use in projects can be fixed, instead of read from
project/build.properties.
• The default values used to create a new project can be changed.
• The repositories used to fetch sbt and its dependencies, including Scala,
can be configured.
176
• The location sbt is retrieved to is configurable. For example,
/home/user/.ivy2/sbt/ could be used instead of project/boot/.
0.5.5 to 0.5.6
0.5.4 to 0.5.5
0.5.2 to 0.5.4
• Many logging related changes and fixes. Added FilterLogger and cleaned
up interaction between Logger, scripted testing, and the builder projects.
This included removing the recordingDepth hack from Logger. Logger
buffering is now enabled/disabled per thread.
177
• Allow multiple instances of Jetty (new jettyRunTasks can be defined with
different ports)
• jettyRunTask accepts configuration in a single configuration wrapper ob-
ject instead of many parameters
• Fix web application class loading (issue #35) by using jettyClasspath=testClasspath—
jettyRunClasspath for loading Jetty. A better way would be to have a
jetty configuration and have jettyClasspath=managedClasspath(‘jetty’),
but this maintains compatibility.
• Copy resources to target/resources and target/test-resources us-
ing copyResources and copyTestResources tasks. Properly include all re-
sources in web applications and classpaths (issue #36). mainResources
and testResources are now the definitive methods for getting resources.
• Updated for 2.8 (sbt now compiles against September 11, 2009 nightly
build of Scala)
• Fixed issue with position of ˆ in compile errors
• Changed order of repositories (local, shared, Maven Central, user, Scala
Tools)
• Added Maven Central to resolvers used to find Scala library/compiler in
launcher
• Fixed problem that prevented detecting user-specified subclasses
• Fixed exit code returned when exception thrown in main thread for
TrapExit
• Added javap task to DefaultProject. It has tab completion on compiled
project classes and the run classpath is passed to javap so that library
classes are available. Examples: :
• Added exec task. Mixin Exec to project definition to use. This forks the
command following exec. Examples: :
• Added sh task for users with a unix-style shell available (runs /bin/sh
-c <arguments>). Mixin Exec to project definition to use. Example: :
178
• Proper dependency graph actions (previously was an unsupported proto-
type): graph-src and graph-pkg for source dependency graph and quasi-
package dependency graph (based on source directories and source depen-
dencies)
To specify in a dependency: :
0.5.1 to 0.5.2
• Fixed problem where dependencies of sbt plugins were not on the compile
classpath
• Added execTask that runs an sbt.ProcessBuilder when invoked
179
• Can define and use an sbt test framework extension in a project
• Fixed run action swallowing exceptions
• Fixed tab completion for method tasks for multi-project builds
• Check that tasks in compoundTask do not reference static tasks
• Make toString of Paths in subprojects relative to root project directory
• crossScalaVersions is now inherited from parent if not specified
• Added scala-library.jar to the javac classpath
• Project dependencies are added to published ivy.xml
• Added dependency tracking for Java sources using classfile parsing (with
the usual limitations)
• Added Process.cat that will send contents of URLs and Files to standard
output. Alternatively, cat can be used on a single URL or File. Example:
:
import java.net.URL
import java.io.File
val spde = new URL("http://technically.us/spde/About")
val dispatch = new URL("http://databinder.net/dispatch/About")
val build = new File("project/build.properties")
cat(spde, dispatch, build) #| "grep -i scala" !
0.4.6 to 0.5/0.5.1
180
• Dependency management and multiple Scala versions
• Experimental plugin for producing project bootstrapper in a self-
extracting jar
• Added ability to directly specify URL to use for dependency with the
from(url: URL) method defined on ModuleID
• Fixed issue #30
• Support cross-building with + when running batch actions
• Additional flattening for project definitions: sources can go either in
project/build/src (recursively) or project/build (flat)
• Fixed manual reboot not changing the version of Scala when it is manually
set
• Fixed tab completion for cross-building
• Fixed a class loading issue with web applications
0.4.5 to 0.4.6
181
• Parallelization at the subtask level
• Parallel test execution at the suite/specification level.
0.4.3 to 0.4.5
182
0.4 to 0.4.3
• Direct dependencies on Scala libraries are checked for version equality with
scala.version
• Transitive dependencies on scala-library and scala-compiler are fil-
tered
• They are fixed by scala.version and provided on the classpath by sbt
• To access them, use the scalaJars method, classOf[ScalaObject].getProtectionDomain.getCodeSource,
or mainCompileConditional.analysis.allExternals
• The configurations checked/filtered as described above are configurable.
Nonstandard configurations are not checked by default.
• Version of sbt and Scala printed on startup
• Launcher asks if you want to try a different version if sbt or Scala could
not be retrieved.
• After changing scala.version or sbt.version with set, note is printed
that reboot is required.
• Moved managed dependency actions to BasicManagedProject (update is
now available on ParentProject)
• Cleaned up sbt’s build so that you just need to do update and full-build
to build from source. The trunk version of sbt will be available for use
from the loader.
• The loader is now a subproject.
• For development, you’ll still want the usual actions (such as package) for
the main builder and proguard to build the loader.
• Fixed analysis plugin improperly including traits/abstract classes in sub-
class search
• ScalaProjects already had everything required to be parent projects:
flipped the switch to enable it
• Proper method task support in scripted tests (package group tests rightly
pass again)
• Improved tests in loader that check that all necessary libraries were down-
loaded properly
0.3.7 to 0.4
183
• Scripted tests now test the version of sbt being built instead of the version
doing the building.
• testResources is put on the test classpath instead of testResourcesPath
• Added jetty-restart, which does jetty-stop and then jetty-run
• Added automatic reloading of default web application
• Changed packaging behaviors (still likely to change)
• Inline configurations now allowed (can be used with configurations in inline
XML)
• Split out some code related to managed dependencies from BasicScalaPro-
ject to new class BasicManagedProject
• Can specify that maven-like configurations should be automatically de-
clared
• Fixed problem with nested modules being detected as tests
• testResources, integrationTestResources, and mainResources should
now be added to appropriate classpaths
• Added project organization as a property that defaults to inheriting from
the parent project.
• Project creation now prompts for the organization.
• Added method tasks, which are top-level actions with parameters.
• Made help, actions, and methods commands available to batch-style
invocation.
• Applied Mikko’s two fixes for webstart and fixed problem with
pack200+sign. Also, fixed nonstandard behavior when gzip enabled.
• Added control method to Logger for action lifecycle logging
• Made standard logging level convenience methods final
• Made BufferedLogger have a per-actor buffer instead of a global buffer
• Added a SynchronizedLogger and a MultiLogger (intended to be used
with the yet unwritten FileLogger)
• Changed method of atomic logging to be a method logAll accepting
List[LogEvent] instead of doSynchronized
• Improved action lifecycle logging
• Parallel logging now provides immediate feedback about starting an action
• General cleanup, including removing unused classes and methods and re-
ducing dependencies between classes
• run is now a method task that accepts options to pass to the main method
(runOptions has been removed, runTask is no longer interactive, and run
no longer starts a console if mainClass is undefined)
• Major task execution changes:
• Tasks automatically have implicit dependencies on tasks with the same
name in dependent projects
• Implicit dependencies on interactive tasks are ignored, explicit dependen-
cies produce an error
• Interactive tasks must be executed directly on the project on which they
184
are defined
• Method tasks accept input arguments (Array[String]) and dynamically
create the task to run
• Tasks can depend on tasks in other projects
• Tasks are run in parallel breadth-first style
• Added test-only method task, which restricts the tests to run to only
those passed as arguments.
• Added test-failed method task, which restricts the tests to run. First,
only tests passed as arguments are run. If no tests are passed, no filtering
is done. Then, only tests that failed the previous run are run.
• Added test-quick method task, which restricts the tests to run. First,
only tests passed as arguments are run. If no tests are passed, no filtering
is done. Then, only tests that failed the previous run or had a dependency
change are run.
• Added launcher that allows declaring version of sbt/scala to build project
with.
• Added tab completion with ~
• Added basic tab completion for method tasks, including test-*
• Changed default pack options to be the default options of Pack200.Packer
• Fixed ~ behavior when action doesn’t exist
0.3.6 to 0.3.7
185
0.3.5 to 0.3.6
0.3.2 to 0.3.5
186
• Added help action to tab completion
• Added handling of (effectively empty) scala source files that create no class
files: they are always interpreted as modified.
• Added prompt to retry project loading if compilation fails
• package action now uses fileTask so that it only executes if files are out
of date
• fixed ScalaTest framework wrapper so that it fails the test action if tests
fail
• Inline dependencies can now specify configurations
0.3.1 to 0.3.2
0.3 to 0.3.1
0.2.3 to 0.3
0.2.2 to 0.2.3
187
0.2.1 to 0.2.2
0.2.0 to 0.2.1
0.1.9 to 0.2.0
0.1.8 to 0.1.9
188
• Split compilation into separate main and test compilations.
• A failure in a ScalaTest run now fails the test action.
• Implemented reporters for compile/scaladoc, ScalaTest, ScalaCheck,
and specs that delegate to the appropriate sbt.Logger.
0.1.7 to 0.1.8
0.1.6 to 0.1.7
• Added graph action to generate dot files (for graphiz) from dependency
information (work in progress).
• Options are now passed to tasks as varargs.
• Redesigned Path properly, including PathFinder returning a Set[Path]
now instead of Iterable[Path].
• Moved paths out of ScalaProject and into BasicProjectPaths to keep
path definitions separate from task definitions.
• Added initial support for managing third-party libraries through the up-
date task, which must be explicitly called (it is not a dependency of com-
pile or any other task). This is experimental, undocumented, and known
to be incomplete.
• Parallel execution implementation at the project level, disabled by default.
To enable, add: scala override def parallelExecution = true to your project
definition. In order for logging to make sense, all project logging is buffered
until the project is finished executing. Still to be done is some sort of
notification of project execution (which ones are currently executing, how
many remain)
• run and console are now specified as “interactive” actions, which means
they are only executed on the project in which they are defined when
called directly, and not on all dependencies. Their dependencies are still
run on dependent projects.
• Generalized conditional tasks a bit. Of note is that analysis is no longer
required to be in metadata/analysis, but is now in target/analysis by
default.
• Message now displayed when project definition is recompiled on startup
• Project no longer inherits from Logger, but now has a log member.
• Dependencies passed to project are checked for null (may help with errors
related to initialization/circular dependencies)
189
• Task dependencies are checked for null
• Projects in a multi-project configuration are checked to ensure that output
paths are different (check can be disabled)
• Made update task globally synchronized because Ivy is not thread-safe.
• Generalized test framework, directly invoking frameworks now (used re-
flection before).
• Moved license files to licenses/
• Added support for specs and some support for ScalaTest (the test action
doesn’t fail if ScalaTest tests fail).
• Added specs, ScalaCheck, ScalaTest jars to lib/
• These are now required for compilation, but are optional at runtime.
• Added the appropriate licenses and notices.
• Options for update action are now taken from updateOptions member.
• Fixed SbtManager inline dependency manager to work properly.
• Improved Ivy configuration handling (not compiled with test dependencies
yet though).
• Added case class implementation of SbtManager called SimpleManager.
• Project definitions not specifying dependencies can now use just a single
argument constructor.
0.1.5 to 0.1.6
• run and console handle System.exit and multiple threads in user code
under certain circumstances (see running project code).
0.1.4 to 0.1.5
190
• Changes in a project propagate the right source recompilations in depen-
dent projects
• Consequences:
• Recompilation when changing java/scala version
• Recompilation when upgrading libraries (again, as indicated in the second
point, situations where you have library-1.0.jar and library-2.0.jar on the
classpath at the same time are not handled predictably. Replacing library-
1.0.jar with library-2.0.jar should work as expected.)
• Changing sbt version will recompile project definitions
0.1.3 to 0.1.4
0.1.2 to 0.1.3
0.1.1 to 0.1.2
0.1 to 0.1.1
The assumption here is that you are familiar with sbt 0.7 but new to sbt 0.13.16.
sbt 0.13.16’s many new capabilities can be a bit overwhelming, but this page
should help you migrate to 0.13.16 with a minimum of fuss.
191
Why move to 0.13.16?
Step 1: Read the Getting Started Guide for sbt 0.13.16 Reading the
Getting Started Guide will probably save you a lot of confusion.
Step 2: Install sbt 0.13.16 Download sbt 0.13.16 as described on the setup
page.
You can run 0.13.16 the same way that you run 0.7.x, either simply:
Or (as most users do) with a shell script, as described on the setup page.
For more details see the setup page.
Rename your project/ directory to something like project-old. This will hide
it from sbt 0.13.16 but keep it in case you want to switch back to 0.7.x.
Create a build.sbt file in the root directory of your project. See .sbt build
definition in the Getting Started Guide, and for simple examples. If you have a
simple project then converting your existing project file to this format is largely
a matter of re-writing your dependencies and maven archive declarations in a
modified yet familiar syntax.
192
This build.sbt file combines aspects of the old project/build/ProjectName.scala
and build.properties files. It looks like a property file, yet contains Scala
code in a special format.
A build.properties file like:
#Project properties
#Fri Jan 07 15:34:00 GMT 2011
project.organization=org.myproject
project.name=My Project
sbt.version=0.7.7
project.version=1.0
def.scala.version=2.7.7
build.scala.versions=2.8.1
project.initialize=false
version := "1.0"
organization := "org.myproject"
scalaVersion := "2.9.2"
Now launch sbt. If you’re lucky it works and you’re done. For help debugging,
see below.
If you get stuck and want to switch back, you can leave your build.sbt file
alone. sbt 0.7.x will not understand or notice it. Just rename your 0.13.16
project directory to something like project10 and rename the backup of your
old project from project-old to project again.
FAQs There’s a section in the FAQ about migration from 0.7 that covers
several other important points.
193
Detailed Topics
This part of the documentation has pages documenting particular sbt topics in
detail. Before reading anything in here, you will need the information in the
Getting Started Guide as a foundation.
Other resources include the How to and Developer’s Guide sections in this ref-
erence, and the API Documentation
Using sbt
This part of the documentation has pages documenting particular sbt topics in
detail. Before reading anything in here, you will need the information in the
Getting Started Guide as a foundation.
Project-level tasks
194
• publish Publishes artifacts (such as jars) to the repository defined by the
publishTo setting, described in Publishing.
• update Resolves and retrieves external dependencies as described in li-
brary dependencies.
Configuration-level tasks
195
threading (including GUIs) in code run by this action. test:run runs a
main class in the test code.
• runMain <main-class> <argument>* Runs the specified main class for
the project in the same virtual machine as sbt. The main class is passed
the arguments provided. Please see Running Project Code for details on
the use of System.exit and multithreading (including GUIs) in code run
by this action. test:runMain runs the specified main class in the test
code.
• test Runs all tests detected during test compilation. See Testing for
details.
• testOnly <test>* Runs the tests provided as arguments. * (will be)
interpreted as a wildcard in the test name. See Testing for details.
• testQuick <test>* Runs the tests specified as arguments (or all tests if
no arguments are given) that:
1. have not been run yet OR
2. failed the last time they were run OR
3. had any transitive dependencies recompiled since the last successful
run * (will be) interpreted as a wildcard in the test name. See Testing
for details.
General commands
196
• + <command> Executes the project specified action or method for all ver-
sions of Scala defined in the crossScalaVersions setting.
197
Command Line Options
198
(jar files or directories) that are added to sbt’s classpath. Note that the entries
are deliminted by comma, e.g.: entry1, entry2,... See also resource in the sbt
launcher documentation.
sbt.version
Version
0.13.16
sbt version to use, usually taken from project/build.properties.
sbt.boot.properties
File
The path to find the sbt boot properties file. This can be a relative path, relative
to the sbt base directory, the users home directory or the location of the sbt jar
file, or it can be an absolute path or an absolute file URI.
sbt.override.build.repos
Boolean
false
If true, repositories configured in a build definition are ignored and the reposito-
ries configured for the launcher are used instead. See sbt.repository.config and
the sbt launcher documentation.
sbt.repository.config
File
~/.sbt/repositories
A file containing the repositories to use for the launcher. The format is the same
as a [repositories] section for a sbt launcher configuration file. This setting is
typically used in conjunction with setting sbt.override.build.repos to true (see
previous row and the sbt launcher documentation).
Console Project
Description
The consoleProject task starts the Scala interpreter with access to your project
definition and to sbt. Specifically, the interpreter is started up with these
commands already executed:
import sbt._
import Process._
import Keys._
199
import <your-project-definition>._
import currentState._
import extracted._
import cpHelpers._
For example, running external processes with sbt’s process library (to be in-
cluded in the standard library in Scala 2.9):
consoleProject can be useful for creating and modifying your build in the
same way that the Scala interpreter is normally used to explore writing code.
Note that this gives you raw access to your build. Think about what you pass
to IO.delete, for example.
Accessing settings
Examples
Evaluating tasks
200
Examples Show all repositories, including defaults.
State
> remainingCommands
> definedCommands.size
Cross-building
Introduction
Publishing Conventions
201
Using Cross-Built Libraries
To use a library built against multiple versions of Scala, double the first % in an
inline dependency to be %%. This tells sbt that it should append the current
version of Scala being used to build the library to the dependency’s name. For
example:
Cross-Building a Project
> + package
> ++ 2.12.2
[info] Setting version to 2.12.2
> ++ 2.11.11
[info] Setting version to 2.11.11
> compile
202
<version> should be either a version for Scala published to a repository or the
path to a Scala home directory, as in ++ /path/to/scala/home. See Command
Line Reference for details.
The ultimate purpose of + is to cross-publish your project. That is, by doing:
> + publish
you make your project available to users for different versions of Scala. See
Publishing for more details on publishing your project.
In order to make this process as quick as possible, different output and managed
dependency directories are used for different versions of Scala. For example,
when building against Scala 2.10.0,
This overrides the defaults to always use the full Scala version instead of the
binary Scala version:
203
"a" % "b" % "1.0" cross CrossVersion.patch
This uses a custom function to determine the Scala version to use based on the
binary Scala version:
This uses a custom function to determine the Scala version to use based on the
full Scala version:
{<build-uri>}<project-id>/config:intask::key
This “scoped key” reference is used by commands like last and inspect and
when selecting a task to run. Only key is usually required by the parser; the
remaining optional pieces select the scope. These optional pieces are individ-
ually referred to as scope axes. In the above description, {<build-uri>} and
<project-id>/ specify the project axis, config: is the configuration axis, and
intask is the task-specific axis. Unspecified components are taken to be the
current project (project axis) or auto-detected (configuration and task axes).
An asterisk (*) is used to explicitly refer to the Global context, as in */*:key.
204
Selecting the configuration In the case of an unspecified configuration (that
is, when the config: part is omitted), if the key is defined in Global, that is
selected. Otherwise, the first configuration defining the key is selected, where
order is determined by the project definition’s configurations member. By
default, this ordering is compile, test, ...
For example, the following are equivalent when run in a project root in the
build in /home/user/sample/:
> compile
> compile:compile
> root/compile
> root/compile:compile
> {file:/home/user/sample/}root/compile:compile
> test:consoleQuick
> test:console
> test:doc
> test:package
Task-specific Settings Some settings are defined per-task. This is used when
there are several related tasks, such as package, packageSrc, and packageDoc,
in the same configuration (such as compile or test). For package tasks, their
settings are the files to package, the options to use, and the output file to
produce. Each package task should be able to have different values for these
settings.
This is done with the task axis, which selects the task to apply a setting to. For
example, the following prints the output jar for the different package tasks.
> package::artifactPath
[info] /home/user/sample/target/scala-2.8.1.final/demo_2.8.1-0.1.jar
> packageSrc::artifactPath
[info] /home/user/sample/target/scala-2.8.1.final/demo_2.8.1-0.1-src.jar
> packageDoc::artifactPath
[info] /home/user/sample/target/scala-2.8.1.final/demo_2.8.1-0.1-doc.jar
205
> test:package::artifactPath
[info] /home/user/sample/target/scala-2.8.1.final/root_2.8.1-0.1-test.jar
Note that a single colon : follows a configuration axis and a double colon ::
follows a task axis.
This section discusses the inspect command, which is useful for exploring re-
lationships between settings. It can be used to determine which setting should
be modified in order to affect another setting, for example.
This shows that libraryDependencies has been defined on the current project
({file:/home/user/sample/}root) in the global configuration (*:). For a task
like update, the output looks like:
Related Settings The “Related” section of inspect output lists all of the
definitions of a key. For example,
206
Dependencies Forward dependencies show the other settings (or tasks) used
to define a setting (or task). Reverse dependencies go the other direction, show-
ing what uses a given setting. inspect provides this information based on either
the requested dependencies or the actual dependencies. Requested dependen-
cies are those that a setting directly specifies. Actual settings are what those
dependencies get resolved to. This distinction is explained in more detail in the
following sections.
...
This shows the inputs to the console task. We can see that it gets its classpath
and options from fullClasspath and scalacOptions(for console). The in-
formation provided by the inspect command can thus assist in finding the right
setting to change. The convention for keys, like console and fullClasspath,
is that the Scala identifier is camel case, while the String representation is
lowercase and separated by dashes. The Scala identifier for a configuration is
uppercase to distinguish it from tasks like compile and test. For example, we
can infer from the previous example how to add code to be run when the Scala
interpreter starts up:
207
Actual Dependencies inspect actual <scoped-key> shows the actual
dependency used. This is useful because delegation means that the dependency
can come from a scope other than the requested one. Using inspect actual, we
see exactly which scope is providing a value for a setting. Combining inspect
actual with plain inspect, we can see the range of scopes that will affect a
setting. Returning to the example in Requested Dependencies,
For initialCommands, we see that it comes from the global scope (*/*:). Com-
bining this with the relevant output from inspect console:
compile:console::initialCommands
The reason we might want to set it here this is that other console tasks will use
this value now. We can see which ones use our new setting by looking at the
reverse dependencies output of inspect actual:
208
We now know that by setting initialCommands on the whole project, we affect
all console tasks in all configurations in that project. If we didn’t want the
initial commands to apply for consoleProject, which doesn’t have our project’s
classpath available, we could use the more specific task axis:
or configuration axis:
The next part describes the Delegates section, which shows the chain of delega-
tion for scopes.
Delegates A setting has a key and a scope. A request for a key in a scope A
may be delegated to another scope if A doesn’t define a value for the key. The
delegation chain is well-defined and is displayed in the Delegates section of the
inspect command. The Delegates section shows the order in which scopes are
searched when a value is not defined for the requested key.
As an example, consider the initial commands for console again:
Triggered Execution
You can make a command run when certain files change by prefixing the com-
mand with ~. Monitoring is terminated when enter is pressed. This triggered
execution is configured by the watch setting, but typically the basic settings
watchSources and pollInterval are modified.
209
• watchSources defines the files for a single project that are monitored
for changes. By default, a project watches resources and Scala and Java
sources.
• watchTransitiveSources then combines the watchSources for the cur-
rent project and all execution and classpath dependencies (see .scala build
definition for details on interProject dependencies).
• pollInterval selects the interval between polling for changes in millisec-
onds. The default value is 500 ms.
Compile
> ~ test:compile
> ~ compile
Testing
You can use the triggered execution feature to run any command or task. One
use is for test driven development, as suggested by Erick on the mailing list.
The following will poll for changes to your source code (main or test) and run
testOnly for the specified test.
Occasionally, you may need to trigger the execution of multiple commands. You
can use semicolons to separate the commands to be triggered.
The following will poll for source changes and run clean and test.
sbt has two alternative entry points that may be used to:
210
• Compile and execute a Scala script containing dependency declarations or
other sbt settings
• Start up the Scala REPL, defining the dependencies that should be on the
classpath
Setup
To set up these entry points, you can either use conscript or manually construct
the startup scripts. In addition, there is a setup script for the script mode that
only requires a JRE installed.
Manual Setup Duplicate your standard sbt script, which was set up accord-
ing to Setup, as scalas and screpl (or whatever names you like).
scalas is the script runner and should use sbt.ScriptMain as the main class,
by adding the -Dsbt.main.class=sbt.ScriptMain parameter to the java com-
mand. Its command line should look like:
For the REPL runner screpl, use sbt.ConsoleMain as the main class:
Usage
sbt Script runner The script runner can run a standard Scala script, but
with the additional ability to configure sbt. sbt settings may be embedded in
the script in a comment block that opens with /***.
211
Example Copy the following script and make it executable. You may need to
adjust the first line depending on your script name and operating system. When
run, the example should retrieve Scala, the required dependencies, compile the
script, and run it directly. For example, if you name it shout.scala, you would
do on Unix:
#!/usr/bin/env scalas
/***
scalaVersion := "2.10.6"
This script will take all *.scala files under src/, append “!” at the end of the
line, and write them under target/.
212
sbt REPL with dependencies The arguments to the REPL mode configure
the dependencies to use when starting up the REPL. An argument may be either
a jar to include on the classpath, a dependency definition to retrieve and put
on the classpath, or a resolver to use when retrieving dependencies.
A dependency definition looks like:
organization%module%revision
organization%%module%revision
"id at url"
Example: To add the Sonatype snapshots repository and add Scalaz 7.0-
SNAPSHOT to REPL classpath:
This syntax was a quick hack. Feel free to improve it. The relevant class is
IvyConsole.
Compiling Scala code with scalac is slow, but sbt often makes it faster. By
understanding how, you can even understand how to make compilation even
faster. Modifying source files with many dependencies might require recompiling
only those source files (which might take 5 seconds for instance) instead of all
the dependencies (which might take 2 minutes for instance). Often you can
control which will be your case and make development faster with a few coding
practices.
Improving the Scala compilation performance is a major goal of sbt, and thus
the speedups it gives are one of the major motivations to use it. A significant
portion of sbt’s sources and development efforts deal with strategies for speeding
up compilation.
To reduce compile times, sbt uses two strategies:
Reduce the overhead for restarting Scalac
213
Implement smart and transparent strategies for incremental recompilation, so
that only modified files and the needed dependencies are recompiled.
sbt always runs Scalac in the same virtual machine. If one compiles source code
using sbt, keeps sbt alive, modifies source code and triggers a new compilation,
this compilation will be faster because (part of) Scalac will have already been
JIT-compiled.
Reduce the number of recompiled source.
When a source file A.scala is modified, sbt goes to great effort to recompile other
source files depending on A.scala only if required - that is, only if the interface of
A.scala was modified. With other build management tools (especially for Java,
like ant), when a developer changes a source file in a non-binary-compatible way,
she needs to manually ensure that dependencies are also recompiled - often by
manually running the clean command to remove existing compilation output;
otherwise compilation might succeed even when dependent class files might need
to be recompiled. What is worse, the change to one source might make depen-
dencies incorrect, but this is not discovered automatically: One might get a
compilation success with incorrect source code. Since Scala compile times are
so high, running clean is particularly undesirable.
By organizing your source code appropriately, you can minimize the amount of
code affected by a change. sbt cannot determine precisely which dependencies
have to be recompiled; the goal is to compute a conservative approximation, so
that whenever a file must be recompiled, it will, even though we might recompile
extra files.
sbt heuristics
sbt tracks source dependencies at the granularity of source files. For each source
file, sbt tracks files which depend on it directly; if the interface of classes,
objects or traits in a file changes, all files dependent on that source must be
recompiled. At the moment sbt uses the following algorithm to calculate source
files dependent on a given source file:
214
The name hashing optimization is enabled by default since sbt 0.13.6.
The heuristics used by sbt imply the following user-visible consequences, which
determine whether a change to a class affects other classes.
All the above discussion about methods also applies to fields and members in
general; similarly, references to classes also extend to objects and traits.
Overview
215
• recompile as little source files as possible cover all changes to type checking
and produced
• byte code triggered by changed source files and/or classpath
The first goal is about making recompilation fast and it’s a sole point of in-
cremental compiler existence. The second goal is about correctness and sets a
lower limit on the size of a set of recompiled files. Determining that set is the
core problem incremental compiler tries to solve. We’ll dive a little bit into this
problem in the overview to understand what makes implementing incremental
compiler a challenging task.
Let’s consider this very simple example:
// A.scala
package a
class A {
def foo(): Int = 12
}
// B.scala
package b
class B {
def bar(x: a.A): Int = x.foo()
}
Let’s assume both of those files are already compiled and user changes A.scala
so it looks like this:
// A.scala
package a
class A {
def foo(): Int = 23 // changed constant
}
// A.scala
package a
216
class A {
def foo(): String = "abc" // changed constant and return type
}
• index source files so it knows whether there were API changes that might
affect other source files; e.g. it needs to detect changes to method signa-
tures as in the example above
• track dependencies between source files; once the change to an API is
detected the algorithm needs to determine the set of files that might be
potentially affected by this change
Both of those pieces of information are extracted from the Scala compiler.
217
API extraction phase The API extraction phase extracts information from
Trees, Types and Symbols and maps it to incremental compiler’s internal data
structures described in the api.specification file.Those data structures allow to
express an API in a way that is independent from Scala compiler version. Also,
such representation is persistent so it is serialized on disk and reused between
compiler runs or even sbt runs.
The API extraction phase consist of two major components:
Mapping Types and Symbols The logic responsible for mapping Types
and Symbols is implemented in API.scala. With introduction of Scala reflection
we have multiple variants of Types and Symbols. The incremental compiler uses
the variant defined in scala.reflect.internal package.
Also, there’s one design choice that might not be obvious. When type corre-
sponding to a class or a trait is mapped then all inherited members are copied
instead of declarations in that class/trait. The reason for doing so is that it
greatly simplifies analysis of API representation because all relevant informa-
tion to a class is stored in one place so there’s no need for looking up parent
type representation. This simplicity comes at a price: the same information
is copied over and over again resulting in a performance hit. For example, ev-
ery class will have members of java.lang.Object duplicated along with full
information about their signatures.
218
files have set. Mapping a Symbol back to (binary) class file is more tricky
because Scala compiler does not track origin of Symbols derived from binary
files. Therefore simple heuristic is used which maps a qualified class name to
corresponding classpath entry. This logic is implemented in dependency phase
which has an access to the full classpath.
The set of Symbols given compilation unit depend on is obtained by performing
a tree walk. The tree walk examines all tree nodes that can introduce a de-
pendency (refer to another Symbol) and gathers all Symbols assigned to them.
Symbols are assigned to tree nodes by Scala compiler during type checking
phase.
Incremental compiler used to rely on CompilationUnit.depends for collecting
dependencies. However, name hashing requires a more precise dependency in-
formation. Check #1002 for details.
// A.scala
class A {
def inc(x: Int): Int = x+1
}
// B.scala
class B {
def foo(a: A, x: Int): Int = a.inc(x)
}
Let’s assume both of those files are compiled and user changes A.scala so it
looks like this:
// A.scala
class A {
def inc(x: Int): Int = x+1
def dec(x: Int): Int = x-1
}
Once user hits save and asks incremental compiler to recompile it’s project it
will do the following:
219
1. Recompile A.scala as the source code has changed (first iteration)
2. While recompiling it will reindex API structure of A.scala and detect it
has changed
3. It will determine that B.scala depends on A.scala and since the API
structure of A.scala has changed B.scala has to be recompiled as well
(B.scala has been invalidated)
4. Recompile B.scala because it was invalidated in 3. due to dependency
change
5. Reindex API structure of B.scala and find out that it hasn’t changed so
we are done
To summarize, we’ll invoke Scala compiler twice: one time to recompile A.scala
and then to recompile B.scala because A has a new method dec.
However, one can easily see that in this simple scenario recompilation of B.scala
is not needed because addition of dec method to A class is irrelevant to the B
class as its not using it and it is not affected by it in any way.
In case of two files the fact that we recompile too much doesn’t sound too bad.
However, in practice, the dependency graph is rather dense so one might end
up recompiling the whole project upon a change that is irrelevant to almost all
files in the whole project. That’s exactly what happens in Play projects when
routes are modified. The nature of routes and reversed routes is that every
template and every controller depends on some methods defined in those two
classes (Routes and ReversedRoutes) but changes to specific route definition
usually affects only small subset of all templates and controllers.
The idea behind name hashing is to exploit that observation and make the
invalidation algorithm smarter about changes that can possibly affect a small
number of files.
220
Just to give you a sneak preview of problems that quickly arise if you consider
that strategy let’s consider those two scenarios.
Inheritance We’ll see how a method not used in another source file might
affect its compilation result. Let’s consider this structure:
// A.scala
abstract class A
// B.scala
class B extends A
// A.scala
abstract class A {
def foo(x: Int): Int
}
Now, once we recompile A.scala we could just say that since A.foo is not used
in B class then we don’t need to recompile B.scala. However, this is not true
because B doesn’t implement a newly introduced, abstract method and an error
should be reported.
Therefore, a simple strategy of looking at used methods for determining whether
a given dependency is relevant or not is not enough.
Enrichment pattern Here we’ll see another case of newly introduced method
(that is not used anywhere yet) that affects compilation results of other files.
This time, no inheritance will be involved but we’ll use enrichment pattern
(implicit conversions) instead.
Let’s assume we have the following structure:
// A.scala
class A
// B.scala
class B {
class AOps(a: A) {
def foo(x: Int): Int = x+1
}
implicit def richA(a: A): AOps = new AOps(a)
def bar(a: A): Int = a.foo(12) // this is expanded to richA(a).foo so we are calling AOPs.
}
221
Now, let’s add a foo method directly to A:
// A.scala
class A {
def foo(x: Int): Int = x-1
}
Now, once we recompile A.scala and detect that there’s a new method defined
in the A class we would need to consider whether this is relevant to the depen-
dency of B.scala on A.scala. Notice that in B.scala we do not use A.foo
(it didn’t exist at the time B.scala was compiled) but we use AOps.foo and
it’s not immediately clear that AOps.foo has anything to do with A.foo. One
would need to detect the fact that a call to AOps.foo as a result of implicit
conversion richA that was inserted because we failed to find foo on A before.
This kind of analysis gets us very quickly to the implementation complexity of
Scala’s type checker and is not feasible to implement in a general case.
Too much information to track All of the above assumed we actually have
full information about the structure of the API and used methods preserved so
we can make use of it. However, as described in Hashing an API representation
we do not store the whole representation of the API but only its hash sum. Also,
dependencies are tracked at source file level and not at class/method level.
One could imagine reworking the current design to track more information but
it would be a very big undertaking. Also, the incremental compiler used to
preserve the whole API structure but it switched to hashing due to the resulting
infeasible memory requirements.
222
// A.scala
class A
// B.scala
class B {
class AOps(a: A) {
def foo(x: Int): Int = x+1
}
implicit def richA(a: A): AOps = new AOps(a)
def bar(a: A): Int = a.foo(12) // this is expanded to richA(a).foo so we are calling AOPs.
}
During the compilation of those two files we’ll extract the following information:
usedNames("A.scala"): A
usedNames("B.scala"): B, AOps, a, A, foo, x, Int, richA, AOps, bar
The usedNames relation track all the names mentioned in the given source file.
The nameHashes relation gives us a hash sum of the groups of members that
are put together in one bucket if they have the same simple name. In addition
to the information presented above we still track the dependency of B.scala on
A.scala.
Now, if we add a foo method to A class:
// A.scala
class A {
def foo(x: Int): Int = x-1
}
usedNames("A.scala"): A, foo
nameHashes("A.scala"): A -> ..., foo -> ...
The incremental compiler compares the name hashes before and after the change
and detects that the hash sum of foo has changed (it’s been added). Therefore,
it looks at all the source files that depend on A.scala, in our case it’s just
B.scala, and checks whether foo appears as a used name. It does, therefore it
recompiles B.scala as intended.
You can see now, that if we added another method to A like xyz then B.scala
wouldn’t be recompiled because nowhere in B.scala is the name xyz mentioned.
223
Therefore, if you have reasonably non-clashing names you should benefit from
a lot of dependencies between source files marked as irrelevant.
It’s very nice that this simple, name-based heuristic manages to withstand the
“enrichment pattern” test. However, name-hashing fails to pass the other test of
inheritance. In order to address that problem, we’ll need to take a closer look
at the dependencies introduced by inheritance vs dependencies introduced by
member references.
// A.scala
class A {
def foo(x: Int): Int = x+1
}
// B.scala
class B(val a: A)
// C.scala
trait C
// D.scala
trait D[T]
224
// X.scala
class X extends A with C with D[B] {
// dependencies by inheritance: A, C, D
// dependencies by member reference: A, C, D, B
}
// Y.scala
class Y {
def test(b: B): Int = b.a.foo(12)
// dependencies by member reference: B, Int, A
}
To sum it up, the way we want to handle inheritance and the problems it
introduces is to track all dependencies introduced by inheritance separately and
have a much more strict way of invalidating dependencies. Essentially, whenever
there’s a dependency by inheritance it will react to any (even minor) change in
parent types.
Computing name hashes One thing we skimmed over so far is how name
hashes are actually computed.
As mentioned before, all definitions are grouped together by their simple name
and then hashed as one bucket. If a definition (for example a class) contains
other definition then those nested definitions do not contribute to a hash sum.
The nested definitions will contribute to hashes of buckets selected by their
name.
225
1. Since Scala supports named arguments in method invocations, the name
of method arguments are part of its interface.
2. Adding a method to a trait requires recompiling all implementing classes.
The same is true for most changes to a method signature in a trait.
3. Calls to super.methodName in traits are resolved to calls to an abstract
method called fullyQualifiedTraitName$$super$methodName; such
methods only exist if they are used. Hence, adding the first call to
super.methodName for a specific method name changes the interface. At
present, this is not yet handled—see #466.
4. sealed hierarchies of case classes allow to check exhaustiveness of pattern
matching. Hence pattern matches using case classes must depend on the
complete hierarchy - this is one reason why dependencies cannot be easily
tracked at the class level (see Scala issue SI-2559 for an example.). Check
#1104 for detailed discussion of tracking dependencies at class level.
warning
Enabling the apiDebug option increases significantly the memory
consumption and degrades the performance of the incremental com-
piler. The underlying reason is that in order to produce meaningful
debugging information about interface differences the incremental
compiler has to retain the full representation of the interface instead
of just the hash sum as it does by default.
Keep this option enabled when you are debugging the incremental
compiler problem only.
curl -O https://java-diff-utils.googlecode.com/files/diffutils-1.2.1.jar
sbt -Dsbt.extraClasspath=diffutils-1.2.1.jar
[info] Loading project definition from /Users/grek/tmp/sbt-013/project
[info] Set current project to sbt-013 (in build file:/Users/grek/tmp/sbt-013/)
> set incOptions := incOptions.value.copy(apiDebug = true)
226
[info] Defining *:incOptions
[info] The new value will be used by compile:incCompileSetup, test:incCompileSetup
[info] Reapplying settings...
[info] Set current project to sbt-013 (in build file:/Users/grek/tmp/sbt-013/)
class A {
def b: Int = 123
}
class A {
def b: String = "abc"
}
and run compile again. Now if you run last compile you should see the
following lines in the debugging log
You can see a unified diff of the two interface textual represetantions. As you
can see, the incremental compiler detected a change to the return type of b
method.
227
change is often invasive, and reducing compilation times is not often a good
enough motivation. That is also why we discuss some of the implications from
the point of view of binary compatibility and software engineering.
Consider the following source file A.scala:
import java.io._
object A {
def openFiles(list: List[File]) =
list.map(name => new FileWriter(name))
}
Let us now consider the public interface of trait A. Note that the return type
of method openFiles is not specified explicitly, but computed by type infer-
ence to be List[FileWriter]. Suppose that after writing this source code, we
introduce some client code and then modify A.scala as follows:
import java.io._
object A {
def openFiles(list: List[File]) =
Vector(list.map(name => new BufferedWriter(new FileWriter(name))): _*)
}
1. Concerning our topic, the client code needs to be recompiled, since chang-
ing the return type of a method, in the JVM, is a binary-incompatible
interface change.
2. If our component is a released library, using our new version requires
recompiling all client code, changing the version number, and so on. Often
not good, if you distribute a library where binary compatibility becomes
an issue.
3. More in general, the client code might now even be invalid. The following
code will for instance become invalid after the change:
228
How can we avoid these problems?
Of course, we cannot solve them in general: if we want to alter the interface
of a module, breakage might result. However, often we can remove implemen-
tation details from the interface of a module. In the example above, for in-
stance, it might well be that the intended return type is more general - namely
Seq[Writer]. It might also not be the case - this is a design choice to be de-
cided on a case-by-case basis. In this example I will assume however that the
designer chooses Seq[Writer], since it is a reasonable choice both in the above
simplified example and in a real-world extension of the above code.
The client snippets above will now become
val a: Seq[Writer] =
new BufferedWriter(new FileWriter("bar.input")) +:
A.openFiles(List(new File("foo.input")))
Further references
Configuration
This part of the documentation has pages documenting particular sbt topics in
detail. Before reading anything in here, you will need the information in the
Getting Started Guide as a foundation.
This page discusses how sbt builds up classpaths for different actions, like
compile, run, and test and how to override or augment these classpaths.
Basics
In sbt 0.10 and later, classpaths now include the Scala library and (when
declared as a dependency) the Scala compiler. Classpath-related settings
and tasks typically provide a value of type Classpath. This is an alias for
229
Seq[Attributed[File]]. Attributed is a type that associates a heterogeneous
map with each classpath entry. Currently, this allows sbt to associate the
Analysis resulting from compilation with the corresponding classpath entry
and for managed entries, the ModuleID and Artifact that defined the
dependency.
To explicitly extract the raw Seq[File], use the files method implicitly added
to Classpath:
sourceGenerators in Compile +=
generate( (sourceManaged in Compile).value / "some_directory")
In this example, generate is some function of type File => Seq[File] that
actually does the work. So, we are appending a new task to the list of main
source generators (sourceGenerators in Compile).
To insert a named task, which is the better approach for plugins:
mySourceGenerator in Compile :=
generate( (sourceManaged in Compile).value / "some_directory")
230
The task method is used to refer to the actual task instead of the result of the
task.
For resources, there are similar keys resourceGenerators and resourceManaged.
Read more on How to exclude .scala source file in project folder - Google Groups
External vs internal Classpaths are also divided into internal and external
dependencies. The internal dependencies are inter-project dependencies. These
effectively put the outputs of one project on the classpath of another project.
External classpaths are the union of the unmanaged and managed classpaths.
• unmanagedClasspath
• managedClasspath
• externalDependencyClasspath
• internalDependencyClasspath
For sources:
For resources
231
• resourceGenerators These are tasks that generate resource files. Typi-
cally, these tasks will put resources in the directory provided by resource-
Managed.
Example You have a standalone project which uses a library that loads
xxx.properties from classpath at run time. You put xxx.properties inside di-
rectory “config”. When you run “sbt run”, you want the directory to be in
classpath.
There is some special support for using compiler plugins. You can set
autoCompilerPlugins to true to enable this functionality.
autoCompilerPlugins := true
To use a compiler plugin, you either put it in your unmanaged library directory
(lib/ by default) or add it as managed dependency in the plugin configura-
tion. addCompilerPlugin is a convenience method for specifying plugin as the
configuration for a dependency:
The compile and testCompile actions will use any compiler plugins found
in the lib directory or in the plugin configuration. You are responsible for
configuring the plugins as necessary. For example, Scala X-Ray requires the
extra option:
scalacOptions += "-Xplugin:<path-to-sxr>/sxr-0.3.0.jar"
232
Continuations Plugin Example
autoCompilerPlugins := true
scalacOptions += "-P:continuations:enable"
autoCompilerPlugins := true
libraryDependencies +=
compilerPlugin("org.scala-lang.plugins" % "continuations" % scalaVersion.value)
scalacOptions += "-P:continuations:enable"
Configuring Scala
sbt needs to obtain Scala for a project and it can do this automatically or you
can configure it explicitly. The Scala version that is configured for a project
will compile, run, document, and provide a REPL for the project code. When
compiling a project, sbt needs to run the Scala compiler as well as provide
the compiler with a classpath, which may include several Scala jars, like the
reflection jar.
The most common case is when you want to use a version of Scala that is
available in a repository. The only required configuration is the Scala version
you want to use. For example,
scalaVersion := "2.10.0"
This will retrieve Scala from the repositories configured via the resolvers set-
ting. It will use this version for building your project: compiling, running,
scaladoc, and the REPL.
233
Configuring the scala-library dependency By default, the standard Scala
library is automatically added as a dependency. If you want to configure it
differently than the default or you have a project with only Java sources, set:
autoScalaLibrary := false
In order to compile Scala sources, the Scala library needs to be on the classpath.
When autoScalaLibrary is true, the Scala library will be on all classpaths: test,
runtime, and compile. Otherwise, you need to add it like any other dependency.
For example, the following dependency definition uses Scala only for tests:
autoScalaLibrary := false
managedScalaInstance := false
234
managedScalaInstance := false
// Add the usual dependency on the library as well on the compiler in the
// 'scala-tool' configuration
libraryDependencies ++= Seq(
"org.scala-lang" % "scala-library" % scalaVersion.value,
"org.scala-lang" % "scala-compiler" % scalaVersion.value % "scala-tool"
)
managedScalaInstance := false
scalaInstance := ...
scalaHome := Some(file("/home/user/scala-2.10/"))
235
a Scala REPL. No managed dependency is recorded on scala-library. This
means that Scala will only be resolved from a repository if you explicitly define
a dependency on Scala or if Scala is depended on indirectly via a dependency.
In these cases, the artifacts for the resolved dependencies will be substituted
with jars in the Scala home lib/ directory.
scalaHome := Some(file("/home/user/scala-2.10/"))
This will be resolved as normal, except that sbt will see if /home/user/scala-2.10/lib/scala-reflect.jar
exists. If it does, that file will be used in place of the artifact from the managed
dependency.
scalaHome := Some(file("/home/user/scala-2.10/"))
To add only some jars, filter the jars from scalaInstance before adding them.
sbt needs Scala jars to run itself since it is written in Scala. sbt uses that same
version of Scala to compile the build definitions that you write for your project
because they use sbt APIs. This version of Scala is fixed for a specific sbt release
and cannot be changed. For sbt 0.13.16, this version is Scala 2.10.6. Because
this Scala version is needed before sbt runs, the repositories used to retrieve this
version are configured in the sbt launcher.
Forking
By default, the run task runs in the same JVM as sbt. Forking is required under
certain circumstances, however. Or, you might want to fork Java processes when
implementing new tasks.
236
By default, a forked process uses the same Java and Scala versions being used
for the build and the working directory and JVM options of the current process.
This page discusses how to enable and configure forking for both run and test
tasks. Each kind of task may be configured separately by scoping the relevant
keys as explained below.
Enable forking
The fork setting controls whether forking is enabled (true) or not (false). It
can be set in the run scope to only fork run commands or in the test scope to
only fork test commands.
To fork all test tasks (test, testOnly, and testQuick) and run tasks (run,
runMain, test:run, and test:runMain),
fork := true
To enable forking run tasks only, set fork to true in the run scope.
Similarly, set fork in (Compile,run) := true to only fork the main run
tasks. run and runMain share the same configuration and cannot be configured
separately.
To enable forking all test tasks only, set fork to true in the test scope:
See Testing for more control over how tests are assigned to JVMs and what
options to pass to each group.
237
// sets the working directory for all `run`-like tasks
baseDirectory in run := file("/path/to/working/directory/")
or specify the configuration to affect only the main or test run tasks:
Java Home
javaHome := Some(file("/path/to/jre/"))
Note that if this is set globally, it also sets the Java installation used to compile
Java sources. You can restrict it to running only by setting it in the run scope:
As with the other settings, you can specify the configuration to affect only the
main or test run tasks or just the test tasks.
238
Configuring output
By default, forked output is sent to the Logger, with standard output logged at
the Info level and standard error at the Error level. This can be configured
with the outputStrategy setting, which is of type OutputStrategy.
// send output to the provided Logger `log` after the process terminates
outputStrategy := Some(BufferedOutput(log: Logger))
As with other settings, this can be configured individually for main or test run
tasks or for test tasks.
Configuring Input
By default, the standard input of the sbt process is not forwarded to the forked
process. To enable this, configure the connectInput setting:
Direct Usage
To fork a new Java process, use the Fork API. The values of interest are
Fork.java, Fork.javac, Fork.scala, and Fork.scalac. These are of type
Fork and provide apply and fork methods. For example, to fork a new Java
process, :
ForkOptions defines the Java installation to use, the working directory, environ-
ment variables, and more. For example, :
239
val cwd: File = ...
val javaDir: File = ...
val options = ForkOptions(
envVars = Map("KEY" -> "value"),
workingDirectory = Some(cwd),
javaHome = Some(javaDir)
)
Global Settings
com.typesafe.sbteclipse.core.EclipsePlugin.EclipseKeys.withSource := true
To change the default shellPrompt for every project using this approach, create
a local plugin ~/.sbt/0.13/plugins/ShellPrompt.scala:
240
import sbt._
import Keys._
Java Sources
sbt has support for compiling Java sources with the limitation that dependency
tracking is limited to the dependencies present in compiled class files.
Usage
javacOptions += "-g:none"
As with options for the Scala compiler, the arguments are not parsed by sbt.
Multi-element options, such as -source 1.5, are specified like:
You can specify the order in which Scala and Java sources are built with the
compileOrder setting. Possible values are from the CompileOrder enumeration:
Mixed, JavaThenScala, and ScalaThenJava. If you have circular dependencies
between Scala and Java sources, you need the default, Mixed, which passes
both Java and Scala sources to scalac and then compiles the Java sources with
javac. If you do not have circular dependencies, you can use one of the other
two options to speed up your build by not passing the Java sources to scalac.
For example, if your Scala sources depend on your Java sources, but your Java
sources do not depend on your Scala sources, you can do:
241
compileOrder := CompileOrder.JavaThenScala
To specify different orders for main and test sources, scope the setting by con-
figuration:
The Scala compiler does not identify compile-time constant variables (Java spec-
ification 4.12.4) as such when parsing a Java file from source. This issue has
several symptoms, described in the Scala ticket SI-5333:
Since Scala 2.11.4, a similar issue arises when using a Java-defined annotation
in a Scala class. The Scala compiler does not recognize @Retention annotations
when parsing the annotation @interface from source and therefore emits the
annotation with visibility RUNTIME (SI-8928).
242
// Include only src/main/java in the compile configuration
unmanagedSourceDirectories in Compile := (javaSource in Compile).value :: Nil
However, there should not be any harm in leaving the Scala directories if they
are empty.
Mapping Files
Relative to a directory
The Path.relativeTo method is used to map a File to its path String relative
to a base directory or directories. The relativeTo method accepts a base
directory or sequence of base directories to relativize an input file against. The
first directory that is an ancestor of the file is used in the case of a sequence of
base directories.
For example:
import Path.relativeTo
val files: Seq[File] = file("/a/b/C.scala") :: Nil
val baseDirectories: Seq[File] = file("/a") :: Nil
val mappings: Seq[(File,String)] = files pair relativeTo(baseDirectories)
243
Rebase
The Path.rebase method relativizes an input file against one or more base
directories (the first argument) and then prepends a base String or File (the
second argument) to the result. As with relativeTo, the first base directory
that is an ancestor of the input file is used in the case of multiple base directories.
For example, the following demonstrates building a Seq[(File, String)] using
rebase:
import Path.rebase
val files: Seq[File] = file("/a/b/C.scala") :: Nil
val baseDirectories: Seq[File] = file("/a") :: Nil
val mappings: Seq[(File,String)] = files pair rebase(baseDirectories, "pre/")
import Path.rebase
val files: Seq[File] = file("/a/b/C.scala") :: Nil
val baseDirectories: Seq[File] = file("/a") :: Nil
val newBase: File = file("/new/base")
val mappings: Seq[(File,File)] = files pair rebase(baseDirectories, newBase)
Flatten
The Path.flat method provides a function that maps a file to the last compo-
nent of the path (its name). For a File to File mapping, the input file is mapped
to a file with the same name in a given target directory. For example:
import Path.flat
val files: Seq[File] = file("/a/b/C.scala") :: Nil
val mappings: Seq[(File,String)] = files pair flat
244
import Path.flat
val files: Seq[File] = file("/a/b/C.scala") :: Nil
val newBase: File = file("/new/base")
val mappings: Seq[(File,File)] = files pair flat(newBase)
Alternatives
To try to apply several alternative mappings for a file, use |, which is implicitly
added to a function of type A => Option[B]. For example, to try to relativize
a file against some base directories but fall back to flattening:
import Path.relativeTo
val files: Seq[File] = file("/a/b/C.scala") :: file("/zzz/D.scala") :: Nil
val baseDirectories: Seq[File] = file("/a") :: Nil
val mappings: Seq[(File,String)] = files pair ( relativeTo(baseDirectories) | flat )
Local Scala
To use a locally built Scala version, define the scalaHome setting, which is of
type Option[File]. This Scala version will only be used for the build and not
for sbt, which will still use the version it was compiled against.
Example:
scalaHome := Some(file("/path/to/scala"))
Using a local Scala version will override the scalaVersion setting and will not
work with cross building.
sbt reuses the class loader for the local Scala version. If you recompile your
local Scala version and you are using sbt interactively, run
> reload
245
Macro Projects
Introduction
1. The current macro implementation in the compiler requires that macro im-
plementations be compiled before they are used. The solution is typically
to put the macros in a subproject or in their own configuration.
2. Sometimes the macro implementation should be distributed with the main
code that uses them and sometimes the implementation should not be
distributed at all.
246
package demo
import language.experimental.macros
import scala.reflect.macros.Context
object Demo {
// Returns the tree of `a` after the typer, printed as source code.
def desugar(a: Any): String = macro desugarImpl
val s = show(a.tree)
c.Expr(
Literal(Constant(s))
)
}
}
macro/src/test/scala/demo/Usage.scala:
package demo
object Usage {
def main(args: Array[String]) {
val s = Demo.desugar(List(1, 2, 3).reverse)
println(s)
}
}
package demo
object Usage {
def main(args: Array[String]) {
val s = Demo.desugar(List(6, 4, 5).sorted)
println(s)
}
}
247
Common Interface
Sometimes, the macro implementation and the macro usage should share some
common code. In this case, declare another subproject for the common code and
have the main project and the macro subproject depend on the new subproject.
For example, the project definitions from above would look like:
Distribution
To include the macro code with the core code, add the binary and source map-
pings from the macro subproject to the core project. And also macro subproject
should be removed from core project dependency in publishing. For example,
the core Project definition above would now look like:
248
lazy val core = (project in file("core"))
.dependsOn(macroSub % "compile-internal, test-internal")
.settings(
commonSettings,
// include the macro classes and resources in the main jar
mappings in (Compile, packageBin) ++= mappings.in(macroSub, Compile, packageBin).value,
// include the macro sources in the main source jar
mappings in (Compile, packageSrc) ++= mappings.in(macroSub, Compile, packageSrc).value
)
You may wish to disable publishing the macro implementation. This is done by
overriding publish and publishLocal to do nothing:
The techniques described here may also be used for the common interface de-
scribed in the previous section.
Paths
This page describes files, sequences of files, and file filters. The base type used
is java.io.File, but several methods are augmented through implicits:
Constructing a File
sbt 0.10+ uses java.io.File to represent a file instead of the custom sbt.Path
class that was in sbt 0.7 and earlier. sbt defines the alias File for java.io.File
so that an extra import is not necessary. The file method is an alias for the
single-argument File constructor to simplify constructing a new file from a
String:
249
Additionally, sbt augments File with a / method, which is an alias for the two-
argument File constructor for building up a path:
Relative files should only be used when defining the base directory of a Project,
where they will be resolved properly.
This setting sets the location of the shell history to be in the base directory of
the build, irrespective of the project the setting is defined in:
Path Finders
250
get This selects all files that end in .scala that are in src or a descendent
directory. The list of files is not actually evaluated until get is called:
If the filesystem changes, a second call to get on the same PathFinder object
will reflect the changes. That is, the get method reconstructs the list of files
each time. Also, get only returns Files that existed at the time it was called.
This selects all files that end in .scala that are in the src directory.
Name Filter The argument to the child and descendent selectors * and ** is
actually a NameFilter. An implicit is used to convert a String to a NameFilter
that interprets * to represent zero or more characters of any value. See the Name
Filters section below for more information.
When evaluated using get, this will return src/main/, lib/, and
target/classes/. The concatenated finder supports all standard meth-
ods. For example,
251
selects all jars directly in the “lib” and “target” directories.
A common problem is excluding version control directories. This can be accom-
plished as follows:
The first selector selects all Scala sources and the second selects all sources that
are a descendent of a .svn directory. The --- method removes all files returned
by the second selector from the sequence of files returned by the first selector.
Filtering There is a filter method that accepts a predicate of type File =>
Boolean and is non-strict:
Mappings The packaging and file copying methods in sbt expect values of
type Seq[(File,String)] and Seq[(File,File)], respectively. These are
mappings from the input file to its (String) path in the jar or its (File) destina-
tion. This approach replaces the relative path approach (using the ## method)
from earlier versions of sbt.
Mappings are discussed in detail on the Mapping-Files page.
252
File Filters
There are some useful combinators added to FileFilter. The || method de-
clares alternative FileFilters. The following example selects all Java or Scala
source files under “src”:
The -- method excludes a files matching a second filter from the files matched
by the first:
This will get right.png and left.png, but not logo.png, for example.
Parallel Execution
Task ordering
read := IO.read(file("/tmp/sample.txt"))
sbt is free to execute write first and then read, read first and then write, or
read and write simultaneously. Execution of these tasks is non-deterministic
because they share a file. A correct declaration of the tasks would be:
253
write := {
val f = file("/tmp/sample.txt")
IO.write(f, "Some content.")
f
}
read := IO.read(write.value)
This establishes an ordering: read must run after write. We’ve also guaranteed
that read will read from the same file that write created.
Practical constraints
Note: The feature described in this section is experimental. The default config-
uration of the feature is subject to change in particular.
Configuration
254
1. A task is tagged in order to classify its purpose and resource utiliza-
tion. For example, the compile task may be tagged as Tags.Compile and
Tags.CPU.
2. A list of rules restrict the tasks that may execute concurrently. For exam-
ple, Tags.limit(Tags.CPU, 4) would allow up to four computation-heavy
tasks to run at a time.
The system is thus dependent on proper tagging of tasks and then on a good
set of rules.
compile := myCompileTask.value
download := downloadImpl.value
255
• the number of CPU-using tasks to be no more than 2
• the number of tasks using the network to be no more than 10
• test execution to only one test at a time across all projects
• the total number of tasks to be less than or equal to 15
Note that these restrictions rely on proper tagging of tasks. Also, the value
provided as the limit must be at least 1 to ensure every task is able to be
executed. sbt will generate an error if this condition is not met.
Most tasks won’t be tagged because they are very short-lived. These tasks are
automatically assigned the label Untagged. You may want to include these tasks
in the CPU rule by using the limitSum method. For example:
...
Tags.limitSum(2, Tags.CPU, Tags.Untagged)
...
Note that the limit is the first argument so that tags can be provided as varargs.
Another useful convenience function is Tags.exclusive. This specifies that a
task with the given tag should execute in isolation. It starts executing only
when no other tasks are running (even if they have the exclusive tag) and no
other tasks may start execution until it completes. For example, a task could
be tagged with a custom tag Benchmark and a rule configured to ensure such a
task is executed by itself:
...
Tags.exclusive(Benchmark)
...
Finally, for the most flexibility, you can specify a custom function of type
Map[Tag,Int] => Boolean. The Map[Tag,Int] represents the weighted
tags of a set of tasks. If the function returns true, it indicates that the
set of tasks is allowed to execute concurrently. If the return value is false,
the set of tasks will not be allowed to execute concurrently. For example,
Tags.exclusive(Benchmark) is equivalent to the following:
...
Tags.customLimit { (tags: Map[Tag,Int]) =>
val exclusive = tags.getOrElse(Benchmark, 0)
// the total number of tasks in the group
val all = tags.getOrElse(Tags.All, 0)
// if there are no exclusive tasks in this group, this rule adds no restrictions
exclusive == 0 ||
// If there is only one task, allow it to execute.
256
all == 1
}
...
There are some basic rules that custom functions must follow, but the main one
to be aware of in practice is that if there is only one task, it must be allowed to
execute. sbt will generate a warning if the user defines restrictions that prevent
a task from executing at all and will then execute the task anyway.
Built-in Tags and Rules Built-in tags are defined in the Tags object. All
tags listed below must be qualified by this object. For example, CPU refers to
the Tags.CPU value.
The built-in semantic tags are:
Of additional note is that the default test task will propagate its tags to each
child task created for each test class.
The default rules provide the same behavior as previous versions of sbt:
concurrentRestrictions in Global := {
val max = Runtime.getRuntime.availableProcessors
Tags.limitAll(if(parallelExecution.value) max else 1) :: Nil
}
257
As before, parallelExecution in Test controls whether tests are mapped to
separate tasks. To restrict the number of concurrently executing tests in all
projects, use:
Custom Tags To define a new tag, pass a String to the Tags.Tag method.
For example:
aCustomTask := aImpl.value
concurrentRestrictions in Global +=
Tags.limit(Custom, 1)
Future work
This is an experimental feature and there are several aspects that may change
or require further work.
compile := myCompileTask.value
compile := {
val result = compile.value
... do some post processing ...
}
258
Fractional weighting Weights are currently ints, but could be changed to
be doubles if fractional weights would be useful. It is important to preserve a
consistent notion of what a weight of 1 means so that built-in and custom tasks
share this definition and useful rules can be written.
Default Behavior User feedback on what custom rules work for what work-
loads will help determine a good set of default tags and rules.
External Processes
Usage
sbt includes a process library to simplify working with external processes. The
library is available without import in build definitions and at the interpreter
started by the consoleProject task.
To run an external command, follow it with an exclamation mark !:
259
ProcessBuilder runs the command and returns an instance of sbt.Process,
which can be used to destroy the process before it completes. With no argu-
ments, the ! method sends output to standard output and standard error. You
can pass a Logger to the ! method to send output to the Logger:
If you need to set the working directory or modify the environment, call
sbt.Process explicitly, passing the command sequence (command and argu-
ment list) or command string first and the working directory second. Any
environment variables can be passed as a vararg list of key/value String pairs.
Process("ls" :: "-l" :: Nil, Path.userHome, "key1" -> value1, "key2" -> value2) ! log
• a #&& b Execute a. If the exit code is nonzero, return that exit code and
do not execute b. If the exit code is zero, execute b and return its exit
code.
• a #|| b Execute a. If the exit code is zero, return zero for the exit code
and do not execute b. If the exit code is nonzero, execute b and return
its exit code.
• a #| b Execute a and b, piping the output of a to the input of b.
There are also operators defined for redirecting output to Files and input from
Files and URLs. In the following definitions, url is an instance of URL and file
is an instance of File.
• a #< url or url #> a Use url as the input to a. a may be a File or a
command.
• a #< file or file #> a Use file as the input to a. a may be a File or
a command.
• a #> file or file #< a Write the output of a to file. a may be a File,
URL, or a command.
260
• a #>> file or file #<< a Append the output of a to file. a may be a
File, URL, or a command.
There are some additional methods to get the output from a forked process into
a String or the output lines as a Stream[String]. Here are some examples,
but see the ProcessBuilder API for details.
Finally, there is a cat method to send the contents of Files and URLs to standard
output.
Copy a File:
"find src -name *.scala -exec grep null {} ;" #| "xargs test -z" #&& "echo null-free" #
Use cat:
261
Running Project Code
The run and console actions provide a means for running user code in the same
virtual machine as sbt.
run also exists in a variant called runMain that takes an additional initial argu-
ment allowing you to specify the fully qualified name of the main class you want
to run. run andrunMain share the same configuration and cannot be configured
separately.
This page describes the problems with running user code in the same virtual
machine as sbt, how sbt handles these problems, what types of code can use
this feature, and what types of code must use a forked jvm. Skip to User Code
if you just want to see when you should use a forked jvm.
Problems
System.exit User code can call System.exit, which normally shuts down the
JVM. Because the run and console actions run inside the same JVM as sbt,
this also ends the build and requires restarting sbt.
Threads User code can also start other threads. Threads can be left running
after the main method returns. In particular, creating a GUI creates several
threads, some of which may not terminate until the JVM terminates. The
program is not completed until either System.exit is called or all non-daemon
threads terminate.
sbt’s Solutions
262
Threads sbt makes a list of all threads running before executing user code.
After the user code returns, sbt can then determine the threads created by the
user code. For each user-created thread, sbt replaces the uncaught exception
handler with a custom one that handles the custom SecurityException thrown
by calls to System.exit and delegates to the original handler for everything else.
sbt then waits for each created thread to exit or for System.exit to be called.
sbt handles a call to System.exit as described above.
A user-created thread is one that is not in the system thread group and is not
an AWT implementation thread (e.g. AWT-XAWT, AWT-Windows). User-created
threads include the AWT-EventQueue-* thread(s).
User Code Given the above, when can user code be run with the run and
console actions?
The user code cannot rely on shutdown hooks and at least one of the following
situations must apply for user code to run in the same JVM:
The requirements on threading and shutdown hooks are required because the
JVM does not actually shut down. So, shutdown hooks cannot be run and
threads are not terminated unless they stop when interrupted. If these require-
ments are not met, code must run in a forked jvm.
The feature of allowing System.exit and multiple threads to be used cannot
completely emulate the situation of running in a separate JVM and is intended
for development. Program execution should be checked in a forked jvm when
using multiple threads or System.exit.
As of sbt 0.13.1, multiple run instances can be managed. There can only be one
application that uses AWT at a time, however.
Testing
Basics
263
• Scala sources in src/test/scala/
• Java sources in src/test/java/
• Resources for the test classpath in src/test/resources/
The resources may be accessed from tests by using the getResource methods
of java.lang.Class or java.lang.ClassLoader.
The main Scala testing frameworks ( ScalaCheck, ScalaTest, and specs2) provide
an implementation of the common test interface and only need to be added to the
classpath to work with sbt. For example, ScalaCheck may be used by declaring
it as a managed dependency:
Test is the configuration and means that ScalaCheck will only be on the test
classpath and it isn’t needed by the main sources. This is generally good practice
for libraries because your users don’t typically need your test dependencies to
use your library.
With the library dependency defined, you can then add test sources in the
locations listed above and compile and run tests. The tasks for running tests
are test and testOnly. The test task accepts no command line arguments
and runs all tests:
> test
testOnly The testOnly task accepts a whitespace separated list of test names
to run. For example:
testQuick The testQuick task, like testOnly, allows to filter the tests to
run to specific tests or wildcards using the same syntax to indicate the filters.
In addition to the explicit filter, only the tests that satisfy one of the following
conditions are run:
264
Tab completion Tab completion is provided for test names based on the
results of the last test:compile. This means that a new sources aren’t available
for tab completion until they are compiled and deleted sources won’t be removed
from tab completion until a recompile. A new test source can still be manually
written out and run using testOnly.
Other tasks Tasks that are available for main sources are generally available
for test sources, but are prefixed with test: on the command line and are
referenced in Scala code with in Test. These tasks include:
• test:compile
• test:console
• test:consoleQuick
• test:run
• test:runMain
Output
By default, logging is buffered for each test source file until all tests for that file
complete. This can be disabled by setting logBuffered:
Test Reports By default, sbt will generate JUnit XML test reports for all
tests in the build, located in the target/test-reports directory for a project.
This can be disabled by disabling the JUnitXmlReportPlugin
Options
To specify test framework arguments as part of the build, add options con-
structed by Tests.Argument:
265
testOptions in Test += Tests.Argument("-verbosity", "1")
Setup and Cleanup Specify setup and cleanup actions using Tests.Setup
and Tests.Cleanup. These accept either a function of type () => Unit or a
function of type ClassLoader => Unit. The variant that accepts a ClassLoader
is passed the class loader that is (or was) used for running the tests. It provides
access to the test classes as well as the test framework classes.
Examples:
Disable Parallel Execution of Tests By default, sbt runs all tasks in par-
allel and within the same JVM as sbt itself. Because each test is mapped to
a task, tests are also run in parallel by default. To make tests within a given
project execute serially: :
Filter classes If you want to only run test classes whose name ends with
“Test”, use Tests.Filter:
266
Forking tests The setting:
specifies that all tests will be executed in a single external JVM. See Forking
for configuring standard options for forking. By default, tests executed in a
forked JVM are executed sequentially. More control over how tests are assigned
to JVMs and what options to pass to those is available with testGrouping key.
For example in build.sbt:
import Tests._
{
def groupByFirst(tests: Seq[TestDefinition]) =
tests groupBy (_.name(0)) map {
case (letter, tests) => new Group(letter.toString, tests, SubProcess(Seq("-Dfirst.lett
} toSeq
The tests in a single group are run sequentially. Control the number of
forked JVMs allowed to run at the same time by setting the limit on
Tags.ForkedTestGroup tag, which is 1 by default. Setup and Cleanup actions
cannot be provided with the actual test class loader when a group is forked.
In addition, forked tests can optionally be run in parallel. This feature is still
considered experimental, and may be enabled with the following setting :
You can add an additional test configuration to have a separate set of test
sources and associated compilation, packaging, and testing tasks and settings.
The steps are:
The following two examples demonstrate this. The first example shows how
to enable integration tests. The second shows how to define a customized test
configuration. This allows you to define multiple types of tests per project.
267
Integration Tests The following full build configuration demonstrates inte-
gration tests.
The standard testing tasks are available, but must be prefixed with it:. For
example,
268
then these will be picked up by the Test configuration and in turn by the
IntegrationTest configuration. Options can be added specifically for integra-
tion tests by putting them in the IntegrationTest configuration:
Or, use := to overwrite any existing options, declaring these to be the definitive
integration test options:
The extend(Test) part means to delegate to Test for undefined FunTest set-
tings. The line that adds the tasks and settings for the new test configuration
is:
settings(inConfig(FunTest)(Defaults.testSettings))
This says to add test and settings tasks in the FunTest configuration.
We could have done it this way for integration tests as well. In fact,
269
Defaults.itSettings is a convenience definition: val itSettings =
inConfig(IntegrationTest)(Defaults.testSettings).
The comments in the integration test section hold, except with IntegrationTest
replaced with FunTest and "it" replaced with "fun". For example, test options
can be configured specifically for FunTest:
> fun:test
270
• We filter the tests to be run for each configuration.
> test
To run tests for the added configuration (here, "fun"), prefix it with the config-
uration name as before:
> fun:test
> fun:testOnly org.example.AFunTest
The tests to run in parallel would be run with test and the ones to run in serial
would be run with serial:test.
JUnit
Support for JUnit is provided by junit-interface. To add JUnit support into your
project, add the junit-interface dependency in your project’s main build.sbt file.
Extensions
This page describes adding support for additional testing libraries and defining
additional test reporters. You do this by implementing sbt interfaces (described
below). If you are the author of the testing framework, you can depend on
the test interface as a provided dependency. Alternatively, anyone can provide
support for a test framework by implementing the interfaces in a separate project
and packaging the project as an sbt Plugin.
271
Custom Test Framework The main Scala testing libraries have built-in sup-
port for sbt. To add support for a different framework, implement the uniform
test interface.
Custom Test Reporters Test frameworks report status and results to test
reporters. You can create a new test reporter by implementing either TestRe-
portListener or TestsListener.
Specify the test reporters you want to use by overriding the testListeners
setting in your project definition.
testListeners += customTestListener
Dependency Management
This part of the documentation has pages documenting particular sbt topics in
detail. Before reading anything in here, you will need the information in the
Getting Started Guide as a foundation.
Artifacts
By default, the published artifacts are the main binary jar, a jar containing the
main sources and resources, and a jar containing the API documentation. You
can add artifacts for the test classes, sources, or API or you can disable some
of the main artifacts.
To add all test artifacts:
272
// enable publishing the jar produced by `test:package`
publishArtifact in (Test, packageBin) := true
273
artifactName := { (sv: ScalaVersion, module: ModuleID, artifact: Artifact) =>
artifact.name + "-" + module.revision + "." + artifact.extension
}
myTask := {
val (art, file) = packagedArtifact.in(Compile, packageBin).value
println("Artifact definition: " + art)
println("Packaged file: " + file.getAbsolutePath)
}
In addition to configuring the built-in artifacts, you can declare other artifacts
to publish. Multiple artifacts are allowed when using Ivy metadata, but a Maven
POM file only supports distinguishing artifacts based on classifiers and these
are not recorded in the POM.
Basic Artifact construction look like:
For example:
See the Ivy documentation for more details on artifacts. See the Artifact API
for combining the parameters above and specifying Configurations and extra
attributes.
To declare these artifacts for publishing, map them to the task that generates
the artifact:
274
val myImageTask = taskKey[File](...)
myImageTask := {
val artifact: File = makeArtifact(...)
artifact
}
...
lazy val proj = Project(...)
.settings( addArtifact(...).settings )
...
A common use case for web applications is to publish the .war file instead of
the .jar file.
To specify the artifacts to use from a dependency that has custom or multiple
artifacts, use the artifacts method on your dependencies. For example:
The from and classifer methods (described on the Library Management page)
are actually convenience methods that translate to artifacts:
275
def from(url: String) = artifacts( Artifact(name, new URL(url)) )
def classifier(c: String) = artifacts( Artifact(name, c) )
Background
276
5. Overriding all of the above, skip in update := true will tell sbt to
never perform resolution. Note that this can cause dependent tasks to
fail. For example, compilation may fail if jars have been deleted from the
cache (and so needed classes are missing) or a dependency has been added
(but will not be resolved because skip is true). Also, update itself will
immediately fail if resolution has not been allowed to run since the last
clean.
1. Run update explicitly. This will typically fix problems with out of date
SNAPSHOTs or locally published artifacts.
2. If a file cannot be found, look at the output of update to see where Ivy
is looking for the file. This may help diagnose an incorrectly defined
dependency or a dependency that is actually not present in a repository.
3. last update contains more information about the most recent resolution
and download. The amount of debugging output from Ivy is high, so you
may want to use lastGrep (run help lastGrep for usage).
4. Run clean and then update. If this works, it could indicate a bug in sbt,
but the problem would need to be reproduced in order to diagnose and fix
it.
5. Before deleting all of the Ivy cache, first try deleting files in
~/.ivy2/cache related to problematic dependencies. For example,
if there are problems with dependency "org.example" % "demo" %
"1.0", delete ~/.ivy2/cache/org.example/demo/1.0/ and retry update.
This avoids needing to redownload all dependencies.
6. Normal sbt usage should not require deleting files from ~/.ivy2/cache,
especially if the first four steps have been followed. If deleting the cache
fixes a dependency management issue, please try to reproduce the issue
and submit a test case.
Plugins
These troubleshooting steps can be run for plugins by changing to the build def-
inition project, running the commands, and then returning to the main project.
For example:
277
Notes
Library Management
There’s now a getting started page about library management, which you may
want to read first.
Documentation Maintenance Note: it would be nice to remove the overlap be-
tween this page and the getting started page, leaving this page with the more
advanced topics such as checksums and external Ivy files.
Introduction
There are two ways for you to manage libraries with sbt: manually or auto-
matically. These two ways can be mixed as well. This page discusses the two
approaches. All configurations shown here are settings that go either directly
in a .sbt file or are appended to the settings of a Project in a .scala file.
Manually managing dependencies involves copying any jars that you want to use
to the lib directory. sbt will put these jars on the classpath during compilation,
testing, running, and when using the interpreter. You are responsible for adding,
removing, updating, and otherwise managing the jars in this directory. No
modifications to your project definition are required to use this method unless
you would like to change the location of the directory you store the jars in.
278
To change the directory jars are stored in, change the unmanagedBase setting
in your project definition. For example, to use custom_lib/:
If you want more control and flexibility, override the unmanagedJars task, which
ultimately provides the manual dependencies to sbt. The default implementa-
tion is roughly:
If you want to add jars from multiple directories in addition to the default
directory, you can do:
sbt uses Apache Ivy to implement dependency management in all three cases.
The default is to use inline declarations, but external configuration can be ex-
plicitly selected. The following sections describe how to use each method of
automatic dependency management.
279
Dependencies Declaring a dependency looks like:
or
If you are using a dependency that was built with sbt, double the first % to be
%%:
This will use the right jar for the dependency built with the version of Scala
that you are currently using. If you get an error while resolving this kind of
dependency, that dependency probably wasn’t published for the version of Scala
you are using. See Cross Build for details.
Ivy can select the latest revision of a module according to constraints you specify.
Instead of a fixed revision like "1.6.1", you specify "latest.integration",
"2.9.+", or "[1.0,)". See the Ivy revisions documentation for details.
For example:
280
sbt can search your local Maven repository if you add it as a repository:
To use the local repository, but not the Maven Central repository:
Override all resolvers for all builds The repositories used to retrieve sbt,
Scala, plugins, and application dependencies can be configured globally and de-
clared to override the resolvers configured in a build or plugin definition. There
are two parts:
[repositories]
local
my-maven-repo: https://example.org/repo
my-ivy-repo: https://example.org/ivy-repo/, [organization]/[module]/[revision]/[type]s/[artif
281
Explicit URL If your project requires a dependency that is not present in a
repository, a direct URL to its jar can be specified as follows:
The URL is only used as a fallback if the dependency cannot be found through
the configured repositories. Also, the explicit URL is not included in published
metadata (that is, the pom or ivy.xml).
Classifiers You can specify the classifier for a dependency using the
classifier method. For example, to get the jdk15 version of TestNG:
libraryDependencies +=
"org.lwjgl.lwjgl" % "lwjgl-platform" % lwjglVersion classifier "natives-windows" classifie
transitiveClassifiers := Seq("sources")
libraryDependencies +=
"log4j" % "log4j" % "1.2.15" exclude("javax.jms", "jms")
282
The excludeAll method is more flexible, but because it cannot be represented
in a pom.xml, it should only be used when a pom doesn’t need to be generated.
For example,
libraryDependencies +=
"log4j" % "log4j" % "1.2.15" excludeAll(
ExclusionRule(organization = "com.sun.jdmk"),
ExclusionRule(organization = "com.sun.jmx"),
ExclusionRule(organization = "javax.jms")
)
libraryDependencies +=
"org.apache.felix" % "org.apache.felix.framework" % "1.8.0" withSources() withJavadoc()
Note that this is not transitive. Use the update-*classifiers tasks for that.
projectID := {
val previous = projectID.value
previous.extra("color" -> "blue", "component" -> "compiler-interface")
}
283
Inline Ivy XML sbt additionally supports directly specifying the configura-
tions or dependencies sections of an Ivy configuration file inline. You can mix
this with inline Scala dependency and repository declarations.
For example:
ivyXML :=
<dependencies>
<dependency org="javax.mail" name="mail" rev="1.4.2">
<exclude module="activation"/>
</dependency>
</dependencies>
Ivy Home Directory By default, sbt uses the standard Ivy home direc-
tory location ${user.home}/.ivy2/. This can be configured machine-wide, for
use by both the sbt launcher and by projects, by setting the system property
sbt.ivy.home in the sbt startup script (described in Setup).
For example:
284
Conflict Management The conflict manager decides what to do when de-
pendency resolution brings in different versions of the same library. By default,
the latest revision is selected. This can be changed by setting conflictManager,
which has type ConflictManager. See the Ivy documentation for details on the
different conflict managers. For example, to specify that no conflicts are allowed,
conflictManager := ConflictManager.strict
With this set, any conflicts will generate an error. To resolve a conflict, you
must configure a dependency override, which is explained in a later section.
The default conflict manager will select the newer version of akka-actor, 2.3.7.
This can be confirmed in the output of show update, which shows the newer
version as being selected and the older version as evicted.
[info] com.typesafe.akka:akka-actor_2.10
[info] - 2.3.7
...
[info] - 2.1.4
...
[info] evicted: true
[info] evictedReason: latest-revision
...
[info] callers: org.w3:banana-rdf_2.10:0.4
Furthermore, the binary version compatibility of the akka-actor 2.1.4 and 2.3.7
are not guaranteed since the second segment has bumped up. sbt 0.13.6+ detects
this automatically and prints out the following warning:
285
Since akka-actor 2.1.4 and 2.3.7 are not binary compatible, the only way to fix
this is to downgrade your dependency to akka-actor 2.1.4, or upgrade banana-rdf
to use akka-actor 2.3.
The default conflict manager chooses the latest revision of log4j, 1.2.17:
This will not add a direct dependency on log4j, but will force the revision to be
1.2.16. This is confirmed by the output of show update:
286
sbt 0.13.6+ will try to reconstruct dependencies tree when it fails to resolve a
managed dependency. This is an approximation, but it should help you figure
out where the problematic dependency is coming from. When possible sbt will
display the source position next to the modules:
[warn] ::::::::::::::::::::::::::::::::::::::::::::::
[warn] :: UNRESOLVED DEPENDENCIES ::
[warn] ::::::::::::::::::::::::::::::::::::::::::::::
[warn] :: foundrylogic.vpp#vpp;2.2.1: not found
[warn] ::::::::::::::::::::::::::::::::::::::::::::::
[warn]
[warn] Note: Unresolved dependencies path:
[warn] foundrylogic.vpp:vpp:2.2.1
[warn] +- org.apache.cayenne:cayenne-tools:3.0.2
[warn] +- org.apache.cayenne.plugins:maven-cayenne-plugin:3.0.2 (/foo/some-test/build.sb
[warn] +- d:d_2.10:0.1-SNAPSHOT
Configurations Ivy configurations are a useful feature for your build when
you need custom groups of dependencies, such as for a plugin. Ivy configurations
are essentially named sets of dependencies. You can read the Ivy documentation
for details.
The built-in use of configurations in sbt is similar to scopes in Maven. sbt adds
dependencies to different classpaths by the configuration that they are defined
in. See the description of Maven Scopes for details.
You put a dependency in a configuration by selecting one or more of its con-
figurations to map to one or more of your project’s configurations. The most
common case is to have one of your configurations A use a dependency’s config-
uration B. The mapping for this looks like "A->B". To apply this mapping to a
dependency, add it to the end of your dependency definition:
This says that your project’s "test" configuration uses ScalaTest’s "compile"
configuration. See the Ivy documentation for more advanced mappings. Most
projects published to Maven repositories will use the "compile" configuration.
A useful application of configurations is to group dependencies that are not used
on normal classpaths. For example, your project might use a "js" configuration
287
to automatically download jQuery and then include it in your jar by modifying
resources. For example:
The config method defines a new configuration with name "js" and makes it
private to the project so that it is not used for publishing. See Update Report
for more information on selecting managed artifacts.
A configuration without a mapping (no "->") is mapped to "default" or
"compile". The -> is only needed when mapping to a different configuration
than those. The ScalaTest dependency above can then be shortened to:
External Maven or Ivy For this method, create the configuration files as you
would for Maven (pom.xml) or Ivy (ivy.xml and optionally ivysettings.xml).
External configuration is selected by using one of the following expressions.
externalIvySettings()
or
externalIvySettings(baseDirectory.value / "custom-settings-name.xml")
or
externalIvySettingsURL(url("your_url_here"))
externalIvyFile()
or
externalIvyFile(Def.setting(baseDirectory.value / "custom-name.xml"))
288
Because Ivy files specify their own configurations, sbt needs to know which con-
figurations to use for the compile, runtime, and test classpaths. For example,
to specify that the Compile classpath should use the ‘default’ configuration:
externalPom()
or
externalPom(Def.setting(baseDirectory.value / "custom-name.xml"))
Full Ivy Example For example, a build.sbt using external Ivy files might
look like:
externalIvySettings()
externalIvyFile(Def.setting(baseDirectory.value / "ivyA.xml"))
289
Known limitations Maven support is dependent on Ivy’s support for Maven
POMs. Known issues with this support:
Proxy Repositories
It’s often the case that users wish to set up a maven/ivy proxy repository in-
side their corporate firewall, and have developer sbt instances resolve artifacts
through such a proxy. Let’s detail what exact changes must be made for this
to work.
Overview
The situation arises when many developers inside an organization are attempting
to resolve artifacts. Each developer’s machine will hit the internet and download
an artifact, regardless of whether or not another on the team has already done so.
Proxy repositories provide a single point of remote download for an organization.
In addition to control and security concerns, Proxy repositories are primarily
important for increased speed across a team.
There are many good proxy repository solutions out there, with the big three
being (in alphabetical order):
• Archiva
• Artifactory
• Nexus
Once you have a proxy repository installed and configured, then it’s time to
configure sbt for your needs. Read the note at the bottom about proxy issues
with ivy repositories.
sbt Configuration
sbt requires configuration in two places to make use of a proxy repository. The
first is the ~/.sbt/repositories file, and the second is the launcher script.
290
Figure 7: image
291
~/.sbt/repositories
The repositories file is an external configuration for the Launcher. The exact
syntax for the configuration file is detailed in the sbt Launcher Configuration.
Here’s an example config:
[repositories]
local
my-ivy-proxy-releases: http://repo.company.com/ivy-releases/, [organization]/[module]/(scal
my-maven-proxy-releases: http://repo.company.com/maven-releases/
Launcher Script The sbt launcher supports two configuration options that al-
low the usage of proxy repositories. The first is the sbt.override.build.repos
setting and the second is the sbt.repository.config setting.
-Dsbt.override.build.repos=true
292
sbt.repository.config If you are unable to create a ~/.sbt/repositories
file, due to user permission errors or for convenience of developers, you can
modify the sbt start script directly with the following:
-Dsbt.repository.config=<path-to-your-repo-file>
This is only necessary if users do not already have their own default repository
file.
The most common mistake made when setting up a proxy repository for sbt is
the attempting to merge both maven and ivy repositories into the same proxy
repository. While some repository managers will allow this, it’s not recom-
mended to do so.
Even if your company does not use ivy, sbt uses a custom layout to handle
binary compatibility constraints of its own plugins. To ensure that these are
resolved correctly, simple set up two virtual/proxy repositories, one for maven
and one for ivy.
Here’s an example setup:
Publishing
This page describes how to publish your project. Publishing consists of upload-
ing a descriptor, such as an Ivy file or Maven POM, and artifacts, such as a
jar or war, to a repository so that other projects can specify your project as a
dependency.
The publish action is used to publish your project to a remote repository.
To use publishing, you need to specify the repository to publish to and the
credentials to use. Once these are set up, you can run publish.
The publishLocal action is used to publish your project to a local Ivy repository.
You can then use this project from other projects on the same machine.
293
Figure 8: image
294
publishTo := Some(Resolver.file("file", new File( "path/to/my/maven-repo/releases" )) )
If you’re using Maven repositories you will also have to select the right repository
depending on your artifacts: SNAPSHOT versions go to the /snapshot repos-
itory while other versions go to the /releases repository. Doing this selection
can be done by using the value of the isSnapshot SettingKey:
publishTo := {
val nexus = "https://my.artifact.repo.net/"
if (isSnapshot.value)
Some("snapshots" at nexus + "content/repositories/snapshots")
else
Some("releases" at nexus + "service/local/staging/deploy/maven2")
}
Credentials
There are two ways to specify credentials for such a repository. The first is to
specify them inline:
The second and better way is to load them from a file, for example:
The credentials file is a properties file with keys realm, host, user, and
password. For example:
Cross-publishing
295
Published artifacts
By default, the main binary jar, a sources jar, and a API documentation jar
are published. You can declare other types of artifacts to publish and disable
or modify the default artifacts. See the Artifacts page for details.
pomExtra :=
<licenses>
<license>
<name>Apache 2</name>
<url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>
makePom adds to the POM any Maven-style repositories you have declared. You
can filter these by modifying pomRepositoryFilter, which by default excludes
local repositories. To instead only include local repositories:
There is also a pomPostProcess setting that can be used to manipulate the final
XML before it is written. It’s type is Node => Node.
Publishing Locally
The publishLocal command will publish to the local Ivy repository. By default,
this is in ${user.home}/.ivy2/local. Other projects on the same machine can
then list the project as a dependency. For example, if the SBT project you are
publishing has configuration parameters like:
296
name := "My Project"
organization := "org.me"
version := "0.1-SNAPSHOT"
The version number you select must end with SNAPSHOT, or you must change
the version number each time you publish. Ivy maintains a cache, and it stores
even local projects in that cache. If Ivy already has a version cached, it will
not check the local repository for updates, unless the version number matches
a changing pattern, and SNAPSHOT is one such pattern.
Resolvers
Maven
resolvers +=
"Sonatype OSS Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots"
This is the most common kind of user-defined resolvers. The rest of this page
describes how to define other types of repositories.
Predefined
297
• Resolver.sbtPluginRepo("releases") (or “snapshots”) This is
sbt Community Repository at https://repo.scala-sbt.org/scalasbt/
sbt-plugin-releases
• Resolver.bintrayRepo("owner", "repo") This is the Bintray reposi-
tory at https://dl.bintray.com/{[}owner{]}/{[}repo{]}/
• Resolver.jcenterRepo This is the Bintray JCenter repository at https:
//jcenter.bintray.com/
For example, to use the java.net repository, use the following setting in your
build definition:
resolvers += JavaNet1Repository
Predefined repositories will go under Resolver going forward so they are in one
place:
Resolver.sonatypeRepo("releases") // Or "snapshots"
Custom
sbt provides an interface to the repository types available in Ivy: file, URL, SSH,
and SFTP. A key feature of repositories in Ivy is using patterns to configure
repositories.
Construct a repository definition using the factory in sbt.Resolver for the
desired type. This factory creates a Repository object that can be further
configured. The following table contains links to the Ivy documentation for the
repository type and the API documentation for the factory and repository class.
The SSH and SFTP repositories are configured identically except for the name
of the factory. Use Resolver.ssh for SSH and Resolver.sftp for SFTP.
Type
Factory
Ivy Docs
Factory API
Repository Class API
Filesystem
Resolver.file
Ivy filesystem
filesystem factory
FileRepository API
298
SFTP
Resolver.sftp
Ivy sftp
sftp factory
SftpRepository API
SSH
Resolver.ssh
Ivy ssh
ssh factory
SshRepository API
URL
Resolver.url
Ivy url
url factory
URLRepository API
Basic Examples These are basic examples that use the default Maven-style
repository layout.
or customize the layout pattern described in the Custom Layout section below.
299
SFTP and SSH Repositories The following defines a repository that is
served by SFTP from host "example.org":
Authentication for the repositories returned by sftp and ssh can be configured
by the as methods.
To use password authentication:
resolvers += {
val keyFile: File = ...
Resolver.ssh("my-ssh-repo", "example.org") as("user", keyFile, "keyFilePassword")
}
300
Custom Layout These examples specify custom repository layouts using pat-
terns. The factory methods accept an Patterns instance that defines the pat-
terns to use. The patterns are first resolved against the base file or URL. The
default patterns give the default Maven-style layout. Provide a different Pat-
terns object to use a different layout. For example:
You can specify multiple patterns or patterns for the metadata and artifacts
separately. You can also specify whether the repository should be Maven com-
patible (as defined by Ivy). See the patterns API for the methods to use.
For filesystem and URL repositories, you can specify absolute patterns by omit-
ting the base URL, passing an empty Patterns instance, and using ivys and
artifacts:
Update Report
301
def matching(f: DependencyFilter): Seq[File]
Any argument to select may be omitted, in which case all values are allowed
for the corresponding component. For example, if the ConfigurationFilter
is not specified, all configurations are accepted. The individual filter types are
discussed below.
Filter Basics Configuration, module, and artifact filters are typically built
by applying a NameFilter to each component of a Configuration, ModuleID,
or Artifact. A basic NameFilter is implicitly constructed from a String, with
* interpreted as a wildcard.
import sbt._
// each argument is of type NameFilter
val mf: ModuleFilter = moduleFilter(organization = "*sbt*",
name = "main" | "actions", revision = "1.*" - "1.0")
import sbt._
// here the function value of type String => Boolean is implicitly converted to a NameFilter
val nf: NameFilter = (s: String) => s.startsWith("dispatch-")
302
ConfigurationFilter A configuration filter essentially wraps a NameFilter
and is explicitly constructed by the configurationFilter method:
import sbt._
val a: ConfigurationFilter = Set("compile", "test")
val b: ConfigurationFilter = (c: String) => c.startsWith("r")
val c: ConfigurationFilter = a | b
import sbt._
val a: ModuleFilter = moduleFilter(name = "dispatch-twitter", revision = "0.7.8")
val b: ModuleFilter = moduleFilter(name = "dispatch-*")
val c: ModuleFilter = b - a
303
def artifactFilter(name: NameFilter = ..., `type`: NameFilter = ...,
extension: NameFilter = ..., classifier: NameFilter = ...): ArtifactFilter
import sbt._
val a: ArtifactFilter = artifactFilter(classifier = "javadoc")
val b: ArtifactFilter = artifactFilter(`type` = "jar")
val c: ArtifactFilter = b - a
import sbt._
Here, we used && and || to combine individual component filters into a depen-
dency filter, which can then be provided to the UpdateReport.matches method.
Alternatively, the UpdateReport.select method may be used, which is equiv-
alent to calling matches with its arguments combined with &&.
Cached resolution
304
Setup
To set up cached resolution include the following setting in your project’s build:
updateOptions := updateOptions.value.withCachedResolution(true)
Dependency as a graph
Cached resolution
305
The actual speedup will depend case by case, but you should see significant
speedup if you have many subprojects. An initial report from a user showed
change from 260s to 25s. Your milage may vary.
Cached resolution is an experimental feature, and you might run into some
issues. When you see them please report to GitHub Issue or sbt-dev list.
First runs The first time you run cached resolution will likely be slow since
it needs to resolve all minigraphs and save the result into filesystem. Whenever
you add a new node the system has not seen, it will save the minigraph. The
second run onwards should be faster, but comparing full-resolution update with
second run onwards might not be a fair comparison.
Ivy fidelity is not guaranteed Some of the Ivy behavior doesn’t make sense,
especially around Maven emulation. For example, it seem to treat all transitive
dependencies introduced by Maven-published library as force() even when the
original pom.xml doesn’t say to:
306
There are also some issues around multiple dependencies to the same library
with different Maven classifiers. In these cases, reproducing the exact result as
normal update may not make sense or is downright impossible.
Motivation
sbt internally uses Apache Ivy to resolve library dependencies. While sbt has
benefited from not having to reinvent its own dependency resolution engine
all these years, we are increasingly seeing scalability challenges especially for
projects with both multiple subprojects and large dependency graph. There are
several factors involved in sbt’s resolution scalability:
Of the above factors, the one that has the most impact is the number of transi-
tive nodes.
1. The more nodes there are, the chances of version conflict increases. Con-
flicts are resolved typically by picking the latest version within the same
library.
2. The more nodes there are, the more it needs to backtrack to check for
exlusion and override rules.
Exclusion and override rules are applied transitively, so any time a new node is
introduced to the graph it needs to check its parent node’s rules, its grandparent
node’s rules, great-grandparent node’s rules, etc.
sbt treats configurations and subprojects to be independent dependency graph.
This allows us to include arbitrary libraries for different configurations and sub-
projects, but if the dependency resolution is slow, the linear scaling starts to
hurt. There have been prior efforts to cache the result of library dependencies,
but it still resulted in full resolution when libraryDependencies has changed.
307
Tasks and Commands
This part of the documentation has pages documenting particular sbt topics in
detail. Before reading anything in here, you will need the information in the
Getting Started Guide as a foundation.
Tasks
Tasks and settings are introduced in the getting started guide, which you may
wish to read first. This page has additional details and background and is
intended more as a reference.
Introduction
Both settings and tasks produce values, but there are two major differences
between them:
1. Settings are evaluated at project load time. Tasks are executed on demand,
often in response to a command from the user.
2. At the beginning of project loading, settings and their dependencies are
fixed. Tasks can introduce new tasks during execution, however.
Features
1. By integrating with the settings system, tasks can be added, removed, and
modified as easily and flexibly as settings.
2. Input Tasks use parser combinators to define the syntax for their argu-
ments. This allows flexible syntax and tab-completions in the same way
as Commands.
3. Tasks produce values. Other tasks can access a task’s value by calling
value on it within a task definition.
4. Dynamically changing the structure of the task graph is possible. Tasks
can be injected into the execution graph based on the result of another
task.
5. There are ways to handle task failure, similar to try/catch/finally.
6. Each task has access to its own Logger that by default persists the logging
for that task at a more verbose level than is initially printed to the screen.
308
Defining a Task
Run “sbt hello” from command line to invoke the task. Run “sbt tasks” to see
this task listed.
Define the key To declare a new task, define a lazy val of type TaskKey:
The name of the val is used when referring to the task in Scala code and at
the command line. The string passed to the taskKey method is a description
of the task. The type parameter passed to taskKey (here, Int) is the type of
value produced by the task.
We’ll define a couple of other keys for the examples:
Implement the task There are three main parts to implementing a task once
its key is defined:
1. Determine the settings and other tasks needed by the task. They are the
task’s inputs.
2. Define the code that implements the task in terms of these inputs.
3. Determine the scope the task will go in.
These parts are then combined just like the parts of a setting are combined.
309
Defining a basic task A task is defined using :=
intTask := 1 + 2
stringTask := System.getProperty("user.name")
sampleTask := {
val sum = 1 + 2
println("sum: " + sum)
sum
}
Tasks with inputs Tasks with other tasks or settings as inputs are also
defined using :=. The values of the inputs are referenced by the value method.
This method is special syntax and can only be called when defining a task, such
as in the argument to :=. The following defines a task that adds one to the
value produced by intTask and returns the result.
sampleTask := intTask.value + 1
Task Scope As with settings, tasks can be defined in a specific scope. For
example, there are separate compile tasks for the compile and test scopes.
The scope of a task is defined the same as for a setting. In the following example,
test:sampleTask uses the result of compile:intTask.
1. Assignment methods have the lowest precedence. These are methods with
names ending in =, except for !=, <=, >=, and names that start with =.
310
2. Methods starting with a letter have the next highest precedence.
3. Methods with names that start with a symbol and aren’t included in
1. have the highest precedence. (This category is divided further accord-
ing to the specific character it starts with. See the Scala specification
for details.)
Note that whenever .value is used, it must be within a task definition, such as
within Def.task above or as an argument to :=.
// initial definition
intTask := 3
Completely override a task by not declaring the previous task as an input. Each
of the definitions in the following example completely overrides the previous one.
That is, when intTask is run, it will only print #3.
311
intTask := {
println("#1")
3
}
intTask := {
println("#2")
5
}
intTask := {
println("#3")
sampleTask.value - 3
}
Introduction The general form of an expression that gets values from multi-
ple scopes is:
<setting-or-task>.all(<scope-filter>).value
Example A common scenario is getting the sources for all subprojects for
processing all at once, such as passing them to scaladoc. The task that we want
to obtain values for is sources and we want to get the values in all non-root
projects and in the Compile configuration. This looks like:
312
The next section describes various ways to construct a ScopeFilter.
Unspecified filters If the task filter is not specified, as in the example above,
the default is to select scopes without a specific task (global). Similarly, an
unspecified configuration filter will select scopes in the global configuration. The
project filter should usually be explicit, but if left unspecified, the current project
context will be used.
For example, the following selects the scope for the Compile and Test configu-
rations of the core project and the global configuration of the util project:
313
val filter: ScopeFilter =
ScopeFilter( inProjects(core), inConfigurations(Compile, Test)) ||
ScopeFilter( inProjects(util), inGlobalConfiguration )
More operations The all method applies to both settings (values of type
Initialize[T]) and tasks (values of type Initialize[Task[T]]). It returns a
setting or task that provides a Seq[T], as shown in this table:
Target
Result
Initialize[T]
Initialize[Seq[T]]
Initialize[Task[T]]
Initialize[Task[Seq[T]]]
This means that the all method can be combined with methods that construct
tasks and settings.
Missing values Some scopes might not define a setting or task. The ? and
?? methods can help in this case. They are both defined on settings and tasks
and indicate what to do when a key is undefined.
?
On a setting or task with underlying type T, this accepts no arguments and
returns a setting or task (respectively) of type Option[T]. The result is None if
the setting/task is undefined and Some[T] with the value if it is.
??
On a setting or task with underlying type T, this accepts an argument of type
T and uses this argument if the setting/task is undefined.
The following contrived example sets the maximum errors to be the maximum
of all aggregates of the current project.
maxErrors := {
// select the transitive aggregates for this project, but not the project itself
val filter: ScopeFilter =
ScopeFilter( inAggregates(ThisProject, includeRoot=false) )
// get the configured maximum errors in each selected scope,
// using 0 if not defined in a scope
val allVersions: Seq[Int] =
(maxErrors ?? 0).all(filter).value
allVersions.max
}
314
Multiple values from multiple scopes The target of all is any task or
setting, including anonymous ones. This means it is possible to get multiple
values at once without defining a new task or setting in each scope. A common
use case is to pair each value obtained with the project, configuration, or full
scope it came from.
For example, the following defines a task that prints non-Compile configurations
that define sbt plugins. This might be used to identify an incorrectly configured
build (or not, since this is a fairly contrived example):
checkPluginsTask := {
val oddPlugins: Seq[(String, Set[String])] =
pluginsWithConfig.all(filter).value
// Print each configuration that defines sbt plugins
for( (config, plugins) <- oddPlugins if plugins.nonEmpty )
println(s"$config defines sbt plugins: ${plugins.mkString(", ")}")
}
The examples in this section use the task keys defined in the previous section.
315
Streams: Per-task logging Per-task loggers are part of a more general sys-
tem for task-specific data called Streams. This allows controlling the verbosity
of stack traces and logging individually for tasks as well as recalling the last
logging for a task. Tasks also have access to their own persisted binary or text
data.
To use Streams, get the value of the streams task. This is a special task that
provides an instance of TaskStreams for the defining task. This type provides
access to named binary and text streams, named loggers, and a default logger.
The default Logger, which is the most commonly used aspect, is obtained by
the log method:
myTask := {
val s: TaskStreams = streams.value
s.log.debug("Saying hi...")
s.log.info("Hello!")
}
traceLevel in myTask := 5
To obtain the last logging output from a task, use the last command:
$ last myTask
[debug] Saying hi...
[info] Hello!
It can be useful to use the result of a task to determine the next tasks to evaluate.
This is done using Def.taskDyn. The result of taskDyn is called a dynamic task
because it introduces dependencies at runtime. The taskDyn method supports
the same syntax as Def.task and := except that you return a task instead of a
plain value.
For example,
316
val dynamic = Def.taskDyn {
// decide what to evaluate based on the value of `stringTask`
if(stringTask.value == "dev")
// create the dev-mode task: this is only evaluated if the
// value of stringTask is "dev"
Def.task {
3
}
else
// create the production task: only evaluated if the value
// of the stringTask is not "dev"
Def.task {
intTask.value + 5
}
}
myTask := {
val num = dynamic.value
println(s"Number selected was $num")
}
Using Def.sequential
317
To call this task type in compilecheck from the shell. If the compilation fails,
compilecheck would stop the execution.
root> compilecheck
[info] Compiling 1 Scala source to /Users/x/proj/target/scala-2.10/classes...
[error] /Users/x/proj/src/main/scala/Foo.scala:3: Unmatched closing brace '}' ignored here
[error] }
[error] ^
[error] one error found
[error] (compile:compileIncremental) Compilation failed
Handling Failure
This section discusses the failure, result, and andFinally methods, which
are used to handle failure of other tasks.
failure The failure method creates a new task that returns the Incomplete
value when the original task fails to complete normally. If the original task
succeeds, the new task fails. Incomplete is an exception with information about
any tasks that caused the failure and any underlying exceptions thrown during
task execution.
For example:
intTask := error("Failed.")
intTask := {
println("Ignoring failure: " + intTask.failure.value)
3
}
This overrides the intTask so that the original exception is printed and the
constant 3 is returned.
failure does not prevent other tasks that depend on the target from failing.
Consider the following example:
318
The following table lists the results of each task depending on the initially in-
voked task:
invoked task
intTask result
aTask result
bTask result
cTask result
overall result
intTask
failure
not run
not run
not run
failure
aTask
failure
success
not run
not run
success
bTask
failure
not run
failure
not run
failure
cTask
failure
success
failure
failure
319
failure
intTask
success
not run
not run
not run
success
aTask
success
failure
not run
not run
failure
bTask
success
not run
success
not run
success
cTask
success
failure
success
failure
failure
The overall result is always the same as the root task (the directly invoked task).
A failure turns a success into a failure, and a failure into an Incomplete. A
normal task definition fails when any of its inputs fail and computes its value
otherwise.
320
result The result method creates a new task that returns the full
Result[T] value for the original task. Result has the same structure as
Either[Incomplete, T] for a task result of type T. That is, it has two
subtypes:
Thus, the task created by result executes whether or not the original task
succeeds or fails.
For example:
intTask := error("Failed.")
This overrides the original intTask definition so that if the original task fails,
the exception is printed and the constant 3 is returned. If it succeeds, the value
is printed and returned.
andFinally The andFinally method defines a new task that runs the original
task and evaluates a side effect regardless of whether the original task succeeded.
The result of the task is the result of the original task. For example:
intTask := intTaskImpl.value
This modifies the original intTask to always print “andFinally” even if the task
fails.
Note that andFinally constructs a new task. This means that the new task has
to be invoked in order for the extra block to run. This is important when calling
andFinally on another task instead of overriding a task like in the previous
example. For example, consider this code:
321
intTask := error("I didn't succeed.")
otherIntTask := intTaskImpl.value
intTask()
It is obvious here that calling intTask() will never result in “finally” being
printed.
Input Tasks
Input Tasks parse user input and produce a task to run. Parsing Input describes
how to use the parser combinators that define the input syntax and tab comple-
tion. This page describes how to hook those parser combinators into the input
task system.
Input Keys
A key for an input task is of type InputKey and represents the input task like a
SettingKey represents a setting or a TaskKey represents a task. Define a new
input task key using the inputKey.apply factory method:
The definition of an input task is similar to that of a normal task, but it can
also use the result of a
Parser applied to user input. Just as the special value method gets the value
of a setting or task, the special parsed method gets the result of a Parser.
322
Basic Input Task Definition
import complete.DefaultParsers._
demo := {
// get the result of parsing
val args: Seq[String] = spaceDelimited("<arg>").parsed
// Here, we also use the value of the `scalaVersion` setting
println("The current Scala version is " + scalaVersion.value)
println("The arguments to demo were:")
args foreach println
}
The Parser provided by the spaceDelimited method does not provide any flex-
ibility in defining the input syntax. Using a custom parser is just a matter of
defining your own Parser as described on the Parsing Input page.
Constructing the Parser The first step is to construct the actual Parser
by defining a value of one of the following types:
We already saw an example of the first case with spaceDelimited, which doesn’t
use any settings in its definition. As an example of the third case, the following
defines a contrived Parser that uses the project’s Scala and sbt version set-
tings as well as the state. To use these settings, we need to wrap the Parser
construction in Def.setting and get the setting values with the special value
method:
323
import complete.DefaultParsers._
This Parser definition will produce a value of type (String,String). The input
syntax defined isn’t very flexible; it is just a demonstration. It will produce one
of the following values for a successful parse (assuming the current Scala version
is 2.10.6, the current sbt version is 0.13.16, and there are 3 commands left to
run):
Again, we were able to access the current Scala and sbt version for the project
because they are settings. Tasks cannot be used to define the parser.
Constructing the Task Next, we construct the actual task to execute from
the result of the Parser. For this, we define a task as usual, but we can access
the result of parsing via the special parsed method on Parser.
The following contrived example uses the previous example’s output (of type
(String,String)) and the result of the package task to print some information
to the screen.
demo := {
val (tpe, value) = parser.parsed
println("Type: " + tpe)
println("Value: " + value)
println("Packaged: " + packageBin.value.getAbsolutePath)
}
324
1. You can use other settings (via Initialize) to construct an input task.
2. You can use the current State to construct the parser.
3. The parser accepts user input and provides tab completion.
4. The parser produces the task to run.
So, you can use settings or State to construct the parser that defines an input
task’s command line syntax. This was described in the previous section. You
can then use settings, State, or user input to construct the task to run. This
is implicit in the input task syntax.
run2 := {
val one = (run in Compile).evaluated
val sep = separator.parsed
val two = (run in Compile).evaluated
}
For a main class Demo that echoes its arguments, this looks like:
325
$ sbt
> run2 a b -- c d
[info] Running Demo c d
[info] Running Demo a b
c
d
a
b
Preapplying input
• partialInput applies the input and allows further input, such as from
the command line
• fullInput applies the input and terminates parsing, so that further input
is not accepted
In each case, the input is applied to the input task’s parser. Because input tasks
handle all input after the task name, they usually require initial whitespace to
be provided in the input.
Consider the example in the previous section. We can modify it so that we:
• Explicitly specify all of the arguments to the first run. We use name and
version to show that settings can be used to define and modify parsers.
• Define the initial arguments passed to the second run, but allow further
input on the command line.
// The argument string for the first run task is ' <name> <version>'
lazy val firstInput: Initialize[String] =
Def.setting(s" ${name.value} ${version.value}")
326
// Make the first arguments to the second run task ' red blue'
lazy val secondInput: String = " red blue"
run2 := {
val one = (run in Compile).fullInput(firstInput.value).evaluated
val two = (run in Compile).partialInput(secondInput).evaluated
}
For a main class Demo that echoes its arguments, this looks like:
$ sbt
> run2 green
[info] Running Demo demo 1.0
[info] Running Demo red blue green
demo
1.0
red
blue
green
The previous section showed how to derive a new InputTask by applying in-
put. In this section, applying input produces a Task. The toTask method on
Initialize[InputTask[T]] accepts the String input to apply and produces a
task that can be used normally. For example, the following defines a plain task
runFixed that can be used by other tasks or run directly without providing any
input:
lazy val runFixed = taskKey[Unit]("A task that hard codes the values to `run`")
runFixed := {
val _ = (run in Compile).toTask(" blue green").value
println("Done!")
}
For a main class Demo that echoes its arguments, running runFixed looks like:
$ sbt
> runFixed
[info] Running Demo blue green
blue
green
Done!
327
Each call to toTask generates a new task, but each task is configured the same
as the original InputTask (in this case, run) but with different input applied.
For example:
lazy val runFixed2 = taskKey[Unit]("A task that hard codes the values to `run`")
runFixed2 := {
val x = (run in Compile).toTask(" blue green").value
val y = (run in Compile).toTask(" red orange").value
println("Done!")
}
The different toTask calls define different tasks that each run the project’s main
class in a new jvm. That is, the fork setting configures both, each has the same
classpath, and each run the same main class. However, each task passes different
arguments to the main class. For a main class Demo that echoes its arguments,
the output of running runFixed2 might look like:
$ sbt
> runFixed2
[info] Running Demo blue green
[info] Running Demo red orange
blue
green
red
orange
Done!
Commands
What is a “command”?
328
Introduction
In sbt, the syntax part, including tab completion, is specified with parser com-
binators. If you are familiar with the parser combinators in Scala’s standard
library, these are very similar. The action part is a function (State, T) =>
State, where T is the data structure produced by the parser. See the Parsing
Input page for how to use the parser combinators.
State provides access to the build state, such as all registered Commands, the
remaining commands to execute, and all project-related information. See States
and Actions for details on State.
Finally, basic help information may be provided that is used by the help com-
mand to display command help.
Defining a Command
329
Single-argument command There is a convenience method for constructing
commands that accept a single argument with arbitrary content.
Full Example
Here’s build.sbt:
import CommandExample._
Here’s project/CommandExample.scala:
330
import sbt._
import Keys._
object CommandExample {
// A simple, no-argument command that prints "Hi",
// leaving the current state unchanged.
def hello = Command.command("hello") { state =>
println("Hi!")
state
}
331
println()
state
}
This page describes the parser combinators in sbt. These parser combinators are
typically used to parse user input and provide tab completion for Input Tasks
and Commands. If you are already familiar with Scala’s parser combinators,
the methods are mostly the same except that their arguments are strict. There
are two additional methods for controlling tab completion that are discussed at
the end of the section.
Parser combinators build up a parser from smaller parsers. A Parser[T] in its
most basic usage is a function String => Option[T]. It accepts a String to
parse and produces a value wrapped in Some if parsing succeeds or None if it
fails. Error handling and tab completion make this picture more complicated,
but we’ll stick with Option for this discussion.
The following examples assume the imports: :
import sbt._
import complete.DefaultParsers._
332
Basic parsers
// A parser that succeeds if the input is 'x', returning the Char 'x'
// and failing otherwise
val singleChar: Parser[Char] = 'x'
// A parser that succeeds if the input is "blue", returning the String "blue"
// and failing otherwise
val litString: Parser[String] = "blue"
// A parser that succeeds if the character is a digit, returning the matched Char
// The second argument, "digit", describes the parser and is used in error messages
val digit: Parser[Char] = charClass( (c: Char) => c.isDigit, "digit")
// A parser that produces the value 3 for an empty input string, fails otherwise
val alwaysSucceed: Parser[Int] = success( 3 )
Built-in parsers
333
Combining parsers
// A parser that matches "fg" or "bg", a space, and then the color, returning the matched va
// ~ is an alias for Tuple2.
val setColor: Parser[String ~ Char ~ String] =
select ~ ' ' ~ color
// Often, we don't care about the value matched by a parser, such as the space above
// For this, we can use ~> or <~, which keep the result of
// the parser on the right or left, respectively
val setColor2: Parser[String ~ String] = select ~ (' ' ~> color)
Transforming results
A key aspect of parser combinators is transforming results along the way into
more useful data structures. The fundamental methods for this are map and
flatMap. Here are examples of map and some convenience methods implemented
on top of map.
// Apply the `digits` parser and apply the provided function to the matched
// character sequence
val num: Parser[Int] = digits map { (chars: Seq[Char]) => chars.mkString.toInt }
// Match a digit character, returning the matched character or return '0' if the input is no
val digitWithDefault: Parser[Char] = charClass(_.isDigit, "digit") ?? '0'
334
// The previous example is equivalent to:
val digitDefault: Parser[Char] =
charClass(_.isDigit, "digit").? map { (d: Option[Char]) => d getOrElse '0' }
Most parsers have reasonable default tab completion behavior. For example,
the string and character literal parsers will suggest the underlying literal for
an empty input string. However, it is impractical to determine the valid com-
pletions for charClass, since it accepts an arbitrary predicate. The examples
method defines explicit completions for such a parser:
Tab completion will use the examples as suggestions. The other method con-
trolling tab completion is token. The main purpose of token is to determine
the boundaries for suggestions. For example, if your parser is:
then the potential completions on empty input are: console fg green fg blue
bg green bg blue
Typically, you want to suggest smaller segments or the number of suggestions
becomes unmanageable. A better parser is:
335
Dependent parsers
Sometimes a parser must analyze some data and then more data needs to be
parsed, and it is dependent on the previous one.
The key for obtaining this behaviour is to use the flatMap function.
As an example, it will shown how to select several items from a list of valid ones
with completion, but no duplicates are possible. A space is used to separate the
different items.
As you can see, the flatMap function provides the previous value. With this
info, a new parser is constructed for the remaining items. The map combinator
is also used in order to transform the output of the parser.
The parser is called recursively, until it is found the trivial case of no possible
choices.
State is the entry point to all available information in sbt. The key methods
are:
The action part of a command performs work and transforms State. The follow-
ing sections discuss State => State transformations. As mentioned previously,
a command will typically handle a parsed value as well: (State, T) => State.
336
Command-related data
This takes the current commands, appends new commands, and drops dupli-
cates. Alternatively, State has a convenience method for doing the above:
The first adds a command that will run after all currently specified commands
run. The second inserts a command that will run next. The remaining com-
mands will run after the inserted command completes.
To indicate that a command has failed and execution should not continue, return
state.fail.
337
Project-related data
Extracted provides:
Project data
Here, a SettingKey[T] is typically obtained from Keys and is the same type
that is used to define settings in .sbt files, for example. Scope selects the scope
the key is obtained for. There are convenience overloads of in that can be used
to specify only the required scope axes. See Structure.scala for where in and
other parts of the settings interface are defined. Some examples:
import Keys._
val extracted: Extracted
import extracted._
// get the package options for the `test:packageSrc` task or Nil if none are defined
val pkgOpts: Seq[PackageOption] = packageOptions in (currentRef, Test, packageSrc) get struc
338
BuildStructure contains information about build and project relationships. Key
members are:
A URI identifies a build and root identifies the initial build loaded. Load-
edBuildUnit provides information about a single build. The key members of
LoadedBuildUnit are:
Classpaths
Running tasks
It can be useful to run a specific project task from a command (not from another
task) and get its result. For example, an IDE-related command might want to get
the classpath from a project or a task might analyze the results of a compilation.
The relevant method is Project.runTask, which has the following signature:
For example,
339
val eval: State => State = (state: State) => {
// This selects the main 'compile' task for the current project.
// The value produced by 'compile' is of type inc.Analysis,
// which contains information about the compiled code.
val taskKey = Keys.compile in Compile
For getting the test classpath of a specific project, use this key:
To access the current State from a task, use the state task as an input. For
example,
Tasks/Settings: Motivation
This page motivates the task and settings system. You should already know
how to use tasks and settings, which are described in the getting started guide
and on the Tasks page.
An important aspect of the task system is to combine two common, related
steps in a build:
340
2. Use some result from that task.
1. Dependency declarations
2. Some form of shared state
makeFoo()
doSomething(foo)
This example is rather exaggerated in its badness, but I claim it is nearly the
same situation as our two step task definitions. Particular reasons this is bad
include:
The first point is like declaring a task dependency, the second is like two tasks
modifying the same state (either project variables or files), and the third is a
consequence of unsynchronized, shared state.
In Scala, we have the built-in functionality to easily fix this: lazy val.
doSomething(foo)
341
Here, lazy val gives us thread safety, guaranteed initialization before access,
and immutability all in one, DRY construct. The task system in sbt does the
same thing for tasks (and more, but we won’t go into that here) that lazy val
did for our bad example.
A task definition must declare its inputs and the type of its output. sbt will
ensure that the input tasks have run and will then provide their results to the
function that implements the task, which will generate its own result. Other
tasks can use this result and be assured that the task has run (once) and be
thread-safe and typesafe in the process.
The general form of a task definition looks like:
myTask := {
val a: A = aTask.value
val b: B = bTask.value
... do something with a, b and generate a result ...
}
(This is only intended to be a discussion of the ideas behind tasks, so see the sbt
Tasks page for details on usage.) Here, aTask is assumed to produce a result of
type A and bTask is assumed to produce a result of type B.
Application
As an example, consider generating a zip file containing the binary jar, source jar,
and documentation jar for your project. First, determine what tasks produce the
jars. In this case, the input tasks are packageBin, packageSrc, and packageDoc
in the main Compile scope. The result of each of these tasks is the File for the
jar that they generated. Our zip file task is defined by mapping these package
tasks and including their outputs in a zip file. As good practice, we then return
the File for this zip so that other tasks can map on the zip task.
zip := {
val bin: File = (packageBin in Compile).value
val src: File = (packageSrc in Compile).value
val doc: File = (packageDoc in Compile).value
val out: File = zipPath.value
val inputs: Seq[(File,String)] = Seq(bin, src, doc) x Path.flat
IO.zip(inputs, out)
out
}
The val inputs line defines how the input files are mapped to paths in the
zip. See Mapping Files for details. The explicit types are not required, but are
included for clarity.
342
The zipPath input would be a custom task to define the location of the zip file.
For example:
This part of the documentation has pages documenting particular sbt topics in
detail. Before reading anything in here, you will need the information in the
Getting Started Guide as a foundation.
Anything that is necessary for building the project should go in project/. This
includes things like the web plugin. ~/.sbt/ should contain local customizations
and commands for working with a build, but are not necessary. An example is
an IDE plugin.
Local settings
There are two options for settings that are specific to a user. An example of
such a setting is inserting the local Maven repository at the beginning of the
resolvers list:
resolvers := {
val localMaven = "Local Maven Repository" at "file://"+Path.userHome.absolutePath+"/.m2/re
localMaven +: resolvers.value
}
343
.sbtrc
Put commands to be executed when sbt starts up in a .sbtrc file, one per
line. These commands run before a project is loaded and are useful for defining
aliases, for example. sbt executes commands in $HOME/.sbtrc (if it exists) and
then <project>/.sbtrc (if it exists).
Generated files
Don’t hard code constants, like the output directory target/. This is especially
important for plugins. A user might change the target setting to point to
build/, for example, and the plugin needs to respect that. Instead, use the
setting, like:
344
// define a task that creates a file,
// writes some content, and returns the File
makeFile := {
val f: File = file("/tmp/data.txt")
IO.write(f, "Some content")
f
}
This arrangement is not always possible, but it should be the rule and not the
exception.
file("/home/user/A.scala")
base / "A.scala"
This is related to the no hard coding best practice because the proper way
involves referencing the baseDirectory setting. For example, the following
defines the myPath setting to be the <base>/licenses/ directory.
In Java (and thus in Scala), a relative File is relative to the current working di-
rectory. The working directory is not always the same as the build root directory
for a number of reasons.
The only exception to this rule is when specifying the base directory for a Project.
Here, sbt will resolve a relative File against the build root directory for you for
convenience.
345
Parser combinators
Plugins
There’s a getting started page focused on using existing plugins, which you may
want to read first.
A plugin is a way to use external code in a build definition. A plugin can
be a library used to implement a task (you might use Knockoff to write a
markdown processing task). A plugin can define a sequence of sbt settings that
are automatically added to all projects or that are explicitly declared for selected
projects. For example, a plugin might add a proguard task and associated
(overridable) settings. Finally, a plugin can define new commands (via the
commands setting).
sbt 0.13.5 introduces auto plugins, with improved dependency management
among the plugins and explicitly scoped auto importing. Going forward, our
recommendation is to migrate to the auto plugins. The Plugins Best Practices
page describes the currently evolving guidelines to writing sbt plugins. See also
the general best practices.
346
Alternatively, you can create project/plugins.sbt with all of the desired sbt
plugins, any general dependencies, and any necessary repositories:
// plain library (not an sbt plugin) for use in the build definition
libraryDependencies += "org.example" % "utilities" % "1.3"
Many of the auto plugins automatically add settings into projects, however,
some may require explicit enablement. Here’s an example:
See using plugins in the Getting Started guide for more details on using plugins.
By Description
347
The build definition classpath is searched for sbt/sbt.plugins descriptor files
containing the names of sbt.AutoPlugin or sbt.Plugin implementations.
The reload plugins command changes the current build to the (root) project’s
project/ build definition. This allows manipulating the build definition project
like a normal project. reload return changes back to the original build. Any
session settings for the plugin definition project that have not been saved are
dropped.
An auto plugin is a module that defines settings to automatically inject into
projects. In addition an auto plugin provides the following feature:
• Automatically import selective names to .sbt files and the eval and set
commands.
• Specify plugin dependencies to other auto plugins.
• Automatically activate itself when all dependencies are present.
• Specify projectSettings, buildSettings, and globalSettings as ap-
propriate.
Plugin dependencies
1. add the setting sequence from the dependency as part of its own setting
sequence, or
2. tell the build users to include them in the right order.
This will pull in the right setting sequence from the plugins in the right order.
The key notion here is you declare the plugins you want, and sbt can fill in the
gap.
348
A plugin implementation is not required to produce an auto plugin, however. It
is a convenience for plugin consumers and because of the automatic nature, it
is not always appropriate.
A minimal sbt plugin is a Scala library that is built against the version of
Scala that sbt runs (currently, 2.10.6) or a Java library. Nothing special needs
to be done for this type of library. A more typical plugin will provide sbt
tasks, commands, or settings. This kind of plugin may provide these settings
automatically or make them available for the user to explicitly integrate.
To make an auto plugin, create a project and configure sbtPlugin to true.
sbtPlugin := true
Then, write the plugin code and publish your project to a repository. The plugin
can be used as described in the previous section.
First, in an appropriate namespace, define your auto plugin object by extending
sbt.AutoPlugin.
package sbthello
import sbt._
import Keys._
349
This example demonstrates how to take a Command (here, helloCommand) and
distribute it in a plugin. Note that multiple commands can be included in one
plugin (for example, use commands ++= Seq(a,b)). See Commands for defining
more useful commands, including ones that accept arguments and affect the
execution state.
If the plugin needs to append settings at the build-level (that is, in ThisBuild)
there’s a buildSettings method. The settings returned here are guaranteed to
be added to a given build scope only once regardless of how many projects for
that build activate this AutoPlugin.
package sbtless
import sbt._
import Keys._
object SbtLessPlugin extends AutoPlugin {
override def requires = SbtJsTaskPlugin
override lazy val projectSettings = ...
}
The requires method returns a value of type Plugins, which is a DSL for
constructing the dependency list. The requires method typically contains one
of the following values:
350
Root plugins and triggered plugins Some plugins should always be ex-
plicitly enabled on projects. we call these root plugins, i.e. plugins that are
“root” nodes in the plugin dependency graph. An auto plugin is by default a
root plugin.
Auto plugins also provide a way for plugins to automatically attach themselves
to projects if their dependencies are met. We call these triggered plugins, and
they are created by overriding the trigger method.
For example, we might want to create a triggered plugin that can append
commands automatically to the build. To do this, set the requires method
to return empty (this is the default), and override the trigger method with
allRequirements.
package sbthello
import sbt._
import Keys._
The build user still needs to include this plugin in project/plugins.sbt, but
it is no longer needed to be included in build.sbt. This becomes more in-
teresting when you do specify a plugin with requirements. Let’s modify the
SbtLessPlugin so that it depends on another plugin:
package sbtless
import sbt._
import Keys._
object SbtLessPlugin extends AutoPlugin {
override def trigger = allRequirements
override def requires = SbtJsTaskPlugin
override lazy val projectSettings = ...
}
As it turns out, PlayScala plugin (in case you didn’t know, the Play framework
is an sbt plugin) lists SbtJsTaskPlugin as one of it required plugins. So, if we
define a build.sbt with:
351
lazy val root = (project in file("."))
.enablePlugins(PlayScala)
package sbthello
import sbt._
import Keys._
352
sbtPlugin := true
name := "sbt-obfuscate"
organization := "org.example"
ObfuscatePlugin.scala:
package sbtobfuscate
import sbt._
import sbt.Keys._
import autoImport._
override def requires = sbt.plugins.JvmPlugin
object Obfuscate {
def apply(sources: Seq[File], obfuscateLiterals: Boolean): Seq[File] = {
// TODO obfuscate stuff!
sources
353
}
}
Usage example A build definition that uses the plugin might look like.
obfuscate.sbt:
This plugin will be available for every sbt project for the current user.
In addition:
354
1a) Manually managed
$ sbt
> reload plugins
[info] Set current project to default (in build file:/Users/sbt/demo2/project/)
>
Then, we can add dependencies like usual and save them to project/plugins.sbt.
It is useful, but not required, to run update to verify that the dependencies are
correct.
355
1d) Project dependency This variant shows how to use sbt’s external
project support to declare a source dependency on a plugin. This means that
the plugin will be built from source and used on the classpath.
Edit project/plugins.sbt
One caveat to using this method is that the local sbt will try to run the remote
plugin’s build. It is quite possible that the plugin’s own build uses a different
sbt version, as many plugins cross-publish for several sbt versions. As such, it
is recommended to stick with binary artifacts when possible.
In a build.sbt file:
import grizzled.sys._
import OperatingSystem._
libraryDependencies ++=
if(os == Windows)
Seq("org.example" % "windows-only" % "1.0")
else
Seq.empty
356
Best Practices
If you’re a plugin writer, please consult the Plugins Best Practices page; it
contains a set of guidelines to help you ensure that your plugin is consistent and
plays well with other plugins.
This page is intended primarily for sbt plugin authors. This page assumes you’ve
read using plugins and Plugins.
A plugin developer should strive for consistency and ease of use. Specifically:
• Plugins should play well with other plugins. Avoiding namespace clashes
(in both sbt and Scala) is paramount.
• Plugins should follow consistent conventions. The experiences of an sbt
user should be consistent, no matter what plugins are pulled in.
Make sure people can find your plugin. Here are some of the recommended
steps:
Users who have their build files in some package will not be able to use your
plugin if it’s defined in default (no-name) package.
Use the sbt-$projectname scheme to name your library and artifact. A plugin
ecosystem with a consistent naming convention makes it easier for users to tell
whether a project or dependency is an SBT plugin.
If the project’s name is foobar the following holds:
357
• BAD: foobar
• BAD: foobar-sbt
• BAD: sbt-foobar-plugin
• GOOD: sbt-foobar
Your plugin should fit in naturally with the rest of the sbt ecosystem. The first
thing you can do is to avoid defining commands, and use settings and tasks
and task-scoping instead (see below for more on task-scoping). Most of the
interesting things in sbt like compile, test and publish are provided using
tasks. Tasks can take advantage of duplication reduction and parallel execution
by the task engine. With features like ScopeFilter, many of the features that
previously required commands are now possible using tasks.
Settings can be composed from other settings and tasks. Tasks can be composed
from other tasks and input tasks. Commands, on the other hand, cannot be
composed from any of the above. In general, use the minimal thing that you
need. One legitimate use of commands may be using plugin to access the build
definition itself not the code. sbt-inspectr was implemented using a command
before it became inspect tree.
Use sbt.AutoPlugin
sbt has a number of predefined keys. Where possible, reuse them in your plugin.
For instance, don’t define:
358
Avoid namespace clashes
Sometimes, you need a new key, because there is no existing sbt key. In this
case, use a plugin-specific prefix.
package sbtobfuscate
In this approach, every lazy val starts with obfuscate. A user of the plugin
would refer to the settings like this:
obfuscateStylesheet := file("something.txt")
Configuration advices
If your plugin introduces either a new set of source code or its own library
dependencies, only then you want your own configuration.
package sbtwhatever
359
object WhateverPlugin extends sbt.AutoPlugin {
override def requires = plugins.JvmPlugin
override def trigger = allRequirements
object autoImport {
// BAD sample
lazy val Whatever = config("whatever") extend(Compile)
lazy val dude = settingKey[String]("A plugin specific key")
}
import autoImport._
override lazy val projectSettings = Seq(
dude in Whatever := "your opinion man" // DON'T DO THIS
)
}
package sbtfuzz
object autoImport {
lazy val Fuzz = config("fuzz") extend(Compile)
}
import autoImport._
360
lazy val baseFuzzSettings: Seq[Def.Setting[_]] = Seq(
test := {
println("fuzz test")
}
)
override lazy val projectSettings = inConfig(Fuzz)(baseFuzzSettings)
}
Provide raw settings and configured settings Split your settings by the
configuration axis like so:
package sbtobfuscate
object autoImport {
lazy val obfuscate = taskKey[Seq[File]]("obfuscate the source")
lazy val obfuscateStylesheet = settingKey[File]("obfuscate stylesheet")
}
import autoImport._
lazy val baseObfuscateSettings: Seq[Def.Setting[_]] = Seq(
obfuscate := Obfuscate((sources in obfuscate).value),
sources in obfuscate := sources.value
)
override lazy val projectSettings = inConfig(Compile)(baseObfuscateSettings)
}
361
// core feature implemented here
object Obfuscate {
def apply(sources: Seq[File]): Seq[File] = {
sources
}
}
import sbtobfuscate.ObfuscatePlugin
Using a “main” task scope for settings Sometimes you want to define
some settings for a particular “main” task in your plugin. In this instance, you
can scope your settings using the task itself. See the baseObfuscateSettings:
In the above example, sources in obfuscate is scoped under the main task,
obfuscate.
There may be times when you need to muck with globalSettings. The general
rule is be careful what you touch.
When overriding global settings, care should be taken to ensure previous settings
from other plugins are not ignored. e.g. when creating a new onLoad handler,
ensure that the previous onLoad handler is not removed.
package sbtsomething
362
object MyPlugin extends AutoPlugin {
override def requires = plugins.JvmPlugin
override def trigger = allRequirements
Travis CI is a hosted continuous integration service for open source and private
projects. Many of the OSS projects hosted on GitHub uses open source edition
of Travis CI to validate pushes and pull requests. We’ll discuss some of the best
practices setting up Travis CI.
Set project/build.properties
Continuous integration is a great way of checking that your code works out-
side of your machine. If you haven’t created one already, make sure to create
project/build.properties and explicitly set the sbt.version number:
sbt.version=0.13.16
A treasure trove of Travis tricks can be found in the Travis’s official documen-
tation. Use this guide as an inspiration, but consult the official source for more
details.
Basic setup
language: scala
363
scala:
- 2.10.4
- 2.12.2
language: scala
scala:
- 2.10.4
- 2.12.2
script:
- sbt ++$TRAVIS_SCALA_VERSION test
For sbt plugins, there is no need for cross building on Scala, so the following is
all you need:
language: scala
script:
- sbt scripted
$ export JVM_OPTS=@/etc/sbt/jvmopts
$ export SBT_OPTS=@/etc/sbt/sbtopts
364
Custom JVM options
The default sbt and JVM options are set by Travis CI people, and it should work
for most cases. If you do decide to customize it, read what they currently use
as the defaults first. Because Travis is already using the environment variable
JVM_OPTS, we can instead create a file travis/jvmopts:
-Dfile.encoding=UTF8
-Xms2048M
-Xmx2048M
-Xss6M
-XX:MaxPermSize=512M
-XX:ReservedCodeCacheSize=256M
and then write out the script section with -jvm-opts option:
script:
- sbt ++$TRAVIS_SCALA_VERSION -jvm-opts travis/jvmopts test
After making the change, confirm on the Travis log to see if the flags are taking
effect:
script:
- sbt ++$TRAVIS_SCALA_VERSION -Dfile.encoding=UTF8 -J-XX:ReservedCodeCacheSize=256M -J-Xms10
365
# Executing command line:
/usr/lib/jvm/java-7-oracle/bin/java
-Xms2048M
-Xmx2048M
-Xss6M
-XX:MaxPermSize=512M
-Dfile.encoding=UTF8
-XX:ReservedCodeCacheSize=256M
-Xms1024M
-jar
/home/travis/.sbt/launchers/0.13.6/sbt-launch.jar
Note: This duplicates the -Xms flag as intended, which might not the best thing
to do.
Caching
1. start up faster
2. allow the use of caches for public repositories
3. disallow the use of sudo, setuid and setgid executables
366
before_cache:
# Cleanup the cached directories to avoid unnecessary cache updates
- find $HOME/.ivy2/cache -name "ivydata-*.properties" -print -delete
- find $HOME/.sbt -name "*.lock" -print -delete
With the above changes combined Travis CI will tar up the cached directories
and uploads them to Amazon S3. Overall, the use of the new infrastructure and
caching seems to shave off a few minutes of build time per job.
Note: The Travis documentation states caching features are still experimental.
Build matrix
language: scala
scala:
- 2.10.4
- 2.12.2
script:
- sbt ++$TRAVIS_SCALA_VERSION test
This is a form of a build matrix. Travis CI comes with variety of the ways to
run builds against different runtimes and parameters. Here’s how to build on
OpenJDK 6, OpenJDK 7, and Oracle JDK 8.
jdk:
- openjdk6
- openjdk7
- oraclejdk8
env:
global:
- SOME_VAR="1"
script:
- sbt "$TEST_COMMAND"
367
Now two jobs will be created to build this sbt plugin, simultaneously running
different integration tests. This technique is described in Parallelizing your
builds across virtual machines.
Notification
The default behavior looks reasonable, but if you want, we can override the
notifications section to email you on successful builds too, or to use some
other channel of communication like IRC.
This might also be a good time to read up on encryption using the command
line travis tool.
For builds that are more prone to flaky network or tests, Travis CI has created
some tricks described in the page My builds is timing out.
Starting your command with travis_retry retries the command three times if
the return code is non-zero. With caching, hopefully the effect of flaky network
is reduced, but it’s an interesting one nonetheless. Here are some cautionary
words from the documentation:
368
Another tidbit about Travis is the output timeout:
Our builds have a global timeout and a timeout that’s based on the
output. If no output is received from a build for 10 minutes, it’s
assumed to have stalled for unknown reasons and is subsequently
killed.
More things
There are more thing you can do, such as set up databases, installing Ubuntu
packages, and deploy continuously.
Sample setting
Here’s a sample that puts them all together. Remember, most of the sections
are optional.
language: scala
jdk:
- openjdk7
env:
# This splits the build into two parts
matrix:
- TEST_COMMAND="scripted sbt-assembly/*"
- TEST_COMMAND="scripted merging/* caching/*"
script:
369
- sbt ++$TRAVIS_SCALA_VERSION -Dfile.encoding=UTF8 -J-XX:ReservedCodeCacheSize=256M "$TEST_C
before_cache:
# Tricks to avoid unnecessary cache updates
- find $HOME/.sbt -name "*.lock" | xargs rm
- find $HOME/.ivy2 -name "ivydata-*.properties" | xargs rm
Let’s talk about testing. Once you write a plugin, it turns into a long-term
thing. To keep adding new features (or to keep fixing bugs), writing tests makes
sense.
sbt comes with scripted test framework, which lets you script a build scenario.
It was written to test sbt itself on complex scenarios – such as change detection
and partial compilation:
The framework is made available via scripted-plugin. The rest of this page
explains how to include the scripted-plugin into your plugin.
step 1: snapshot
Before you start, set your version to a -SNAPSHOT one because scripted-
plugin will publish your plugin locally. If you don’t use SNAPSHOT, you could
get into a horrible inconsistent state of you and the rest of the world seeing
different artifacts.
370
step 2: scripted-plugin
ScriptedPlugin.scriptedSettings
scriptedLaunchOpts := { scriptedLaunchOpts.value ++
Seq("-Xmx1024M", "-XX:MaxPermSize=256M", "-Dplugin.version=" + version.value)
}
scriptedBufferLog := false
step 3: src/sbt-test
In project/plugins.sbt:
sys.props.get("plugin.version") match {
case Some(x) => addSbtPlugin("com.eed3si9n" % "sbt-assembly" % x)
case _ => sys.error("""|The system property 'plugin.version' is not defined.
Specify this property using the scriptedLaunchOpts -D.""".stripMargin)
}
371
step 4: write a script
Now, write a script to describe your scenario in a file called test located at the
root dir of your test project.
So my script will run assembly task, and checks if foo.jar gets created. We’ll
cover more complex tests later.
> scripted
372
This will copy your test build into a temporary dir, and executes the test script.
If everything works out, you’d see publishLocal running, then:
The file commands are great, but not nearly enough because none of them test
the actual contents. An easy way to test the contents is to implement a custom
task in your test build.
For my hello project, I’d like to check if the resulting jar prints out “hello”. I
can take advantage of sbt.Process to run the jar. To express a failure, just
throw an error. Here’s build.sbt:
373
[error] x sbt-assembly / simple
[error] {line 6} Command failed: check failed
[error] {file:/Users/foo/work/sbt-assembly/}default-373f46/*:scripted: sbt-assembly / simple f
[error] Total time: 14 s, completed Sep 21, 2011 8:00:00 PM
Until you get the hang of it, it might take a while for the test itself to behave
correctly. There are several techniques that may come in handy.
First place to start is turning off the log buffering.
This for example should print out the location of the temporary dir:
Add the following line to your test script to suspend the test until you hit the
enter key:
$ pause
There are literally 100+ scripted tests under sbt project itself. Browse around
to get inspirations.
For example, here’s the one called by-name.
> compile
# Both A.scala and B.scala need to be recompiled because the type has changed
-> compile
374
sbt new and Templates
sbt 0.13.13 adds a new command called new, to create new build definitions
from a template. The new command is extensible via a mechanism called the
template resolver.
First, you need sbt’s launcher version 0.13.13 or above. Normally the exact
version for the sbt launcher does not matter because it will use the version
specified by sbt.version in project/build.properties; however for new
sbt’s launcher 0.13.13 or above is required as the command functions without a
project/build.properties present.
Next, run:
This ran the template scala/scala-seed.g8 using Giter8, prompted for values
for “name” (which has a default value of “hello”, which we accepted hitting
[Enter]), and created a build under ./hello.
scala-seed is the official template for a “minimal” Scala project, but it’s defi-
nitely not the only one out there.
Giter8 support
375
• akka/hello-akka.g8 (Simple Akka application)
• playframework/play-scala-seed.g8 (Play Scala Seed Template)
• playframework/play-java-seed.g8 (Play Java Seed template)
• lagom/lagom-scala.g8 (A Lagom Scala seed template for sbt)
• lagom/lagom-java.g8 (A Lagom Java seed template for sbt)
• scala-native/scala-native.g8 (Scala Native)
• scala-native/sbt-crossproject.g8 (sbt-crosspoject)
• http4s/http4s.g8 (http4s services)
• unfiltered/unfiltered.g8 (Unfiltered application)
• scalatra/scalatra-sbt.g8 (Basic Scalatra template using SBT 0.13.x.)
For more, see Giter8 templates on the Giter8 wiki. sbt provides out-of-the-box
support for Giter8 templates by shipping with a template resolver for Giter8.
How to create a Giter8 template See Making your own templates for the
details on how to create a new Giter8 template.
License
-------
Written in <YEAR> by <AUTHOR NAME> <AUTHOR E-MAIL ADDRESS>
[other author/contributor lines as appropriate]
To the extent possible under law, the author(s) have dedicated all copyright and related and neig
You should have received a copy of the CC0 Public Domain Dedication along with this software. If n
The rest of this page explains how to extend the sbt new command to provide
support for something other than Giter8 templates. You can skip this section
if you’re not interested in extending new.
376
Template Resolver A template resolver is a partial function that looks at
the arguments after sbt new and determines whether it can resolve to a partic-
ular template. This is analogous to resolvers resolving a ModuleID from the
Internet.
The Giter8TemplateResolver takes the first argument that does not start with
a hyphen (-), and checks whether it looks like a GitHub repo or a git repo that
ends in “.g8”. If it matches one of the patterns, it will pass the arguments to
Giter8 to process.
To create your own template resolver, create a library that has template-resolver
as a dependency:
package sbt.template;
import Def.Setting
import Keys._
/** An experimental plugin that adds the ability for Giter8 templates to be resolved
*/
object Giter8TemplatePlugin extends AutoPlugin {
override def requires = CorePlugin
override def trigger = allRequirements
377
Seq(
templateResolverInfos +=
TemplateResolverInfo(ModuleID("org.scala-sbt.sbt-giter8-resolver", "sbt-giter8-resol
"sbtgiter8resolver.Giter8TemplateResolver")
)
}
How to…
See Detailed Table of Contents for the list of all the how-tos.
Classpaths
The classpathTypes setting controls the types of managed artifacts that are
included on the classpath by default. To add a new type, such as mar,
classpathTypes += "mar"
See the default types included by running show classpathTypes at the sbt
prompt.
The dependencyClasspath task scoped to Compile provides the classpath to use
for compilation. Its type is Seq[Attributed[File]], which means that each
entry carries additional metadata. The files method provides just the raw
Seq[File] for the classpath. For example, to use the files for the compilation
classpath in another task, :
example := {
val cp: Seq[File] = (dependencyClasspath in Compile).value.files
...
}
Note: This classpath does not include the class directory, which
may be necessary for compilation in some situations.
378
Get the runtime classpath, including the project’s compiled classes
example := {
val cp: Seq[File] = (fullClasspath in Runtime).value.files
...
}
Get the test classpath, including the project’s compiled test classes
example := {
val cp: Seq[File] = (fullClasspath in Test).value.files
...
}
exportJars := true
This will use the result of packageBin on the classpath instead of the class
directory.
379
Note: Specifically, fullClasspath is the concatenation of dependen-
cyClasspath and exportedProducts. When exportJars is true, ex-
portedProducts is the output of packageBin. When exportJars is
false, exportedProducts is just products, which is by default the
directory containing class files and resources.
The result of the update task has type UpdateReport, which contains the results
of dependency resolution. This can be used to extract the files for specific types
of artifacts in a specific configuration. For example, to get the jars and zips of
dependencies in the Compile configuration, :
example := {
val artifactTypes = Set("jar", "zip")
val files: Seq[File] =
Classpaths.managedJars(Compile, artifactTypes, update.value)
...
}
A classpath has type Seq[Attributed[File]], which means that each entry car-
ries additional metadata. The files method provides just the raw Seq[File]
for the classpath. For example, :
A classpath has type Seq[Attributed[File]], which means that each entry car-
ries additional metadata. This metadata is in the form of an AttributeMap. Use-
ful keys for entries in the map are artifact.key, moduleID.key, and analysis.
For example,
380
Note: Entries may not have some or all metadata. Only entries from
source dependencies, such as internal projects, have an incremental
compilation Analysis. Only entries for managed dependencies have
an Artifact and ModuleID.
Customizing paths
This page describes how to modify the default source, resource, and library
directories and what files get included from them.
The directory that contains the main Scala sources is by default src/main/scala.
For test Scala sources, it is src/test/scala. To change this, modify
scalaSource in the Compile (for main sources) or Test (for test sources). For
example,
Note: The Scala source directory can be the same as the Java source
directory.
The directory that contains the main Java sources is by default src/main/java.
For test Java sources, it is src/test/java. To change this, modify javaSource
in the Compile (for main sources) or Test (for test sources).
For example,
Note: The Scala source directory can be the same as the Java source
directory.
381
Change the default resource directory
When set for Compile, Runtime, or Test, unmanagedBase is the directory con-
taining libraries for that configuration, overriding the default. For example,
the following declares lib/main/ to contain jars only for Compile and not for
running or testing: :
By default, sbt includes .scala files from the project’s base directory as main
source files. To disable this, configure sourcesInBase:
sourcesInBase := false
382
unmanagedSourceDirectories in Compile += baseDirectory.value / "extra-src"
To have different filters for main and test libraries, configure Compile and Test
separately:
383
Include/exclude files in the resource directory
To have different filters for main and test libraries, configure Compile and Test
separately:
Note: By default, sbt includes all files that are not hidden.
To have different filters for main and test libraries, configure Compile and Test
separately:
Note: By default, sbt includes jars, zips, and native dynamic li-
braries, excluding hidden files.
Generating files
sbt provides standard hooks for adding source and resource generation tasks.
384
Generate sources
> run
[info] Running Test
Hi
Change Compile to Test to make it a test source. For efficiency, you would only
want to generate sources when necessary and not every run.
By default, generated sources are not included in the packaged source artifact.
To do so, add them as you would other mappings. See Adding files to a package.
A source generator can return both Java and Scala sources mixed together in
the same sequence. They will be distinguished by their extension later.
385
Generate resources
Executing run (or package, not compile) will add a file demo to resourceManaged,
which is target/scala-*/resource_managed". By default, generated re-
sources are not included in the packaged source artifact. To do so, add them
as you would other mappings. See Adding files to a package.
As a specific example, the following generates a properties file myapp.properties
containing the application name and version:
Change Compile to Test to make it a test resource. Normally, you would only
want to generate resources when necessary and not every run.
386
Inspect the build
The help command is used to show available commands and search the help for
commands, tasks, or settings. If run without arguments, help lists the available
commands.
> help
The tasks command, without arguments, lists the most commonly used tasks.
It can take a regular expression to search task names and descriptions. The
verbosity can be increased to show or search less commonly used tasks. See
help tasks for details.
The settings command, without arguments, lists the most commonly used set-
tings. It can take a regular expression to search setting names and descriptions.
The verbosity can be increased to show or search less commonly used settings.
See help settings for details.
387
> inspect test:compile
...
[info] Dependencies:
[info] test:compile::compileInputs
[info] test:compile::streams
[info] Reverse dependencies:
[info] test:definedTestNames
[info] test:definedSbtPlugins
[info] test:printWarnings
[info] test:discoveredMainClasses
[info] test:definedTests
[info] test:exportedProducts
[info] test:products
...
For each task, inspect tree show the type of the value generated by the task.
For a setting, the toString of the setting is displayed. See the Inspecting
Settings page for details on the inspect command.
While the help, settings, and tasks commands display a description of a task,
the inspect command also shows the type of a setting or task and the value of
a setting. For example:
388
> inspect update
[info] Task: sbt.UpdateReport
[info] Description:
[info] Resolves and optionally retrieves dependencies, producing a report.
...
The projects command displays the currently loaded projects. The projects
are grouped by their enclosing build and the current project is indicated by an
asterisk. For example,
> projects
[info] In file:/home/user/demo/
[info] * parent
[info] sub
[info] In file:/home/user/dep/
[info] sample
389
Show the current session (temporary) settings
session list displays the settings that have been added at the command line
for the current project. For example,
session list-all displays the settings added for all projects. For details, see
help session.
> about
[info] This is sbt 0.12.0
[info] The current project is {file:~/code/sbt.github.com/}default
[info] The current project is built against Scala 2.9.2
[info] Available Plugins: com.jsuereth.ghpages.GhPages, com.jsuereth.git.GitPlugin, com.jsuere
[info] sbt, sbt plugins, and build definitions are using Scala 2.9.2
The inspect command shows the value of a setting as part of its output, but
the show command is dedicated to this job. It shows the output of the setting
provided as an argument. For example,
390
The show command will execute the task provided as an argument and then
print the result. Note that this is different from the behavior of the inspect
command (described in other sections), which does not execute a task and thus
can only display its type and not its generated value.
sbt detects the classes with public, static main methods for use by the run
method and to tab-complete the runMain method. The discoveredMainClasses
task does this discovery and provides as its result the list of class names. For
example, the following shows the main classes discovered in the main sources:
391
Interactive mode
Use tab completion
> tes<TAB>
> test
> test<TAB>
testFrameworks testListeners testLoader testOnly testOptions test:
Now, there is more than one possibility for the next character, so sbt prints the
available options. We will select testOnly and get more suggestions by entering
the rest of the command and hitting tab twice:
> testOnly<TAB><TAB>
-- sbt.DagSpecification sbt.EmptyRelationTest sbt.KeyTest sbt.Relati
The first tab inserts an unambiguous space and the second suggests names of
tests to run. The suggestion of -- is for the separator between test names and
options provided to the test framework. The other suggestions are names of
test classes for one of sbt’s modules. Test name suggestions require tests to be
compiled first. If tests have been added, renamed, or removed since the last
test compilation, the completions will be out of date until another successful
compile.
Some commands have different levels of completion. Hitting tab multiple times
increases the verbosity of completions. (Presently, this feature is only used by
the set command.)
392
Modify the default JLine keybindings
JLine, used by both Scala and sbt, uses a configuration file for many of its
keybindings. The location of this file can be changed with the system property
jline.keybindings. The default keybindings file is included in the sbt launcher
and may be used as a starting point for customization.
By default, sbt only displays > to prompt for a command. This can be changed
through the shellPrompt setting, which has type State => String. State
contains all state for sbt and thus provides access to all build information for
use in the prompt string.
Examples:
// set the prompt (for this build) to include the project id.
shellPrompt in ThisBuild := { state => Project.extract(state).currentRef.project + "> " }
// set the prompt (for the current project) to include the username
shellPrompt := { state => System.getProperty("user.name") + "> " }
Use history
Interactive mode remembers history even if you exit sbt and restart it. The
simplest way to access history is to press the up arrow key to cycle through
previously entered commands. Use Ctrl+r to incrementally search history back-
wards. The following commands are supported:
By default, interactive history is stored in the target/ directory for the current
project (but is not removed by a clean). History is thus separate for each
subproject. The location can be changed with the historyPath setting, which
393
has type Option[File]. For example, history can be stored in the root directory
for the project instead of the output directory:
The history path needs to be set for each project, since sbt will use the value of
historyPath for the current project (as selected by the project command).
The previous section describes how to configure the location of the history file.
This setting can be used to share the interactive history among all projects in a
build instead of using a different history for each project. The way this is done
is to set historyPath to be the same file, such as a file in the root project’s
target/ directory:
historyPath :=
Some( (target in LocalRootProject).value / ".history")
The in LocalRootProject part means to get the output directory for the root
project for the build.
If, for whatever reason, you want to disable history, set historyPath to None
in each project it should be disabled in:
This runs clean and then compile before entering the interactive prompt. If
either clean or compile fails, sbt will exit without going to the prompt. To enter
the prompt whether or not these initial commands succeed, prepend -shell,
which means to run shell if any command fails. For example,
394
Configure and use logging
When a command is run, more detailed logging output is sent to a file than
to the screen (by default). This output can be recalled for the command just
executed by running last.
For example, the output of run when the sources are uptodate is:
> run
[info] Running A
Hi!
[success] Total time: 0 s, completed Feb 25, 2012 1:00:00 PM
> last
[debug] Running task... Cancelable: false, max worker threads: 4, check cycles: false
[debug]
[debug] Initial source changes:
[debug] removed:Set()
[debug] added: Set()
[debug] modified: Set()
[debug] Removed products: Set()
[debug] Modified external sources: Set()
[debug] Modified binary dependencies: Set()
[debug] Initial directly invalidated sources: Set()
[debug]
[debug] Sources indirectly invalidated by:
[debug] product: Set()
[debug] binary dep: Set()
[debug] external source: Set()
[debug] Initially invalidated: Set()
[debug] Copy resource mappings:
[debug]
[info] Running A
[debug] Starting sandboxed run...
[debug] Waiting for threads to exit or System.exit to be called.
[debug] Classpath:
[debug] /tmp/e/target/scala-2.9.2/classes
[debug] /tmp/e/.sbt/0.12.0/boot/scala-2.9.2/lib/scala-library.jar
[debug] Waiting for thread runMain to exit
[debug] Thread runMain exited.
[debug] Interrupting remaining threads (should be all daemons).
395
[debug] Sandboxed run complete..
[debug] Exited with code 0
[success] Total time: 0 s, completed Jan 1, 2012 1:00:00 PM
Configuration of the logging level for the console and for the backing file are
described in following sections.
When a task is run, more detailed logging output is sent to a file than to the
screen (by default). This output can be recalled for a specific task by running
last <task>. For example, the first time compile is run, output might look
like:
> compile
[info] Updating {file:/.../demo/}example...
[info] Resolving org.scala-lang#scala-library;2.9.2 ...
[info] Done updating.
[info] Compiling 1 Scala source to .../demo/target/scala-2.9.2/classes...
[success] Total time: 0 s, completed Jun 1, 2012 1:11:11 PM
The output indicates that both dependency resolution and compilation were
performed. The detailed output of each of these may be recalled individually.
For example,
and:
396
Show warnings from the previous compilation
The Scala compiler does not print the full details of warnings by default. Com-
piling code that uses the deprecated error method from Predef might generate
the following output:
> compile
[info] Compiling 1 Scala source to <...>/classes...
[warn] there were 1 deprecation warnings; re-run with -deprecation for details
[warn] one warning found
> printWarnings
[warn] A.scala:2: method error in object Predef is deprecated: Use sys.error(message) instead
[warn] def x = error("Failed.")
[warn] ^
The quickest way to change logging levels is by using the error, warn, info, or
debug commands. These set the default logging level for commands and tasks.
For example,
> warn
will by default show only warnings and errors. To set the logging level before
any commands are executed on startup, use -- before the logging level. For
example,
$ sbt --warn
> compile
[warn] there were 2 feature warning(s); re-run with -feature for details
[warn] one warning found
[success] Total time: 4 s, completed ...
>
397
Change the logging level for a specific task, configuration, or project
The amount of logging is controlled by the logLevel setting, which takes values
from the Level enumeration. Valid values are Error, Warn, Info, and Debug
in order of increasing verbosity. The logging level may be configured globally,
as described in the previous section, or it may be applied to a specific project,
configuration, or task. For example, to change the logging level for compilation
to only show warnings and errors:
A common scenario is that after running a task, you notice that you need
more information than was shown by default. A logLevel based solution typ-
ically requires changing the logging level and running a task again. However,
there are two cases where this is unnecessary. First, warnings from a previous
compilation may be displayed using printWarnings for the main sources or
test:printWarnings for test sources. Second, output from the previous execu-
tion is available either for a single task or for in its entirety. See the section on
printWarnings and the sections on previous output.
By default, sbt hides the stack trace of most exceptions thrown during execution.
It prints a message that indicates how to display the exception. However, you
may want to show more of stack traces by default.
The setting to configure is traceLevel, which is a setting with an Int value.
When traceLevel is set to a negative value, no stack traces are shown. When
it is zero, the stack trace is displayed up to the first sbt stack frame. When
positive, the stack trace is shown up to that many stack frames.
For example, the following configures sbt to show stack traces up to the first
sbt frame:
The every part means to override the setting in all scopes. To change the trace
printing behavior for a single project, configuration, or task, scope traceLevel
appropriately:
398
Print the output of tests immediately instead of buffering
By default, sbt buffers the logging output of a test until the whole class finishes.
This is so that output does not get mixed up when executing in parallel. To
disable buffering, set the logBuffered setting to false:
logBuffered := false
The setting extraLoggers can be used to add custom loggers. A custom logger
should implement [AbstractLogger]. extraLoggers is a function ScopedKey[_]
=> Seq[AbstractLogger]. This means that it can provide different logging
based on the task that requests the logger.
extraLoggers := {
val currentFunction = extraLoggers.value
(key: ScopedKey[_]) => {
myCustomLogger(key) +: currentFunction(key)
}
}
Here, we take the current function currentFunction for the setting and provide
a new function. The new function prepends our custom logger to the ones
provided by the old function.
The special task streams provides per-task logging and I/O via a Streams in-
stance. To log, a task uses the log member from the streams task. Calling log
provides a Logger.
:
myTask := {
val log = streams.value.log
log.warn("A warning.")
}
Since settings cannot reference tasks, the special task streams cannot be used
to provide logging during setting initialization. The recommended way is to use
sLog. Calling sLog.value provides a Logger.
399
mySetting := {
val log = sLog.value
log.warn("A warning.")
}
Project metadata
A project should define name and version. These will be used in various parts of
the build, such as the names of generated artifacts. Projects that are published
to a repository should also override organization.
version := "1.0"
organization := "org.example"
By convention, this is a reverse domain name that you own, typically one specific
to your project. It is used as a namespace for projects.
A full/formal name can be defined in the organizationName setting. This is
used in the generated pom.xml. If the organization has a web site, it may be
set in the organizationHomepage setting. For example:
organizationHomepage := Some(url("http://example.org"))
400
Set the project’s homepage and other metadata
homepage := Some(url("http://scala-sbt.org"))
startYear := Some(2008)
Configure packaging
exportJars := true
The jar will be used by run, test, console, and other tasks that use the full
classpath.
By default, sbt constructs a manifest for the binary package from settings such
as organization and mainClass. Additional attributes may be added to the
packageOptions setting scoped by the configuration and package task.
Main attributes may be added with Package.ManifestAttributes. There are
two variants of this method, once that accepts repeated arguments that map
an attribute of type java.util.jar.Attributes.Name to a String value and
other that maps attribute names (type String) to the String value.
For example,
401
manifest.getAttributes("foo/bar/").put(Attributes.Name.SEALED, "false")
Package.JarManifest( manifest )
}
The artifactName setting controls the name of generated packages. See the
Artifacts page for details.
Note that mappings is scoped by the configuration and the specific package
task. For example, the mappings for the test source package are defined by the
mappings in (Test, packageSrc) task.
Running commands
402
Provide multiple commands to run consecutively
> ~ ;clean;compile
The < command reads commands from the files provided to it as arguments.
Run help < at the sbt prompt for details.
The alias command defines, removes, and displays aliases for commands. Run
help alias at the sbt prompt for details.
Example usage:
The eval command compiles and runs the Scala expression passed to it as an
argument. The result is printed along with its type. For example,
403
Configure and use Scala
The scalaVersion configures the version of Scala used for compilation. By de-
fault, sbt also adds a dependency on the Scala library with this version. See the
next section for how to disable this automatic dependency. If the Scala version
is not specified, the version sbt was built against is used. It is recommended to
explicitly specify the version of Scala.
For example, to set the Scala version to “2.11.1”,
scalaVersion := "2.11.1"
sbt adds a dependency on the Scala standard library by default. To disable this
behavior, set the autoScalaLibrary setting to false.
autoScalaLibrary := false
To set the Scala version in all scopes to a specific value, use the ++ command.
For example, to temporarily use Scala 2.10.4, run:
> ++ 2.10.4
Defining the scalaHome setting with the path to the Scala home directory will
use that Scala installation. sbt still requires scalaVersion to be set when a
local Scala version is used. For example,
scalaVersion := "2.10.0-local"
scalaHome := Some(file("/path/to/scala/home/"))
404
Enter the Scala REPL with a project’s dependencies on the classpath,
but not the compiled project classes
The consoleQuick action retrieves dependencies and puts them on the classpath
of the Scala REPL. The project’s sources are not compiled, but sources of any
source dependencies are compiled. To enter the REPL with test dependencies
on the classpath but without compiling test sources, run test:consoleQuick.
This will force compilation of main sources.
The console action retrieves dependencies and compiles sources and puts them
on the classpath of the Scala REPL. To enter the REPL with test dependencies
and compiled test sources on the classpath, run test:console.
Enter the Scala REPL with plugins and the build definition on the
classpath
> consoleProject
Define the initial commands evaluated when entering the Scala REPL
405
Define the commands evaluated when exiting the Scala REPL
sbt runs tests in the same JVM as sbt itself and Scala classes are not in the
same class loader as the application classes. This is also the case in console
and when run is not forked. Therefore, when using the Scala interpreter, it is
important to set it up properly to avoid an error message like:
The key is to initialize the Settings for the interpreter using embeddedDefaults.
For example:
406
Generate API documentation
sbt will run javadoc if there are only Java sources in the project. If there are
any Scala sources, sbt will run scaladoc. (This situation results from scaladoc
not processing Javadoc comments in Java sources nor linking to Javadoc.)
407
Enable automatic linking to the external Scaladoc of managed depen-
dencies
Set autoAPIMappings := true for sbt to tell scaladoc where it can find the
API documentation for managed dependencies. This requires that dependencies
have this information in its metadata and you are using scaladoc for Scala
2.10.2 or later.
apiMappings += (
(unmanagedBase.value / "a-library.jar") ->
url("https://example.org/api/")
)
Set apiURL to define the base URL for the Scaladocs for your library. This will
enable clients of your library to automatically link against the API documenta-
tion using autoAPIMappings. (This only works for Scala 2.10.2 and later.) For
example,
apiURL := Some(url("https://example.org/api/"))
This information will get included in a property of the published pom.xml, where
it can be automatically consumed by sbt.
Triggered execution
You can make a command run when certain files change by prefixing the com-
mand with ~. Monitoring is terminated when enter is pressed. This triggered
408
execution is configured by the watch setting, but typically the basic settings
watchSources and pollInterval are modified as described in later sections.
The original use-case for triggered execution was continuous compilation:
> ~ test:compile
> ~ compile
You can use the triggered execution feature to run any command or task, how-
ever. The following will poll for changes to your source code (main or test) and
run testOnly for the specified test.
> ~ ;a ;b
• watchSources defines the files for a single project that are monitored
for changes. By default, a project watches resources and Scala and Java
sources.
• watchTransitiveSources then combines the watchSources for the cur-
rent project and all execution and classpath dependencies (see .scala build
definition for details on inter-project dependencies).
pollInterval := 1000 // in ms
409
Define Custom Tasks
Sequencing
taskY := {
val x = taskX.value
x + 1
}
This is more constrained compared to the imperative style plain Scala code with
side effects such as the follows:
410
tasks in parallel. Another benefit is that we can deduplicate the graph, and
make sure that the task evaluation, such as compile in Compile, is called once
per command execution, as opposed to compiling the same source many times.
Because task system is generally set up this way, running something sequentially
is possible, but you will be fighting the system a bit, and it’s not always going
to be easy.
project/build.properties
sbt.version=0.13.16
project/style.sbt
build.sbt
411
To call this task type in compilecheck from the shell. If the compilation fails,
compilecheck would stop the execution.
root> compilecheck
[info] Compiling 1 Scala source to /Users/x/proj/target/scala-2.10/classes...
[error] /Users/x/proj/src/main/scala/Foo.scala:3: Unmatched closing brace '}' ignored here
[error] }
[error] ^
[error] one error found
[error] (compile:compileIncremental) Compilation failed
If sequential task is not enough, another step up is the dynamic task. Unlike
Def.task which expects you to return pure value A, with a Def.taskDyn you
return a task sbt.Def.Initialize[sbt.Task[A]] which the task engine can
continue the rest of the computation with.
Let’s try implementing a custom task called compilecheck that runs compile
in Compile and then scalastyle in Compile task added by scalastyle-sbt-
plugin.
project/build.properties
sbt.version=0.13.16
project/style.sbt
build.sbt v1
412
}
}).value
)
Now we have the same thing as the sequential task, except we can now return
the result c from the first task.
Now we can actually call compile in Compile from the shell and make it do
what we want it to do.
Thus far we’ve mostly looked at tasks. There’s another kind of tasks called
input tasks that accepts user input from the shell. A typical example for this is
the run in Compile task. The scalastyle task is actually an input task too.
See input task for the details of the input tasks.
Now suppose we want to call run in Compile task and then open the browser
for testing purposes.
src/main/scala/Greeting.scala
413
build.sbt v1
Here, I’m faking the browser opening using println as the side effect. We can
now call this task from the shell:
build.sbt v2 We can actually remove runopen key, by rewriting the new input
task to run in Compile:
Let’s suppose that there’s a task already that does the bowser opening called
openbrowser because of a plugin. Here’s how we can sequence a task after an
input tasks.
build.sbt v1
414
lazy val root = (project in file("."))
.settings(
runopen := (Def.inputTaskDyn {
import sbt.complete.Parsers.spaceDelimited
val args = spaceDelimited("<args>").parsed
Def.taskDyn {
(run in Compile).toTask(" " + args.mkString(" ")).value
openbrowser
}
}).evaluated,
openbrowser := {
println("open browser!")
}
)
415
The actualRun in Compile’s implementation was copy-pasted from run task’s
implementation in Defaults.scala.
Now we can call run foo from the shell and it will evaluate actualRun in
Compile with the passed in argument, and then evaluate the openbrowser task.
If all you care about is the side effects, and you really just want to emulate
humans typing in one command after another, a custom command might be
just want you need. This comes in handy for release procedures.
Here’s from the build script of sbt itself:
Examples
This section of the documentation has example sbt build definitions and code.
Contributions are welcome!
You may want to read the Getting Started Guide as a foundation for under-
standing the examples.
Note: As of sbt 0.13.7 blank lines are no longer used to delimit build.sbt files.
The following example requires sbt 0.13.7+.
Listed here are some examples of settings (each setting is independent). See
.sbt build definition for details.
416
)
// increase the time between polling for file changes when using continuous execution
pollInterval := 1000,
// append several options to the list of options passed to the Java compiler
javacOptions ++= Seq("-source", "1.5", "-target", "1.5"),
417
try { f } finally { println("Elapsed: " + (now - start)/1000.0 + " s") }
}""".stripMargin,
// set the initial commands when entering 'console' or 'consoleQuick', but not 'consoleP
initialCommands in console := "import myproject._",
// set the prompt (for this build) to include the project id.
shellPrompt in ThisBuild := { state => Project.extract(state).currentRef.project + "> "
// set the prompt (for the current project) to include the username
shellPrompt := { state => System.getProperty("user.name") + "> " },
418
timingFormat := {
import java.text.DateFormat
DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT)
},
// set the location of the JDK to use for compiling Java code.
// if 'fork' is true, this is used for 'run' as well
javaHome := Some(file("/usr/lib/jvm/sun-jdk-1.6")),
// Use Scala from a directory on the filesystem instead of retrieving from a repository
scalaHome := Some(file("/home/user/scala/trunk/")),
// only show warnings and errors on the screen for all tasks (the default is Info)
// individual tasks can then be more verbose using the previous setting
logLevel := Level.Warn,
419
// only show 10 lines of stack traces
traceLevel := 10,
// Exclude transitive dependencies, e.g., include log4j without including logging via jd
libraryDependencies +=
"log4j" % "log4j" % "1.2.15" excludeAll(
ExclusionRule(organization = "com.sun.jdmk"),
ExclusionRule(organization = "com.sun.jmx"),
ExclusionRule(organization = "javax.jms")
)
)
420
.sbt build with .scala files example
.sbt builds can be supplemented with project/*.scala files. When the build
file gets large enough, the first thing to factor out are resolvers and dependencies.
project/Resolvers.scala
import sbt._
import Keys._
object Resolvers {
val sunrepo = "Sun Maven2 Repo" at "http://download.java.net/maven/2"
val sunrepoGF = "Sun GF Maven2 Repo" at "http://download.java.net/maven/glassfish"
val oraclerepo = "Oracle Maven2 Repo" at "http://download.oracle.com/maven"
project/Dependencies.scala
import sbt._
import Keys._
object Dependencies {
val logbackVersion = "0.9.16"
val grizzlyVersion = "1.9.19"
421
These files can be used mange library dependencies in one place.
project/ShellPrompPlugin.scala
When you want to implement custom commands or tasks, you can organize your
build by defining an one-off auto plugin.
import sbt._
import Keys._
// Shell prompt which show the current project and git branch
object ShellPromptPlugin extends AutoPlugin {
override def trigger = allRequirements
override lazy val projectSettings = Seq(
shellPrompt := buildShellPrompt
)
val devnull: ProcessLogger = new ProcessLogger {
def info (s: => String) {}
def error (s: => String) { }
def buffer[T] (f: => T): T = f
}
def currBranch =
("git status -sb" lines_! devnull headOption)
.getOrElse("-").stripPrefix("## ")
val buildShellPrompt: State => String = {
case (state: State) =>
val currProject = Project.extract (state).currentProject.id
s"""$currProject:$currBranch> """
}
}
This auto plugin will display the current project name and the git branch.
build.sbt
import Resolvers._
import Dependencies._
422
version := "0.1.0",
scalaVersion := "2.12.2"
)
423
lazy val pricing = (project in file("cdap2-pricing"))
.dependsOn(common, compact, server)
.settings(
buildSettings,
libraryDependencies ++= pricingDeps
)
// Custom configurations
lazy val Common = config("common") describedAs("Dependencies required in all configurations.
lazy val Scalate = config("scalate") extend(Common) describedAs("Dependencies for using Scal
lazy val Saxon = config("saxon") extend(Common) describedAs("Dependencies for using Saxon ut
424
organization := "com.example",
version := "0.1.0",
scalaVersion := "2.10.4"
)
// We want our Common sources to have access to all of the dependencies on the classpa
// for compile and test, but when depended on, it should only require dependencies i
classpathConfiguration in Common := CustomCompile,
// Modify the default Ivy configurations.
// 'overrideConfigs' ensures that Compile is replaced by CustomCompile
ivyConfigurations := overrideConfigs(Scalate, Saxon, Common, CustomCompile)(ivyConfigura
// Put all dependencies without an explicit configuration into Common (optional)
defaultConfiguration := Some(Common),
// Declare dependencies in the appropriate configurations
libraryDependencies ++= Seq(
"org.fusesource.scalate" % "scalate-core" % "1.5.0" % Scalate,
"org.squeryl" %% "squeryl" % "0.9.5-6" % Scalate,
"net.sf.saxon" % "saxon" % "8.7" % Saxon
)
)
425
Advanced command example
This is an advanced example showing some of the power of the new settings
system. It shows how to temporarily modify all declared dependencies in the
build, regardless of where they are defined. It directly operates on the final
Seq[Setting[_]] produced from every setting involved in the build.
The modifications are applied by running canonicalize. A reload or using set
reverts the modifications, requiring canonicalize to be run again.
This particular example shows how to transform all declared dependencies on
ScalaCheck to use version 1.8. As an exercise, you might try transforming other
dependencies, the repositories used, or the scalac options used. It is possible to
add or remove settings as well.
This kind of transformation is possible directly on the settings of Project, but it
would not include settings automatically added from plugins or build.sbt files.
What this example shows is doing it unconditionally on all settings in all projects
in all builds, including external builds.
import sbt._
import Keys._
// Define the command. This takes the existing settings (including any session settings)
// and applies 'f' to each Setting[_]
def canonicalize = Command.command("canonicalize") { (state: State) =>
val extracted = Project.extract(state)
import extracted._
val transformed = session.mergeSettings map ( s => f(s) )
val newStructure = Load.reapply(transformed, structure)
Project.setProject(session, newStructure, state)
}
// Transforms a Setting[_].
def f(s: Setting[_]): Setting[_] = s.key.key match {
// transform all settings that modify libraryDependencies
case Keys.libraryDependencies.key =>
// hey scalac. T == Seq[ModuleID]
s.asInstanceOf[Setting[Seq[ModuleID]]].mapInit(mapLibraryDependencies)
// preserve other settings
case _ => s
}
// This must be idempotent because it gets applied after every transformation.
426
// That is, if the user does:
// libraryDependencies += a
// libraryDependencies += b
// then this method will be called for Seq(a) and Seq(a,b)
def mapLibraryDependencies(key: ScopedKey[Seq[ModuleID]], value: Seq[ModuleID]): Seq[Modul
value map mapSingle
Project Information
Usage
and it will display the full output from the last run of the update command.
427
How do I disable ansi codes in the output? Sometimes sbt doesn’t detect
that ansi codes aren’t supported and you get output that looks like:
or ansi codes are supported but you want to disable colored output. To com-
pletely disable ansi codes, set the sbt.log.format system property to false.
For example,
How can I start a Scala interpreter (REPL) with sbt project configu-
ration (dependencies, etc.)? In sbt’s shell run console.
Build definitions
What are the :=, +=, and ++= methods? These are methods on keys used
to construct a Setting or a Task. The Getting Started Guide covers all these
methods, see .sbt build definition, task graph, and appending values for example.
What is the % method? It’s used to create a ModuleID from strings, when
specifying managed dependencies. Read the Getting Started Guide about li-
brary dependencies.
How do I add files to a jar package? The files included in an artifact are
configured by default by a task mappings that is scoped by the relevant package
task. The mappings task returns a sequence Seq[(File,String)] of mappings
from the file to include to the path within the jar. See mapping files for details
on creating these mappings.
For example, to add generated sources to the packaged source artifact:
428
This takes sources from the managedSources task and relativizes them against
the managedSource base directory, falling back to a flattened mapping. If a
source generation task doesn’t write the sources to the managedSource directory,
the mapping function would have to be adjusted to try relativizing against
additional directories or something more appropriate for the generator.
How can a task avoid redoing work if the input files are unchanged?
There is basic support for only doing work when input files have changed or
when the outputs haven’t been generated yet. This support is primitive and
subject to change.
The relevant methods are two overloaded methods called FileFunction.cached.
Each requires a directory in which to store cached data. Sample usage is:
There are two additional arguments for the first parameter list that allow the file
tracking style to be explicitly specified. By default, the input tracking style is
FilesInfo.lastModified, based on a file’s last modified time, and the output
tracking style is FilesInfo.exists, based only on whether the file exists. The
other available style is FilesInfo.hash, which tracks a file based on a hash of
its contents. See the FilesInfo API for details.
A more advanced version of FileFunction.cached passes a data structure of
type ChangeReport describing the changes to input and output files since the
last evaluation. This version of cached also expects the set of files generated as
output to be the result of the evaluated function.
Extending sbt
429
samples. The sources for this configuration go in src/samples/scala/. Un-
specified settings delegate to those defined for the compile configuration. For
example, if scalacOptions are not overridden for samples, the options for the
main sources are used.
Options specific to samples may be declared like:
This uses the main options as base options because of +=. Use := to ignore the
main options:
The example adds all of the usual compilation related settings and tasks to
samples:
samples:run
samples:runMain
samples:compile
samples:console
samples:consoleQuick
samples:scalacOptions
samples:fullClasspath
samples:package
samples:packageSrc
...
How can I create a custom run task, in addition to run? This answer
is extracted from a mailing list discussion.
Read the Getting Started Guide up to custom settings for background.
A basic run task is created by:
430
If you want to be able to supply arguments on the command line, replace
TaskKey with InputKey and fullRunTask with fullRunInputTask. The Test
part can be replaced with another configuration, such as Compile, to use that
configuration’s classpath.
This run task can be configured individually by specifying the task key in the
scope. For example:
As an example, consider a proguard task. This task needs the ProGuard jars
in order to run the tool. First, define and add the new configuration:
ivyConfigurations += ProguardConfig
Then,
431
// Use the dependencies in a task, typically by putting them
// in a ClassLoader and reflectively calling an appropriate
// method.
proguard := {
val cp: Seq[File] = (managedClasspath in proguard).value
// ... do something with , which includes proguard ...
}
Defining the intermediate classpath is optional, but it can be useful for debug-
ging or if it needs to be used by multiple tasks. It is also possible to specify
artifact types inline. This alternative proguard task would look like:
proguard := {
val artifactTypes = Set("jar")
val cp: Seq[File] =
Classpaths.managedJars(proguardConfig, artifactTypes, update.value)
// ... do something with , which includes proguard ...
}
432
// Adds the files in 'extra' to the "extra" component
// under an exclusive machine-wide lock.
// The returned value is 'true' if files were actually copied and 'false'
// if the target files already exists (based on name only).
val copied: Boolean = s.locked(cs.lockFile, cs.addToComponent("extra", extra.toArray))
How can I take action when the project is loaded or unloaded? The
single, global setting onLoad is of type State => State (see State and Actions)
and is executed once, after all projects are built and loaded. There is a similar
hook onUnload for when a project is unloaded. Project unloading typically
occurs as a result of a reload command or a set command. Because the
onLoad and onUnload hooks are global, modifying this setting typically involves
composing a new function with the previous value. The following example shows
the basic structure of defining onLoad:
{
// the key for the current count
val key = AttributeKey[Int]("loadCount")
// the State transformer
val f = (s: State) => {
val previous = s get key getOrElse 0
println("Project load count: " + previous)
s.put(key, previous + 1)
}
onLoad in Global := {
val previous = (onLoad in Global).value
f compose previous
433
}
}
Errors
A more subtle variation of this error occurs when using scoped settings.
This setting varies between the test and compile scopes. The solution is use
the scoped setting, both as the input to the initializer, and the setting that we
update.
434
Dependency Management
How do I resolve a checksum error? This error occurs when the published
checksum, such as a sha1 or md5 hash, differs from the checksum computed for
a downloaded artifact, such as a jar or pom.xml. An example of such an error
is:
The invalid checksum should generally be reported to the repository owner (as
was done for the above error). In the meantime, you can temporarily disable
checking with the following setting:
Miscellaneous
How do I use the Scala interpreter in my code? sbt runs tests in the
same JVM as sbt itself and Scala classes are not in the same class loader as the
application classes. Therefore, when using the Scala interpreter, it is important
to set it up properly to avoid an error message like:
435
Failed to initialize compiler: class scala.runtime.VolatileBooleanRef not found.
** Note that as of 2.8 scala does not assume use of the java classpath.
** For the old behavior pass -usejavacp to scala, or if using a Settings
** object programmatically, settings.usejavacp.value = true.
The key is to initialize the Settings for the interpreter using embeddedDefaults.
For example:
How do I migrate from 0.7 to 0.10+? See the migration page first and
then the following questions.
Where has 0.7’s lib_managed gone? By default, sbt 0.13.16 loads managed
libraries from your ivy cache without copying them to a lib_managed directory.
This fixes some bugs with the previous solution and keeps your project directory
small. If you want to insulate your builds from the ivy cache being cleared, set
retrieveManaged := true and the dependencies will be copied to lib_managed
as a build-local cache (while avoiding the issues of lib_managed in 0.7.x).
This does mean that existing solutions for sharing libraries with your favoured
IDE may not work. Refer to Community Plugins page for a list of currently
available plugins for your IDE.
What are the commands I can use in 0.13.16 vs. 0.7? For a list of
commands, run help. For details on a specific command, run help <command>.
To view a list of tasks defined on the current project, run tasks. Alternatively,
436
see the Running page in the Getting Started Guide for descriptions of common
commands and tasks.
If in doubt start by just trying the old command as it may just work. The
built in TAB completion will also assist you, so you can just press TAB at the
beginning of a line and see what you get.
The following commands work pretty much as in 0.7 out of the box:
reload
update
compile
test
testOnly
publishLocal
exit
My tests all run really fast but some are broken that weren’t in 0.7!
Be aware that compilation and tests run in parallel by default in sbt 0.13.16. If
your test code isn’t thread-safe then you may want to change this behaviour by
adding one of the following to your build.sbt:
What happened to the web development and Web Start support since
0.7? Web application support was split out into a plugin. See the xsbt-web-
plugin project.
For an early version of an xsbt Web Start plugin, visit the xsbt-webstart project.
437
How are inter-project dependencies different in 0.13.16 vs. 0.7? In
0.13.16, there are three types of project dependencies (classpath, execution, and
configuration) and they are independently defined. These were combined in a
single dependency type in 0.7.x. A declaration like:
meant that the B project had a classpath and execution dependency on A and A
had a configuration dependency on B. Specifically, in 0.7.x:
In 0.13.16, declare the specific type of dependency you want. Read about multi-
project builds in the Getting Started Guide for details.
</td>
PathFinder class
Seq[File], PathFinder class, PathFinder object
Where can I find plugins for 0.13.16? See Community Plugins for a list
of currently available plugins.
Index
This is an index of common methods, types, and values you might find in an
sbt build definition. For command names, see Running. For available plugins,
see the plugins list.
438
Values and Types
Dependency Management
Process
439
• A ProcessBuilder is the type used to define a process. It provides combi-
nators for building up processes from smaller processes.
• A Process represents the actual forked process.
• The Process companion object provides methods for constructing primi-
tive processes.
Build Structure
• Build is the trait implemented for a .scala build definition, which defines
project relationships and settings.
• Plugin is the trait implemented for sbt plugins.
• Project is both a trait and a companion object that declares a single
module in a build. See .scala build definition.
• Keys is an object that provides all of the built-in keys for settings and
tasks.
• State contains the full state for a build. It is mainly used by Commands
and sometimes Input Tasks. See also State and Actions.
Methods
Settings and Tasks See the Getting Started Guide for details.
• :=, +=, ++= These construct a Setting, which is the fundamental type in
the settings system.
• value This uses the value of another setting or task in the definition of
a new setting or task. This method is special (it is a macro) and cannot
be used except in the argument of one of the setting definition methods
above (:=, …) or in the standalone construction methods Def.setting and
Def.task. See Task-Graph for details.
• in specifies the Scope or part of the Scope of a setting being referenced.
See scopes.
File and IO See RichFile, PathFinder, and Paths for the full documentation.
• / When called on a single File, this is new File(x,y). For Seq[File], this
is applied for each member of the sequence..
• * and ** are methods for selecting children (*) or descendants (**) of a
File or Seq[File] that match a filter.
• |, ||, &&, &, -, and -- are methods for combining filters, which are often
used for selecting Files. See NameFilter and FileFilter. Note that methods
with these names also exist for other types, such as collections (like Seq)
and Parser (see Parsing Input).
440
• pair Used to construct mappings from a File to another File or to a
String. See Mapping Files.
• get forces a PathFinder (a call-by-name data structure) to a strict
Seq[File] representation. This is a common name in Scala, used by
types like Option.
Parsing These methods are used to build up Parsers from smaller Parsers.
They closely follow the names of the standard library’s parser combinators. See
Parsing Input for the full documentation. These are used for Input Tasks and
Commands.
441
Processes These methods are used to fork external processes. Note that this
API has been included in the Scala standard library for version 2.9. Process-
Builder is the builder type and Process is the type representing the actual forked
process. The methods to combine processes start with # so that they share the
same precedence.
• run, !, !!, !<, lines, lines_! are different ways to start a process once
it has been defined. The lines variants produce a Stream[String] to obtain
the output lines.
• #<, #<<, #> are used to get input for a process from a source or send the
output of a process to a sink.
• #| is used to pipe output from one process into the input of another.
• #||, #&&, ### sequence processes in different ways.
On 2008-12-18, Mark Harrah announced sbt 0.3.2 as the initial release of sbt.
Mark remained the primary author of sbt until sbt 0.13.1 (2013-12-11). In 2014,
sbt project was handed over to the authors of this document Josh Suereth and
Eugene Yokota.
As we move towards sbt 1.0, we wish to stabilize what’s already stable and
innovate where it matters. There are several levels of stability:
• conceptual stability
• source compatibility of the build definition
• binary compatibility of the plugins
The only thing that we plan to change is the last point. In sbt 1.0, we will
replace the interactive shell with sbt server that’s accessible via JSON API and
a text-based client.
442
Source compatibility of the build definition Source compatibility means
that a build source that worked for sbt version A works for another version B
without modification. Our goal for sbt 1.0 is to adopt Semantic Versioning, and
maintain source compatibility of the build during 1.x.y.
Modularization
The process we aim to take for sbt 1.0 is to disassemble sbt into smaller modules
and layers. To be clear, sbt 0.13’s codebase already does consist of numerous
subprojects.
Layers are more coarse-grained sets of subproject(s) that can be used indepen-
dently. Another purpose of the modularization is to distinguish between public
API and internal implementation. Reducing the surface area of the sbt code
base has several benefits:
• It makes it easier for the build users and the plugin authors to learn the
APIs.
• It makes it easier for us to maintain binary and semantic compatibilities.
• It encourages the reuse of the modules.
Module summary
IO API (sbt/io) IO API is a low level API to deal with files and directories.
443
Figure 9: Module diagram
Util APIs (sbt/util) Util APIs provide commonly used features like logging
and internal datatypes used by sbt.
Build API (tbd) This is the part that’s exposed to build.sbt. The respon-
sibility of the module is to load the build files and plugins, and provide a way
for commands to be executed on the state.
444
This might remain at sbt/sbt.
This page discusses the coding style and other guidelines for sbt 1.0.
General goal
sbt 1.0 will primarily target Scala 2.12. We will cross-build against Scala 2.10.
Modular design
Aim small The fewer methods we can expose to the build user, the easier sbt
becomes to maintain.
445
Hide implementation details The implementation details should be hidden
behind sbt.internal.x packages, where x could be the name of the main
package (like io).
Hide external classes Avoid exposing external classes in the API, except for
standard Scala and Java classes.
Compiler flags
-encoding utf8
-deprecation
-feature
-unchecked
-Xlint
-language:higherKinds
-language:implicitConversions
-Xfuture
-Yinline-warnings
-Yno-adapted-args
-Ywarn-dead-code
-Ywarn-numeric-widen
-Ywarn-value-discard
-Xfatal-warnings
Package name and organization name Use the package name appended
with the layer name, such as sbt.io for IO layer. The organization name for
published artifacts should remain org.scala-sbt.
Binary resiliency
A good overview on the topic of binary resiliency is Josh’s 2012 talk on Binary
resiliency. The guideline here applies mostly to publicly exposed APIs.
446
MiMa Use MiMa.
Abstract classes are also useful To trait, or not to trait?. Abstract classes
are less flexible than traits, but traits pose more problems for binary compati-
bility. Abstract classes also have better Java interoperability.
Seal traits and abstract classes If there’s no need to keep a class open,
seal it.
Finalize the leaf classes If there’s no need to keep a class open, finalize it.
Typeclass and subclass inheritance The typeclass pattern with pure traits
might ease maintaining binary compatibility more so than subclassing.
Avoid case classes, use sbt-datatype Case classes involve code generation
that makes it harder to maintain binary compatibility over time.
Avoid overuse of def apply def apply should be reserved for factory meth-
ods in a companion object that returns type T.
447
Use specific datatypes (Vector, List, or Array), rather than Seq
scala.Seq is scala.collection.Seq, which is not immutable. Default to
Vector. Use List if constant prepending is needed. Use Array if Java
interoperability is needed. Note that using mutable collections is perfectly fine
within the implementation.
Avoid calling toSeq on Map Same as above. This will introduce non-
determinism.
Style matters
448
package sbt.io
object syntax {
implicit def uriToRichURI(uri: URI): RichURI = new RichURI(uri)
}
When all the layers are available, the sbt package should also define an object
called syntax which forwards implicit conversions from all the layers:
package sbt
object syntax {
implicit def uriToRichURI(uri: URI): io.RichURI = io.syntax.uriToRichURI(uri)
....
}
sbt-datatype
To enable the plugin for your build, put the following line in project/datatype.sbt:
449
Your datatype definitions should be placed by default in src/main/datatype
and src/test/datatype. Here’s how your build should be configured:
Datatype schema
1. Records
2. Interfaces
3. Enums
{
"types": [
{
"name": "Person",
"type": "record",
"target": "Scala",
"fields": [
{
"name": "name",
"type": "String"
},
{
"name": "age",
"type": "int"
}
]
}
]
}
450
final class Person(
val name: String,
val age: Int) extends Serializable {
override def equals(o: Any): Boolean = o match {
case x: Person => (this.name == x.name) && (this.age == x.age)
case _ => false
}
override def hashCode: Int = {
37 * (37 * (17 + name.##) + age.##)
}
override def toString: String = {
"Person(" + name + ", " + age + ")"
}
private[this] def copy(name: String = name, age: Int = age): Person = {
new Person(name, age)
}
def withName(name: String): Person = {
copy(name = name)
}
def withAge(age: Int): Person = {
copy(age = age)
}
}
object Person {
def apply(name: String, age: Int): Person = new Person(name, age)
}
Or the following Scala code (after changing the target property to "Java"):
451
return true;
} else if (!(obj instanceof Person)) {
return false;
} else {
Person o = (Person)obj;
return name().equals(o.name()) && (age() == o.age());
}
}
public int hashCode() {
return 37 * (37 * (17 + name().hashCode()) + (new Integer(age())).hashCode());
}
public String toString() {
return "Person(" + "name: " + name() + ", " + "age: " + age() + ")";
}
}
{
"types": [
{
"name": "Greeting",
"namespace": "com.example",
"target": "Scala",
"type": "interface",
"fields": [
{
"name": "message",
"type": "String"
}
],
"types": [
{
"name": "SimpleGreeting",
"namespace": "com.example",
"target": "Scala",
"type": "record"
}
]
}
]
}
452
This generates abstract class named Greeting and a class named SimpleGreeting
that extends Greeting.
In addition, interfaces can define messages, which generates abstract method
declarations.
{
"types": [
{
"name": "FooService",
"target": "Scala",
"type": "interface",
"messages": [
{
"name": "doSomething",
"response": "int*",
"request": [
{
"name": "arg0",
"type": "int*",
"doc": [
"The first argument of the message.",
]
}
]
}
]
}
]
}
{
"types": [
{
"name": "Weekdays",
"type": "enum",
"target": "Java",
"symbols": [
"Monday", "Tuesday", "Wednesday", "Thursday",
"Friday", "Saturday", "Sunday"
]
}
]
}
453
This schema will generate the following Java code:
Or the following Scala code (after changing the target property to):
{
"types": [
{
"name": "Greeting",
"type": "record",
"target": "Scala",
"fields": [
{
"name": "message",
"type": "String"
}
]
454
}
]
}
The generated code could be used in a Scala program using the following code:
Imagine now that you would like to extend your datatype to include a date to
the Greetings. The datatype can be modified accordingly:
{
"types": [
{
"name": "Greeting",
"type": "record",
"target": "Scala",
"fields": [
{
"name": "message",
"type": "String"
},
{
"name": "date",
"type": "java.util.Date"
}
]
}
]
}
Unfortunately, the code that used Greeting would no longer compile, and
classes that have been compiled against the previous version of the datatype
would crash with a NoSuchMethodError.
To circumvent this problem and allow you to grow your datatypes, it is possible
to indicate the version since the field exists and a default value in the datatype
definition:
{
"types": [
{
"name": "Greeting",
"type": "record",
"target": "Scala",
455
"fields": [
{
"name": "message",
"type": "String"
},
{
"name": "date",
"type": "java.util.Date",
"since": "0.2.0",
"default": "new java.util.Date()"
}
]
}
]
}
Now the code that was compiled against previous definitions of the datatype
will still run.
{
"codecNamespace": "com.example.codec",
"fullCodec": "CustomJsonProtocol",
"types": [
{
"name": "Person",
"namespace": "com.example",
"type": "record",
"target": "Scala",
"fields": [
{
"name": "name",
"type": "String"
},
{
"name": "age",
456
"type": "int"
}
]
}
]
}
scala> assert(p == q)
All the elements of the schema definition accept a number of parameters that
will influence the generated code. These parameters are not available for every
node of the schema. Please refer to the syntax summary to see whether a
parameters can be defined for a node.
name
This parameter defines the name of a field, record, field, etc.
457
target
This parameter determines whether the code will be generated in Java or Scala.
namespace
This parameter exists only for Definitions. It determines the package in which
the code will be generated.
doc
The Javadoc that will accompany the generated element.
fields
For a protocol or a record only, it describes all the fields that compose the
generated entity.
types
For a protocol, it defines the child protocols and records that extend it.
For an enumeration, it defines the values of the enumeration.
since
This parameter exists for fields only. It indicates the version in which the field
has been added to its parent protocol or record.
When this parameter is defined, default must also be defined.
default
This parameter exists for fields only. It indicates what the default value should
be for this field, in case it is used by a class that has been compiled against an
earlier version of this datatype.
It must contain an expression which is valid in the target language of the parent
protocol or record.
type for fields
It indicates what is the underlying type of the field.
Always use the type that you want to see in Scala. For instance, if your field
will contain an integer value, use Int rather than Java’s int. datatype will
automatically use Java’s primitive types if they are available.
For non-primitive types, it is recommended to write the fully-qualified type.
type for other definitions
It simply indicates the kind of entity that you want to generate: protocol,
record or enumeration.
458
Settings
This location can be changed by setting a new location in your build definition:
Syntax summary
459
(, "symbols": [ Symbol* ])? }
Symbol := ID
{ "name": ID
(, "doc": string constant)? }
Compiler Interface
The compiler interface is the communication link between sbt and the Scala
compiler.
It is used to get information from the Scala compiler, and must therefore be
compiled against the Scala version in use for the configured projects.
The code for this project can be found in the directory compile/interface.
Because the compiler interface is recompiled against each Scala version in use
in your project, its source must stay compatible with all the Scala versions that
sbt supports (from Scala 2.8 to the latest version of Scala).
This comes at great cost for both the sbt maintainers and the Scala compiler
authors:
1. The compiler authors cannot remove old and deprecated public APIs from
the Scala compiler.
2. sbt cannot use new APIs defined in the Scala compiler.
3. sbt must implement all kinds of hackery to remain source-compatible with
all versions of the Scala compiler and support new features.
460
To circumvent this problem, a new mechanism that allows sbt to fetch the
version of the sources for the compiler interface that are the most specific for
the Scala version in use has been implemented in sbt.
For instance, for a project that is compiled using Scala 2.11.8-M2, sbt will look
for the following version of the sources for the compiler interface, in this order:
1. 2.11.8-M2
2. 2.11.8
3. 2.11
4. The default sources.
This new mechanism allows both the Scala compiler and sbt to move forward
and enjoy new APIs while being certain than users of older versions of Scala
will still be able to use sbt.
Finally, another advantage of this technique is that it relies on Ivy to retrieve
the sources of the compiler bridge, but can be easily ported for use with Maven,
which is the distribution mechanism that the sbt maintainers would like to use
to distribute sbt’s modules.
sbt Launcher
The sbt launcher provides a generic container that can load and run programs
resolved using the Ivy dependency manager. Sbt uses this as its own deployment
mechanism.
The code is hosted at sbt/launcher.
The sbt launcher component is a self-contained jar that boots a Scala application
or server without Scala or the application already existing on the system. The
only prerequisites are the launcher jar itself, an optional configuration file, and
a Java runtime version 1.6 or greater.
461
Overview
A user downloads the launcher jar and creates a script to run it. In this docu-
mentation, the script will be assumed to be called launch. For Unix, the script
would look like: java -jar sbt-launcher.jar "$@"
The user can now launch servers and applications which provide sbt launcher
configuration.
Alternatively, you can repackage the launcher with a launcher configuration
file. For example, sbt/sbt pulls in the raw JAR and injects the appropriate
boot.properties files for sbt.
The user can then launch the application using myapp arg1 arg2 ...
More on launcher configuration can be found at Launcher Configuration
Servers The sbt launcher can be used to launch and discover running servers
on the system. The launcher can be used to launch servers similarly to applica-
tions. However, if desired, the launcher can also be used to ensure that only one
instance of a server is running at time. This is done by having clients always
use the launcher as a service locator.
To discover where a server is running (or launch it if it is not run-
ning), the user downloads the configuration file for the server (call it
my.server.configuration) and creates a script to discover the server (call it
find-myserver):
This command will print out one string, the URI at which to reach the server,
e.g. sbt://127.0.0.1:65501. Clients should use the IP/port to connect to to
the server and initiate their connection.
When using the locate feature, the sbt launcher makes the following restrictions
to servers:
• The Server must have a starting class that extends the xsbti.ServerMain
class
462
• The Server must have an entry point (URI) that clients can use to detect
the server
• The server must have defined a lock file which the launcher can use to
ensure that only one instance is running at a time
• The filesystem on which the lock file resides must support locking.
• The server must allow the launcher to open a socket against the port
without sending any data. This is used to check if a previous server is still
alive.
resolvers += sbtResolver.value
463
Make the entry point to your class implement xsbti.AppMain. An example
that uses some of the information:
package com.acme.launcherapp
// demonstrate the ability to reboot the application into different versions of Scal
// and how to return the code to exit with
scalaVersion match
{
case "2.10.6" =>
new xsbti.Reboot {
def arguments = configuration.arguments
def baseDirectory = configuration.baseDirectory
def scalaVersion = "2.11.8"
def app = configuration.provider.id
}
case "2.11.8" => new Exit(1)
case _ => new Exit(0)
}
}
class Exit(val code: Int) extends xsbti.Exit
}
Next, define a configuration file for the launcher. For the above class, it might
look like:
[scala]
version: 2.11.8
[app]
org: com.acme
name: launcherapp
version: 0.0.1
class: com.acme.launcherapp.Main
cross-versioned: true
464
[repositories]
local
maven-central
[boot]
directory: ${user.home}/.myapp/boot
Execution Let’s review what’s happening when the launcher starts your ap-
plication.
On startup, the launcher searches for its configuration and then parses it. Once
the final configuration is resolved, the launcher proceeds to obtain the necessary
jars to launch the application. The boot.directory property is used as a base
directory to retrieve jars to. Locking is done on the directory, so it can be shared
system-wide. The launcher retrieves the requested version of Scala to
${boot.directory}/${scala.version}/lib/
If this directory already exists, the launcher takes a shortcut for startup perfor-
mance and assumes that the jars have already been downloaded. If the directory
does not exist, the launcher uses Apache Ivy to resolve and retrieve the jars. A
similar process occurs for the application itself. It and its dependencies are
retrieved to
${boot.directory}/${scala.version}/${app.org}/${app.name}/.
465
Once all required code is downloaded, the class loaders are set up. The launcher
creates a class loader for the requested version of Scala. It then creates a child
class loader containing the jars for the requested app.components and with the
paths specified in app.resources. An application that does not use components
will have all of its jars in this class loader.
The main class for the application is then instantiated. It must be a public class
with a public no-argument constructor and must conform to xsbti.AppMain.
The run method is invoked and execution passes to the application. The argu-
ment to the ‘run’ method provides configuration information and a callback to
obtain a class loader for any version of Scala that can be obtained from a repos-
itory in [repositories]. The return value of the run method determines what
is done after the application executes. It can specify that the launcher should
restart the application or that it should exit with the provided exit code.
The sbt launcher is a mechanism whereby modules can be loaded from Ivy and
executed within a JVM. It abstracts the mechanism of grabbing and caching
jars, allowing users to focus on what application they want, and control its
versions.
The launcher’s primary goal is to take configuration for applications— mostly
Ivy coordinates and a main class—and start the application. The launcher
resolves the Ivy module, caches the required runtime jars, and starts the appli-
cation.
The sbt launcher provides the application with the means to load a different
application when it completes, exit normally, or load additional applications
from inside another.
The sbt launcher provides these core functions:
• Module Resolution
• Classloader Caching and Isolation
• File Locking
• Service Discovery and Isolation
Module Resolution
The primary purpose of the sbt launcher is to resolve applications and run them.
This is done through the [app] configuration section. See launcher configuration
for more information on how to configure module resolution.
Module resolution is performed using the Ivy dependency management library.
This library supports loading artifacts from Maven repositories as well.
466
Classloader Caching and Isolation
The sbt launcher’s classloading structure is different than just starting an ap-
plication in the standard Java mechanism. Every application loaded by the
launcher is given its own classloader. This classloader is a child of the Scala
classloader used by the application. The Scala classloader can see all of the
xsbti.* classes from the launcher itself.
Here’s an example classloader layout from an sbt-launched application.
In this diagram, three different applications were loaded. Two of these use the
same version of Scala (2.9.2). In this case, sbt can share the same classloader for
these applications. This has the benefit that any JIT optimisations performed
on Scala classes can be re-used between applications thanks to the shared class-
loader.
Caching
The sbt launcher creates a secondary cache on top of Ivy’s own cache. This helps
isolate applications from errors resulting from unstable revisions, like -SNAPSHOT.
467
For any launched application, the launcher creates a directory to store all its
jars. Here’s an example layout.
Locking
This feature requires a filesystem which supports locking. It is exposed via the
xsbti.GlobalLock interface.
Note: This is both a thread and file lock. Not only are we limiting access to a
single process, but also a single thread within that process.
The launcher also provides a mechanism to ensure that only one instance of a
server is running, while dynamically starting it when a client requests. This is
done through the --locate flag on the launcher. When the launcher is started
with the --locate flag it will do the following:
The configured server.lock file is thus used to prevent multiple servers from
running. sbt itself uses this to prevent more than one server running on any given
project directory by configuring server.lock to be ${user.dir}/.sbtserver.
The launcher may be configured in one of the following ways in increasing order
of precedence:
468
• Put a configuration file named sbt.boot.properties on the classpath.
Put it in the classpath root without the /sbt prefix.
• Specify the location of an alternate configuration on the command line,
either as a path or an absolute URI. This can be done by either specifying
the location as the system property sbt.boot.properties or as the first
argument to the launcher prefixed by @. The system property has lower
precedence. Resolution of a relative path is first attempted against the
current working directory, then against the user’s home directory, and
then against the directory containing the launcher jar.
Example
[scala]
version: ${sbt.scala.version-auto}
[app]
org: ${sbt.organization-org.scala-sbt}
name: sbt
version: ${sbt.version-read(sbt.version)[0.13.5]}
class: ${sbt.main.class-sbt.xMain}
components: xsbti,extra
cross-versioned: ${sbt.cross.versioned-false}
[repositories]
local
typesafe-ivy-releases: http://repo.typesafe.com/typesafe/ivy-releases/, [organization]/[mod
maven-central
sonatype-snapshots: https://oss.sonatype.org/content/repositories/snapshots
[boot]
directory: ${sbt.boot.directory-${sbt.global.base-${user.home}/.sbt}/boot/}
[ivy]
ivy-home: ${sbt.ivy.home-${user.home}/.ivy2/}
checksums: ${sbt.checksums-sha1,md5}
override-build-repos: ${sbt.override.build.repos-false}
repository-config: ${sbt.repository.config-${sbt.global.base-${user.home}/.sbt}/repositorie
469
1. Scala Configuration The [scala] section is used to configure the version
of Scala. It has one property:
470
There are several built-in strings that can be used for common repositories:
Besides built in repositories, other repositories can be configured using the fol-
lowing syntax:
The name property is an identifier which Ivy uses to cache modules resolved
from this location. The name should be unique across all repositories.
The url property is the base url where Ivy should look for modules.
The pattern property is an optional specification of how Ivy should look for
modules. By default, the launcher assumes repositories are in the maven style
format.
The skipConsistencyCheck string is used to tell Ivy not to validate checksums
and signatures of files it resolves.
4. The Boot section The [boot] section is used to configure where the sbt
launcher will store its cache and configuration information. It consists of the
following properties:
• directory - The directory defined here is used to store all cached JARs
resolved launcher.
• properties - (optional) A properties file to use for any read variables.
5. The Ivy section The [ivy] section is used to configure the Ivy depen-
dency manager for resolving applications. It consists of the following properties:
• ivy-home - The home directory for Ivy. This determines where the ivy-
local repository is located, and also where the Ivy cache is stored. Defaults
to ~/.ivy2
• checksums - The comma-separated list of checksums that Ivy should use
to verify artifacts have correctly resolved, e.g. md5 or sha1.
• override-build-repos - If this is set, then the isOverrideRepositories
method on xsbti.Launcher interface will return its value. The use of
this method is application-specific, but in the case of sbt denotes that the
configuration of repositories in the launcher should override those used by
any build. Applications should respect this convention if they can.
• repository-config - This specifies a configuration location where Ivy
repositories can also be configured. If this file exists, then its contents
override the [repositories] section.
471
6. The Server Section When using the --locate feature of the launcher,
this section configures how a server is started. It consists of the following prop-
erties:
• lock - The file that controls access to the running server. This file will
contain the active port used by a server and must be located on a filesystem
that supports locking.
• jvmargs - A file that contains line-separated JVM arguments that were
used when starting the server.
• jvmprops - The location of a properties file that will define override prop-
erties in the server. All properties defined in this file will be set as -D Java
properties.
Variable Substitution
• ${variable.name}
• ${variable.name-default}
read(property.name)[default]
This will look in the file configured by boot.properties for a value. If there
is no boot.properties file configured, or the property does not exist, then the
default value is chosen.
Syntax
The configuration file is line-based, read as UTF-8 encoded, and defined by the
following grammar. 'nl' is a newline or end of file and 'text' is plain text
without newlines or the surrounding delimiters (such as parentheses or square
brackets):
472
configuration: scala app repositories boot log appProperties
scala: "[" "scala" "]" nl version nl classifiers nl
app: "[" "app" "]" nl org nl name nl version nl components nl class nl crossVersioned nl res
repositories: "[" "repositories" "]" nl (repository nl)*
boot: "[" "boot" "]" nl directory nl bootProperties nl search nl promptCreate nl promptFill
log: "[" "log" "]" nl logLevel nl
appProperties: "[" "app-properties" "]" nl (property nl)*
ivy: "[" "ivy" "]" nl homeDirectory nl checksums nl overrideRepos nl repoConfig nl
directory: "directory" ":" path
bootProperties: "properties" ":" path
search: "search" ":" ("none" | "nearest" | "root-first" | "only" ) ("," path)*
logLevel: "level" ":" ("debug" | "info" | "warn" | "error")
promptCreate: "prompt-create" ":" label
promptFill: "prompt-fill" ":" boolean
quickOption: "quick-option" ":" boolean
version: "version" ":" versionSpecification
versionSpecification: readProperty | fixedVersion
readProperty: "read" "(" propertyName ")" "[" default "]"
fixedVersion: text
classifiers: "classifiers" ":" text ("," text)*
homeDirectory: "ivy-home" ":" path
checksums: "checksums" ":" checksum ("," checksum)*
overrideRepos: "override-build-repos" ":" boolean
repoConfig: "repository-config" ":" path
org: "org" ":" text
name: "name" ":" text
class: "class" ":" text
components: "components" ":" component ("," component)*
crossVersioned: "cross-versioned" ":" ("true" | "false" | "none" | "binary" | "full")
resources: "resources" ":" path ("," path)*
repository: ( predefinedRepository | customRepository ) nl
predefinedRepository: "local" | "maven-local" | "maven-central"
customRepository: label ":" url [ ["," ivyPattern] ["," artifactPattern] [", mavenCompatible
property: label ":" propertyDefinition ("," propertyDefinition)*
propertyDefinition: mode "=" (set | prompt)
mode: "quick" | "new" | "fill"
set: "set" "(" value ")"
prompt: "prompt" "(" label ")" ("[" default "]")?
boolean: "true" | "false"
nl: "\r\n" | "\n" | "\r"
path: text
propertyName: text
label: text
default: text
checksum: text
ivyPattern: text
473
artifactPattern: text
url: text
component: text
Notes
Here are some more docs that used to be part of Developer Guide.
Core Principles
This document details the core principles overarching sbt’s design and code style.
sbt’s core principles can be stated quite simply:
With these principles in mind, let’s walk through the core design of sbt.
This is the first piece you hit when starting sbt. sbt’s command engine is the
means by which it processes user requests using the build state. The command
engine is essentially a means of applying state transformations on the build
state, to execute user requests.
In sbt, commands are functions that take the current build state (sbt.State)
and produce the next state. In other words, they are essentially functions of
sbt.State => sbt.State. However, in reality, Commands are actually string
processors which take some string input and act on it, returning the next build
state.
So, the entirety of sbt is driven off the sbt.State class. Since this class needs
to be resilient in the face of custom code and plugins, it needs a mechanism
to store the state from any potential client. In dynamic languages, this can be
done directly on objects.
A naive approach in Scala is to use a Map<String,Any>. However, this violates
tenant #1: Everything should have a Type. So, sbt defines a new type of map
called an AttributeMap. An AttributeMap is a key-value storage mechanism
where keys are both strings and expected Types for their value.
Here is what the type-safe AttributeKey key looks like :
474
sealed trait AttributeKey[T] {
/** The label is the identifier for the key and is camelCase by convention. */
def label: String
/** The runtime evidence for ``T`` */
def manifest: Manifest[T]
}
These keys store both a label (string) and some runtime type information
(manifest). To put or get something on the AttributeMap, we first need to con-
struct one of these keys. Let’s look at the basic definition of the AttributeMap:
trait AttributeMap {
/** Gets the value of type ``T`` associated with the key ``k`` or ``None`` if no value is
* If a key with the same label but a different type is defined, this method will return ``
def get[T](k: AttributeKey[T]): Option[T]
/** Adds the mapping ``k -> value`` to this map, replacing any existing mapping for ``k``.
* Any mappings for keys with the same label but different types are unaffected. */
def put[T](k: AttributeKey[T], value: T): AttributeMap
}
Now that there’s a definition of what build state is, there needs to be a way to
dynamically construct it. In sbt, this is done through the Setting[_] sequence.
Settings Architecture
475
1. There can be no circular dependencies
2. If one Initialize[_] depends on another Initialize[_] key, then all
associated Initialize[_] blocks for that key must have run before we
load the value.
normalizedName := normalize(name.value)
476
Task Architecture
The next layer in sbt is around these user requests, or tasks. When a user
configures a build, they are defining a set of repeatable tasks that they can
run on their project. Things like compile or test. These tasks also have a
dependency graph, where e.g. the test task requires that compile has run
before it can successfully execute.
sbt defines a class Task[T]. The T type parameter represents the type of data
returned by a task. Remember the tenets of sbt? “All things have types” and
“Dependencies are explicit” both hold true for tasks. sbt promotes a style of
task dependencies that is closer to functional programming: return data for
your users rather than using shared mutable state.
Most build tools communicate over the filesystem, and indeed by necessity sbt
does some of this. However, for stable parallelization it is far better to keep
tasks isolated on the filesystem and communicate directly through types.
Similarly to how a Setting[_] stores both dependencies and an initialization
function, a Task[_] stores both its Task[_]dependencies and its behavior (a
function).
TODO - More on Task[_]
TODO - Transition into InputTask[_], rehash Command
TODO - Transition into Scope.
Settings Core
This page describes the core settings engine a bit. This may be useful for using it
outside of sbt. It may also be useful for understanding how sbt works internally.
The documentation is comprised of two parts. The first part shows an example
settings system built on top of the settings engine. The second part comments
on how sbt’s settings system is built on top of the settings engine. This may help
illuminate what exactly the core settings engine provides and what is needed to
build something like the sbt settings system.
Example
Setting up To run this example, first create a new project with the following
build.sbt file:
resolvers += sbtResolver.value
477
Then, put the following examples in source files SettingsExample.scala and
SettingsUsage.scala. Finally, run sbt and enter the REPL using console.
To see the output described below, enter SettingsUsage.
Example Settings System The first part of the example defines the custom
settings system. There are three main parts:
There is also a fourth, but its usage is likely to be specific to sbt at this time.
The example uses a trivial implementation for this part.
SettingsExample.scala:
import sbt._
478
// These three functions + a scope (here, Scope) are sufficient for defining our setting
}
Example Usage This part shows how to use the system we just defined. The
end result is a Settings[Scope] value. This type is basically a mapping Scope
-> AttributeKey[T] -> Option[T]. See the Settings API documentation for
details.
SettingsUsage.scala:
import sbt._
import SettingsExample._
import Types._
object SettingsUsage {
val b4 = ScopedKey(Scope(4), b)
// Show results.
for(i <- 0 to 5; k <- Seq(a, b)) {
println( k.label + i + " = " + applied.get( Scope(i), k) )
}
}
479
This produces the following output when run:
a0 = None
b0 = None
a1 = None
b1 = None
a2 = None
b2 = None
a3 = Some(3)
b3 = None
a4 = Some(3)
b4 = Some(9)
a5 = Some(4)
b5 = Some(9)
• For the None results, we never defined the value and there was no value
to delegate to.
• For a3, we explicitly defined it to be 3.
• a4 wasn’t defined, so it delegates to a3 according to our delegates func-
tion.
• b4 gets the value for a4 (which delegates to a3, so it is 3) and multiplies
by 3
• a5 is defined as the previous value of a5 + 1 and since no previous value
of a5 was defined, it delegates to a4, resulting in 3+1=4.
• b5 isn’t defined explicitly, so it delegates to b4 and is therefore equal to 9
as well
Scopes sbt defines a more complicated scope than the one shown here for the
standard usage of settings in a build. This scope has four components: the
project axis, the configuration axis, the task axis, and the extra axis. Each
component may be Global (no specific value), This (current context), or Se-
lect (containing a specific value). sbt resolves This_ to either Global or Select
depending on the context.
For example, in a project, a This project axis becomes a Select referring to the
defining project. All other axes that are This are translated to Global. Functions
like inConfig and inTask transform This into a Select for a specific value. For
example, inConfig(Compile)(someSettings) translates the configuration axis
for all settings in someSettings to be Select(Compile) if the axis value is This.
So, from the example and from sbt’s scopes, you can see that the core settings
engine does not impose much on the structure of a scope. All it requires is a
delegates function Scope => Seq[Scope] and a display function. You can
choose a scope type that makes sense for your situation.
480
Constructing settings The app, value, update, and related methods are the
core methods for constructing settings. This example obviously looks rather dif-
ferent from sbt’s interface because these methods are not typically used directly,
but are wrapped in a higher-level abstraction.
With the core settings engine, you work with HLists to access other settings.
In sbt’s higher-level system, there are wrappers around HList for TupleN and
FunctionN for N = 1-9 (except Tuple1 isn’t actually used). When working
with arbitrary arity, it is useful to make these wrappers at the highest level
possible. This is because once wrappers are defined, code must be duplicated
for every N. By making the wrappers at the top-level, this requires only one
level of duplication.
Additionally, sbt uniformly integrates its task engine into the settings system.
The underlying settings engine has no notion of tasks. This is why sbt uses a
SettingKey type and a TaskKey type. Methods on an underlying TaskKey[T]
are basically translated to operating on an underlying SettingKey[Task[T]]
(and they both wrap an underlying AttributeKey).
For example, a := 3 for a SettingKey a will very roughly translate to setting(a,
value(3)). For a TaskKey a, it will roughly translate to setting(a, value(
task { 3 } ) ). See main/Structure.scala for details.
Setting Initialization
This page outlines the mechanisms by which sbt loads settings for a particular
build, including the hooks where users can control the ordering of everything.
As stated elsewhere, sbt constructs its initialization graph and task graph via
Setting[_] objects. A setting is something which can take the values stored at
other Keys in the build state, and generates a new value for a particular build
key. sbt converts all registered Setting[_] objects into a giant linear sequence
and compiles them into a task graph. This task graph is then used to execute
your build.
All of sbt’s loading semantics are contained within the Load.scala file. It is
approximately the following:
The blue circles represent actions happening when sbt loads a project. We can
see that sbt performs the following actions in load:
481
Figure 13: image
482
1. Compile the user-level project (~/.sbt/<version>/)
Each of these loads defines several sequences of settings. The diagram shows
the two most important:
or in a build.sbt file:
483
Controlling Initialization
The order which sbt uses to load settings is configurable at a project level.
This means that we can’t control the order of settings added to Build/Global
namespace, but we can control how each project loads, e.g. plugins and .sbt
files. To do so, use the AddSettings class:
import sbt._
import Keys._
import AddSettings._
The AddSettings object provides the following “groups” of settings you can use
for ordering:
• autoPlugins All the ordered settings of plugins after they’ve gone through
dependency resolution
• buildScalaFiles The full sequence of settings defined directly in
project/*.scala builds.
• sbtFiles(*) Specifies the exact setting DSL files to include (files must
use the .sbt file format)
• userSettings All the settings defined in the user directory ~/.sbt/<version>/.
• defaultSbtFiles Include all local *.sbt file settings.
484
For example, let’s see what happens if we move the build.sbt files before the
buildScalaFile.
Let’s create an example project the following definition. project/build.scala:
This build defines a version string which appends the Scala version if the current
Scala version is not the in the 2.10.x series. Now, when issuing a release we
want to lock down the version. Most tools assume this can happen by writing
a version.sbt file. version.sbt:
version := "1.0.0"
However, when we load this new build, we find that the version in version.sbt
has been overridden by the one defined in project/Build.scala because of
the order we defined for settings, so the new version.sbt file has no effect.
Build Loaders
Build loaders are the means by which sbt resolves, builds, and transforms build
definitions. Each aspect of loading may be customized for special applications.
Customizations are specified by overriding the buildLoaders methods of your
build definition’s Build object. These customizations apply to external projects
loaded by the build, but not the (already loaded) Build in which they are defined.
Also documented on this page is how to manipulate inter-project dependencies
from a setting.
Custom Resolver
485
The resolver should return None if it cannot handle the URI or Some containing
a function that will retrieve the build. The ResolveInfo provides a staging
directory that can be used or the resolver can determine its own target directory.
Whichever is used, it should be returned by the loading function. A resolver is
registered by passing it to BuildLoader.resolve and overriding Build.buildLoaders
with the result:
...
object Demo extends Build {
...
override def buildLoaders =
BuildLoader.resolve(demoResolver) ::
Nil
• ResolveInfo
• BuildLoader
Full Example
import sbt._
import Keys._
486
def demoResolver(info: BuildLoader.ResolveInfo): Option[() => File] =
if(info.uri.getScheme != "demo")
None
else
{
// Use a subdirectory of the staging directory for the new local build.
// The subdirectory name is derived from a hash of the URI,
// and so identical URIs will resolve to the same directory (as desired).
val base = RetrieveUnit.temporary(info.staging, info.uri)
// Construct a sample project on the fly with the name specified in the URI.
def resolveDemo(base: File, ssp: String): File =
{
// Only create the project if it hasn't already been created.
if(!base.exists)
IO.write(base / "build.sbt", template.format(ssp))
base
}
version := "1.0"
"""
}
Custom Builder
A builder returns None if it does not want to handle the build identified by
the BuildInfo. Otherwise, it provides a function that will load the build when
evaluated. Register a builder by passing it to BuildLoader.build and overriding
Build.buildLoaders with the result:
...
object Demo extends Build {
487
...
override def buildLoaders =
BuildLoader.build(demoBuilder) ::
Nil
• BuildInfo
• BuildLoader
• BuildUnit
val n = Project.normalizeProjectID(model.getName)
val base = Option(model.getProjectDirectory) getOrElse info.base
val root = Project(n, base) settings( pomSettings(model) )
val build = new Build { override def projects = Seq(root) }
488
val loader = this.getClass.getClassLoader
val definitions = new LoadedDefinitions(info.base, Nil, loader, build :: Nil, Nil)
val plugins = new LoadedPlugins(info.base / "project", Nil, loader, Nil, Nil)
new BuildUnit(info.uri, info.base, definitions, plugins)
}
Custom Transformer
...
object Demo extends Build {
...
override def buildLoaders =
BuildLoader.transform(demoTransformer) ::
Nil
• TransformInfo
• BuildLoader
• BuildUnit
489
need to be known (once all projects are loaded), plugins and build definitions
can transform it to manipulate inter-project dependencies at setting evaluation
time. The only requirement is that no new projects are introduced because
all projects are loaded before settings get evaluated. That is, all Projects
must have been declared directly in a Build or referenced as the argument to
Project.aggregate or Project.dependsOn.
buildDependencies in Global := {
val deps = (buildDependencies in Global).value
val oldURI = uri("...") // the URI to replace
val newURI = uri("...") // the URI replacing oldURI
def substitute(dep: ClasspathDep[ProjectRef]): ClasspathDep[ProjectRef] =
if(dep.project.build == oldURI)
ResolvedClasspathDependency(ProjectRef(newURI, dep.project.project), dep.configuration
else
dep
val newcp =
for( (proj, deps) <- deps.cp) yield
(proj, deps map substitute)
BuildDependencies(newcp, deps.aggregate)
}
490
Creating Command Line Applications Using sbt
There are several components of sbt that may be used to create a command
line application. The launcher and the command system are the two main ones
illustrated here.
As described on the launcher page, a launched application implements the xs-
bti.AppMain interface and defines a brief configuration file that users pass to
the launcher to run the application. To use the command system, an applica-
tion sets up a State instance that provides command implementations and the
initial commands to run. A minimal hello world example is given below.
1. build.sbt
2. Main.scala
3. hello.build.properties
Like for sbt itself, you can specify commands from the command line (batch
mode) or run them at an prompt (interactive mode).
Build Definition: build.sbt The build.sbt file should define the standard
settings: name, version, and organization. To use the sbt command system, a
dependency on the command module is needed. To use the task system, add a
dependency on the task-system module as well.
organization := "org.example"
name := "hello"
version := "0.1-SNAPSHOT"
491
Application: Main.scala The application itself is defined by implementing
xsbti.AppMain. The basic steps are
1. Provide command definitions. These are the commands that are available
for users to run.
2. Define initial commands. These are the commands that are initially sched-
uled to run. For example, an application will typically add anything speci-
fied on the command line (what sbt calls batch mode) and if no commands
are defined, enter interactive mode by running the ‘shell’ command.
3. Set up logging. The default setup in the example rotates the log file after
each user interaction and sends brief logging to the console and verbose
logging to the log file.
package org.example
import sbt._
import java.io.{File, PrintWriter}
/** Sets up the application by constructing an initial State instance with the supported
* and initial commands to run. See the State API documentation for details. */
def initialState(configuration: xsbti.AppConfiguration): State =
{
val commandDefinitions = hello +: BasicCommands.allBasicCommands
val commandsToRun = Hello +: "iflast shell" +: configuration.arguments.map(_.trim)
State( configuration, commandDefinitions, Set.empty, None, commandsToRun, State.newHis
AttributeMap.empty, initialGlobalLogging, State.Continue )
}
/** Configures logging to log to a temporary backing file as well as to the console.
* An application would need to do more here to customize the logging level and
492
* provide access to the backing file (like sbt's last command and logLevel setting).*/
def initialGlobalLogging: GlobalLogging =
GlobalLogging.initial(MainLogging.globalDefault _, File.createTempFile("hello", "log")
}
[scala]
version: 2.9.1
[app]
org: org.example
name: hello
version: 0.1-SNAPSHOT
class: org.example.Main
components: xsbti
cross-versioned: true
[repositories]
local
maven-central
typesafe-ivy-releases: http://repo.typesafe.com/typesafe/ivy-releases/, [organization]/[mod
Nightly Builds
493
launcher is used. This means that you must set sbt.version=yyyyMMdd-HHmmss
in an existing <build-base>/project/build.properties. You can verify the
right version of sbt is being used to build a project by running about.
To reduce problems, it is recommended to not use a launcher jar for one nightly
version to launch a different nightly version of sbt.
494