SBT use preliminary and generate Eclipse project with SBT plugin

Source: Internet
Author: User

I used to manage the Java project with Maven and now I'm writing a Scala project.
But the difference between compiling a Scala project in Scala-ide and SBT has never been clear.
Subject to the article:
http://my.oschina.net/yjwxh/blog/601566
To find out the relevant information

The documents on the official website are especially friendly to Chinese, but it's hard to accept the complicated configuration method when I look at it again
SBT is a build tool that is not integrated in Eclipse-ide
After downloading the installation MSI,/bin and/conf two folders appear in the default path, added to path
It's very unfriendly to download a lot of jar packages after running SBT in CMD, For the first time, I thought it was wrong.
The second time, running all night, finally good.

The first small example:
Create a new Hello folder
Create a new Hw.scala file
Object Hi {
def main (args:array[string]) = println ("hi!")
}
Enter a specific path
Run SBT to generate the target folder under the Hello folder
Running run
Can get the simplest HelloWorld degree of execution

First modification of the first example:
add a file to the Hello folder BUILD.SBT
lazy val root = (Project in file (".")).
Settings (
name:= "Hello",
version:= "1.0",
scalaversion:= "2.11.8"
)
The basic build settings are placed in the configuration file under the root directory .
at least the name and the Version property .
of course, you can specify the SBT version and enter it in the/hello/project/build.properties
sbt.version=0.13.11
forces the specified version of SBT to be used, although the various SBT versions are compatible, the official recommendation is to specify

The catalog of projects used by SBT is similar to MAVEN's
built: Compiled classes, packaged jars, managed files, chches and documents are all by default in the target directory
Here are the options you can perform after entering SBT
Clean Delete files under the TARGT directory
compile compiling source files
Test compile Run all the tests
Console enters a parser containing classpath, quit or ctrl + Z exit
Run < parameters > executes the project's main class on the same virtual machine as where SBT is located
The package Scala,java the class files in the resources and the files into jar packages
Help < commands > Display assistance
Reload re-build

SBT Clean compile "testonly TestA testb"
First the cleanup is performed, then the compilation is performed, and finally the testonly and its two parameters are
One of the bigger orders is
~compile
You can automatically run the compilation while changing the source file, just change the code, the results will come out (two monitors)

Three styles of building definitions:
Multi-project. SBT Build Definition
BARE.SBT Build Definition
. Scala Build Definition

Each project has some immutable mapping tables to describe the project
Map-key
A second example:
Lazy val commonsettings = Seq (
Organization: = "Com.example",
Version: = "0.1.0",
Scalaversion: = "2.11.7"
)

lazy val root = (Project in file (".")).
settings (commonsettings: _*).
Settings (
Name: = "Hello"
)
Name,version,scalaversion on the left, it's all keys .
A key is an instance of a settingkey[t], or taskkey[t], or inputkey[t]
These keys can be set in Java syntax
lazy val root = (Project in file (".")).
Settings (
Name: = ("Hello")
)
Settingkey[t]: A key corresponds to a value that is calculated only once, when the project is loaded, and then it is saved
Taskkey[t]: A key corresponds to the value of a task and is recalculated every time, potentially with potential side effects
Inputkey[t]: A key corresponds to a task that can receive command-line arguments

built-in keys:
The keys built in are actually the fields of the keys of the object
BUILD.SBT will implicitly include the import sbt.keys._
So you can use name to get to Sbt.keys.name
custom keys:

by using the three ways above. Span style= "font-family: ' Microsoft Yahei '; font-size:18px; " > example: lazy val hello = Taskkey[unit] ("a task example")
is defined for a new task, hello.
That means CALs and defs can also be set up for projects other than. SBT, regardless of the location of the configuration
Here's lazy Val instead of Val can avoid the problem of initialization order


There are two ways to add a third-party dependent library:
Put the jar file into the lib/
adding dependencies in BUILD.SBT
Example:
Val Derby = "Org.apache.derby"% "Derby"% "10.4.1.3"

Lazy val commonsettings = Seq (
Organization: = "Com.example",
Version: = "0.1.0",
Scalaversion: = "2.11.7"
)

Lazy val root = (Project in file (".")).
Settings (commonsettings: _*).
Settings
Name: = "Hello",
Librarydependencies + = Derby
)
One of the + + good understanding
The% method is used to construct the Ivy module ID from a string
The following document describes it as a bit more complicated than it looks.


Https://github.com/typesafehub/sbteclipse
for SBT higher than 0.13, you can create a catalog of project files used by Eclipse
There are two ways to use this plugin in SBT:
Configuring in ~/.SBT/0.13/PLUGINS/PLUGINS.SBT
in the project directory go down configuration: PROJECT_DIR/PROJECT/PLUGINS.SBT
The syntax for the latest version of the configuration is:
addsbtplugin ("com.typesafe.sbteclipse"% "sbteclipse-plugin"% "4.0.0")
You can then use the Eclipse command at the command line to generate a directory structure that you can import as an Eclipse project
For example, create the following directory structure
PS d:\javaweb_workspace\scala\sbt_eclipse_test> tree/f.
D:\JAVAWEB_WORKSPACE\SCALA\SBT_ECLIPSE_TEST
│BUILD.SBT

└─project
PLUGINS.SBT
PS d:\javaweb_workspace\scala\sbt_eclipse_test> SBT
Java HotSpot (TM) 64-bit Server VM warning:ignoring option maxpermsize=256m; support is removed in 8.0
[INFO] Loading Global plugins from C:\Users\kongchung\.sbt\0.13\plugins
[INFO] Loading project definition from D:\javaweb_workspace\SCALA\sbt_eclipse_test\project
[Info] Updating {file:/d:/javaweb_workspace/scala/sbt_eclipse_test/project/}sbt_eclipse_test-build ...
[INFO] resolving org.fusesource.jansi#jansi;1.4 ...
[INFO] done updating.
[INFO] Set current project to Sbt_eclipse_test (in Build file:/d:/javaweb_workspace/scala/sbt_eclipse_test/)
> Eclipse
[INFO] about-to-create Eclipse project files for your project (s).
[Info] Updating {file:/d:/javaweb_workspace/scala/sbt_eclipse_test/}sbt_eclipse_test ...
[INFO] resolving org.fusesource.jansi#jansi;1.4 ...
[INFO] done updating.
[INFO] successfully created Eclipse project files for project (s):
[INFO] sbt_eclipse_test
> Exit
PS d:\javaweb_workspace\scala\sbt_eclipse_test> tree/f.
D:\JAVAWEB_WORKSPACE\SCALA\SBT_ECLIPSE_TEST
│.classpath
│.project
│BUILD.SBT

├─.settings
│org.eclipse.core.resources.prefs
│org.scala-ide.sdt.core.prefs

├─project
││PLUGINS.SBT
││
│├─project
││└─target
││└─config-classes
││$0b5cfab2a23c8f87aa49$.class
││$0b5cfab2a23c8f87aa49.cache
││$0b5cfab2a23c8f87aa49.class
││
│└─target
│├─config-classes
│├─resolution-cache
││├─default
│││└─sbt_eclipse_test-build
│││└─scala_2.10
references to this example:
HTTP://WWW.TUICOOL.COM/ARTICLES/F26BJQ
The directory I got is still missing some file directories
These settings can be specified in the BUILD.SBT configuration

SBT use preliminary and generate Eclipse project with SBT plugin

Related Article

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.