Sbt-simple Build Tool Primer

Source: Internet
Author: User

Sbt-simple Build Tool Primer


SBT Source file directory structure
Src/main/resources/<files to include in main jar here> scala/<main Scala sources> ja va/<main Java sources> test/resources <files to include in test jar here> scala/&L T;test Scala sources> java/<test java sources>


SBT Command-line mode

Create a directory Hello, create the source file Hw.scala, enter command-line mode in this directory ,

Common commands are,

Clean Delete all the generated files (in the target directory).
Compile Compile the source files (in the Src/main/scala and Src/main/java directories).
Test Compile and run all tests.
Console Go to a Scala parser that contains all the compiled files and all dependent classpath. Input: Quit, Ctrl+d (Unix), or CTRL + Z (Windows) returns to SBT.
Run < parameter >* Executes the main class of the project on the same virtual machine as where SBT is located.
Package Package the files under Src/main/resources and Src/main/scala and the class files compiled in Src/main/java into a jar file.
Help < commands > Displays detailed help information for the specified command. If no command is specified, an introduction to all commands is displayed.
Reload Reload the build definition (BUILD.SBT, Project/*.scala, PROJECT/*.SBT the content defined in these files). A reload is required after the build definition file has been modified.

As follows

[Email protected]/e/test-sbt/hello$ Sbt[info] Set current project to Hello (in Build file:/e:/test-sbt/hello/) >

Execute the following command,

[email protected] /e/test-sbt/hello$ sbt[info] set current project to  hello  (in build file:/e:/test-sbt/hello/) > clean[success] total time: 0  s, completed 2015-1-6 16:22:45> compile[info] updating {file:/e:/ Test-sbt/hello/}hello ... [info] resolving org.scala-lang#scala-library;2.10.4 ...  [info] resolving  org.scala-lang#scala-compiler;2.10.4 ...  [info] resolving org.scala-lang# scala-reflect;2.10.4 ...  [info] resolving org.scala-lang#jline;2.10.4 ...  &NBSP;[INFO]&NBSP;RESOLVING&NBSP;ORG.FUSESOURCE.JANSI#JANSI;1.4&NBSP, ..... [Info] done updating. [Info] compiling 1 scala source to e:\test-sbt\hello\target\scala-2.10\classes ... [Success] total time: 4 s, completed 2015-1-6 16:22:53> run[info]  running hihi! [success] total time: 0 s, completed 2015-1-6 16:22:58>


Configuration file. SBT. SBT vs. Scala build statement definition

A project's build definition can be a file that ends with an. SBT suffix in the project root directory, or a file that ends in a. Scala directory under subdirectory project

This chapter focuses on the. sbt file definition, which is already well-suited for most situations. The Scala definition approach is typically used in multiple. sbt files to share shared definition statements or complex project constructs. For more information, see. Scala definition


What is a build statement?

By validating and parsing the build statement file, SBT describes the completion of the build process with an immutable Map (Key-value key-value pair)

For example, a key is name and its Map value is a string, and this configuration item name represents this item

The build statement definition does not directly modify the map that affects SBT

Instead, the build statement is a large list of objects of type setting[t], T is the type of Setting Map value, and Setting can modify the map by adding a new key-value pair, modifying the value of the key that exists ( An immutable data structure and value in functional programming, modifying values by new assignments, rather than modifying them on the basis of the original value

In BUILD.SBT you may want to create a setting[string] type of configuration item stating the project name:

Name: = "Hello"

This setting[string] is modified by adding or replacing the original key value, and the assigned value is "Hello", and the modified map becomes a new map. Create a MAP,SBT first configuration list all modified configurations are placed in one piece, and if some configuration values depend on certain configuration items will be processed after the dependent configuration item. Then SBT builds a new map with a sorted configuration item

Summary: The build statement is a list of setting[t], Setting[t] is a map Key-value key-value pair that can be modified, and T is the type of the key-value pair value


How to define BUILD.SBT configuration items

BUILD.SBT is a seq[setting[_]], is a series of empty lines to split the Scala expression, each line is an element of the sequence.

For example:

Name: = "Hello" version: = "1.0" scalaversion: = "2.10.3"

Each configuration item is a Scala expression, and the expression in BUILD.SBT is independent rather than a Scala code block. These expressions are made up of Val, Lazy Val, and Def, and objects and classes are not allowed to be defined in BUILD.SBT and should be defined in the. Scala source code files in the subdirectory project.

The left value of the configuration expression, such as name, version and Scalaversion, is the instance of the configuration key, key is Settingkey[t], taskkey[t], or inputkey[t], where T is the type of expected value, and the type of the specific key is described below.

The Key object has a method: = Call will return Setting[t], you can call it in Java syntax style:

name.:= ("Hello")

Allow Name: = "Hello" invocation in Scala (this form of invocation is allowed in Scala for a single argument)

Name: = method returns a Setting object with the specific type setting[string], and the generic type String also appears in the name key itself definition, but the name type is settingkey[string]. In this example, the returned setting[string] object is added or replaced by a new SBT configuration item map, given a value of "Hello"

If a configuration value of one of the error types is given, it cannot be compiled by

Name: =//would not compile
The configuration items must be split with a blank line

The configuration is not allowed to be written in the following format BUDIL.SBT:

Won't compile, not blank linesname: = "Hello" version: = "1.0" scalaversion: = "2.10.3"

SBT requires a delimiter to determine the end of an expression and the start of another expression, and the. sbt file contains a series of Scala expressions, not a single Scala project, which must be separated and compiled separately.


KeysType

SETTINGKEY[T]: The value of this type of key is calculated only once (it will remain after the calculation is completed when the project is loaded)

TASKKEY[T]: The value of this type of key will be called as a task, can be repeated call calculation, may also have an impact

INPUTKEY[T]: This type of key is for a task to pass some input parameters

Built-in Keys

The built-in keys are member variables of the calling object keys and implicitly include import SBT for BUILD.SBT. Keys._, so SBT. Keys.name can write name

Customize Keys

Custom keys can be created using Settingkey, Taskkey, and Inputkey methods, respectively. Each method defines the type of the expected associated value and the description of the key, and each key's name is stored in a constant in Val, for example, a key that defines a task type called Hello

Lazy val Hello = Taskkey[unit] ("An example task")

. SBT can contain definitions of vals and defs, all of which are executed before the configuration entry is parsed, Vals and defs definitions must be separated from all configuration items with a blank line

Note: In general, it is recommended to replace with the lazy Val with Val to avoid initializing the issue of Merit order

Task keys and configuration keys

Taskkey[t] is called a task, task operations such as compile or package, they may return a unit type (unit in Scala equivalent to void) or return an associated task, such as the package this task will return a Taskke Y[file] Create a Jar package task

When launching a task, such as executing compile in interactive mode, SBT will perform tasks related to that task, and the SBT Project description table (map) can hold a string of configuration items (such as name configuration items), It can also be a task that saves an executable block of code (for example, a compile task), even if an actionable task returns a string that can be repeated at any time.

Defining task and Settings

You can use: = Assign a value to a configuration item or task, and the value of the configuration item will be calculated once when the item is loaded, and the calculation will be performed when the task is executed.

For example:

Taskhello: = {println ("hello!")} Settingsname: = "Hello"
Types of tasks and configuration items

Setting with a task key and a configuration key created by a slight difference, taskkey: = 42 result is setting[task[t]] however settingkey: = 42 result is setting[t]. For the most part the difference is basically seen because the task key still creates a value of type T when the task executes

T and Task[t] different meanings: A configuration item cannot depend on a task because the configuration item is evaluated only once when the project is initialized.

The interactive mode of Keys in SBT

In SBT interactive mode, you can perform any task key, when input compile will execute the task key to compile task, if the given key type is not a task but a configuration item will output the value of the configuration item, if key is the execution result of the task type will not output to the terminal , it depends on the output of the task to execute show <task name> instead of <task name>. The customary definition of the SBT key is named after the same hump as the Scala naming style.

For more information on a key, you can use inspect <keyname> in interactive mode to see the type of value of the key and the summary description information, etc.

Importing packages in BUILD.SBT

You can use the import statement again at the top of the BUILD.SBT, and they don't need to be split with blank lines. SBT implicitly imports the following packages by default:

Import Sbt._import Process._import keys._
Add a Dependency package

There are two ways to add a dependency to a third-party library, one is to put the jar package directly into the Lib directory, and the other is to add a dependency configuration in BUILD.SBT, as in the following method:

Librarydependencies + = "Org.apache.derby"% "Derby"% "10.4.1.3"

The above configuration will add an Apache Derby library to the project, and the version is 10.4.1.3

Librarydependencies key contains two methods: + = (not: =) and%, + = is a new value appended to the original value instead of replacing the original value, more explanations can refer to configuration configuration items. The% method function is to construct an Ivy module ID that is parsed in the dependent library.


See more: http://sbt.scala.so/

======================end======================


Sbt-simple Build Tool Primer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.