Sbt in Action Summary Notes

Notes from Book Sbt in Action

http://www.manning.com/suereth2/


Chapter 1

Maven concepts

Maven has set of lifecycles

Lifecycle
              Phases
                        Goals ( Tasks in Ant)


Maven has following lifecycles

default ( build and deployment )
clean ( used to clean )
site ( used for documentation )

Each phase can have number of goals
Phases are executed in sequence
All previous phases ( and goals ) are executed if you call some phase

You can assign goals to phases via plugins goals

e.g Maven syntax


SBT is better for

Type safety
Ease of making new tasks
Test and development features for incremental builds

SBT concepts

Sbt just has tasks
Tasks can have dependency with each other.
Tasks with no dependency are executed in parallel
Tasks output can be passed on to next output

Settings
Settings are values ( e.g ) Project name

Plugins
If something is reusable tasks which is done again and again in different projects , we can abstract it as plugin.

This plugin can be used in different projects and can be sourced by build file of projects.


Chapter 2

Download and add sbt to your path

http://www.scala-sbt.org/download.html

Every project using sbt should have two files:

• project/build.properties
• build.sbt

build.properties tell which version of sbt to use
build.sbt tells which all settings are applicable for this project , including the library dependencies

start sbt by typing sbt at command prompt


After sbt starts , you can see the help by typing

> help

This will show all of the help topics

> help

  help                                    Displays this help message or prints detailed help on requested commands (run 'help <command>').
  about                                   Displays basic information about sbt and the build.
  tasks                                   Lists the tasks defined for the current project.
  settings                                Lists the settings defined for the current project.
  reload                                  (Re)loads the project in the current directory
  projects                                Lists the names of available projects or temporarily adds/removes extra builds to the session.
  project                                 Displays the current project or changes to the provided `project`.
  set [every] <setting>                   Evaluates a Setting and applies it to the current project.
  session                                 Manipulates session settings.  For details, run 'help session'.
  inspect [uses|tree|definitions] <key>   Prints the value for 'key', the defining scope, delegates, related definitions, and dependencies.
  <log-level>                             Sets the logging level to 'log-level'.  Valid levels: debug, info, warn, error
  ; <command> (; <command>)*              Runs the provided semicolon-separated commands.
  ~ <command>                             Executes the specified command whenever source files change.
  last                                    Displays output from a previous command or the output from a specific task.
  last-grep                               Shows lines from the last output for 'key' that match 'pattern'.
  export <tasks>+                         Executes tasks and displays the equivalent command lines.
  exit                                    Terminates the build.
  --<command>                             Schedules a command to run before other commands on startup.
  show <key>                              Displays the result of evaluating the setting or task associated with 'key'.


> tasks

This will show which all tasks are defined for this project

> settings

This will show which all settings are applicable for this project

For example which is sScalaversion etc

> settings

This is a list of settings defined for the current project.
It does not list the scopes the settings are defined in; use the 'inspect' command for that.

  autoCompilerPlugins            If true, enables automatically generating -Xplugin arguments to the compiler based on the classpath for the plugin configuration.
  autoScalaLibrary               Adds a dependency on scala-library if true.
  baseDirectory                  The base directory.  Depending on the scope, this is the base directory for the build, project, configuration, or task.
  classDirectory                 Directory for compiled classes and copied resources.
  crossPaths                     If true, enables cross paths, which distinguish output directories for cross-building.
  fork                           If true, forks a new JVM when running.  If false, runs in the same JVM as the build.
  initialCommands                Initial commands to execute when starting up the Scala interpreter.
  javaHome                       Selects the Java installation used for compiling and forking.  If None, uses the Java installation running the build.
  javaSource                     Default Java source directory.
  libraryDependencies            Declares managed dependencies.
  managedResourceDirectories     List of managed resource directories.
  maxErrors                      The maximum number of errors, such as compile errors, to list.
  name                           Project name.
  offline                        Configures sbt to work without a network connection where possible.
  organization                   Organization/group ID.
  publishArtifact                Enables (true) or disables (false) publishing an artifact.
  publishTo                      The resolver to publish to.
  resourceDirectory              Default unmanaged resource directory, used for user-defined resources.
  scalaHome                      If Some, defines the local Scala installation to use for compilation, running, and testing.
  scalaSource                    Default Scala source directory.
  scalaVersion                   The version of Scala used for building.
  sourceDirectories              List of all source directories, both managed and unmanaged.
  sourceDirectory                Default directory containing sources.
  target                         Main directory for files generated by the build.
  unmanagedBase                  The default directory for manually managed libraries.
  unmanagedResourceDirectories   Unmanaged resource directories, containing resources manually created by the user.
  unmanagedSourceDirectories     Unmanaged source directories, which contain manually created sources.
  version                        The version/revision of the current module.

More settings may be viewed by increasing verbosity.  See 'help settings'.

> settings name
Project name.

More settings may be searched by increasing verbosity.  See 'help settings'.

> settings scalaVersion
The version of Scala used for building.

More settings may be searched by increasing verbosity.  See 'help settings'.

> name
[info] learn_sbt
> scalaVersion
[info] 2.10.3
>


Starting Scala console

> console

This will start scala console where you can run scala code

> scala

To exist the scala console type

:q

If you change build.sbt ( or Build.scala) you have to reload the settings using

> reload

To run tests

> test

To continuously run tasks

Prefix them by ~

e.g

> ~comple

This will keep on compiling code with each change

Auto completion works in sbt 

> test<TAB>

This will show set of possible options with tasks similar to test

To run only particular test in sbt

> testOnly <TESTNAME>


Chapter 8

When sbt reads your project description,

it reads the .sbt files in the root directory,
the .sbt files in /project and
.scala files in /project.

It compiles everything that it finds and this gives you your build definition. sbt then runs this build and creates your artifact, your jar or whatever.

Difference between dependency and plugin

A plugin is simply a jar which contains settings or tasks. The jar contains a definition
file which sbt reads to work out what settings and tasks are available.

To include a plugin in your build,  add the following lines to project/plugins.sbt:

project/plugins.sbt

addSbtPlugin("org.scalastyle" %% "scalastyle-sbt-plugin" % "0.4.0")

resolvers += "sonatype-releases" at
"https://oss.sonatype.org/content/repositories/releases/"

Repositories of plugins and dependeicies are different from each other , it means  If you've a repository which contains both plugins and dependencies of your artifact, you'll need to include in both project/plugins.sbt and build.sbt.


Version

When sbt resolves the plugin, it appends both the scala version and the sbt version. But the
appended scala version number is not the scala version that you've declared in the build.sbt,
but the version of scala which is used by sbt.

Example

build.sbt

scalaVersion := "2.9.2"

and you are running sbt version 0.13, then the version appended by addSbtPlugin will be
2.10, not 2.9.2, because sbt 0.13 uses version 2.10 of scala. Similarly, if you are running
sbt version 0.12 and you have scalaVersion := "2.10", then the version appended will be
2.9.

Create your own plugin

Define the code in

project/MyPlugin.scala

In build.sbt

Declare the plugin

Call the plugin defined in project/MyPlugin.scala

Credentials

To add credentials for some repo

Use the file

$HOME/.sbt/0.13/credentials.sbt








No comments:

Post a Comment

Please share your views and comments below.

Thank You.