Build a Scala project with SBT

Source: Internet
Author: User

Now that you've decided to work on Scala, it's going to be thorough. As we do as we do, we learn SBT. SBT uses ivy as a library management tool. Ivy defaults to building the library repository under user home.

Installing SBT

Download the appropriate version of SBT on the SBT website (http://www.scala-sbt.org/).

It is recommended to configure SBT environment variables in the Windows environment. (Development on Windows really hurts.) The specific steps do not say, is to configure the Sbt/bin directory into path.

There is a sbt.bat under Sbt/bin to view the bat file. You can see that the file relies on Conf/sbtconfig.txt.

Add in Conf/sbtconfig.txt:

-dfile.encoding=utf8-dsbt.boot.directory=d:/sbt-repository/boot/-dsbt.ivy.home=d:/ sbt-repository/

Description

-dsbt.boot.directory-dsbt.ivy.home

Both are configuration sbt.boot directories and Ivy.home directories, where information such as cache jar packages are stored. Information such as the jar package is stored in the user home directory by default.

In addition, the JVM parameters that run SBT can also be specified in Sbtconfig.txt.

SBT Command-line environment

Enter the project directory at the command line, enter SBT, and go to the SBT command line. (It is recommended to enter the project catalog before running the SBT command.) )

Note: The first time you use the SBT command, the program will download the required jar packages. The whole process takes a long time and you have to wait patiently. When the ">" prompt appears in the window, SBT has successfully started. After the download is complete, the display is as follows:

SBT command

Help commands

View a detailed help document for a command

Show Project configuration Show

Show Name: View the name of the current project.

Show Librarydependencies: View the library that the current project depends on.

Using the Scala console console

Enter console enter to launch a REPL within the current session. SBT loads all the jar packages and current code that are dependent on the current project. You can experiment with your semi-finished product in this interpreter.

Because the dependent jar packages are also loaded, you can have a good time in the console for third-party libraries that you may not be familiar with!

Running the Scala program run

If an item has an object with a method (or if the object inherits App quality (trait)), you can run the code in SBT by entering run.

When SBT discovers that there are multiple main methods, it asks which one you want to execute.

Build a project with SBT

Create the project directory under the Projects directory.

HELLO-SBT is the project directory. Project is its child directory.

Create the Build.properties file in the project directory. Writes the version using SBT to the file. If this machine does not have a corresponding version, SBT will download the required version automatically. (This machine uses the current latest SBT version 0.13.9)

Under Project directory, create BUILD.SBT, fill in the corresponding project configuration

Then on the command line, into the current project, using the SBT command, when you see the angle bracket prompt, that is the creation success. (Note: The first use of SBT will take a long time to download the appropriate files.) )

Import an SBT project into eclipse

Using SBT's corresponding profile in eclipse requires the Sbteclipse plugin to be installed, and the Sbteclipse plugin will generate the files needed for the Eclipse project based on the SBT configuration.

Sbteclipse official address is https://github.com/typesafehub/sbteclipse which has installation instructions.

Create the PLUGINS.SBT in the project directory of the engineering catalog, where the plug-in content is added.

Execute Reload Command

You can then generate the Eclipse project file by executing the Eclipse command directly.

Import the project in Eclipse:

If you did not create the SRC directory, manually create the following directory, and then rerun the Eclipse command.

Src/main/scala

Src/main/java

Src/main/resources

Src/test/scala

Src/test/scala

Src/test/resources

The items that are imported into eclipse appear as follows:

If you find that there is no resources in the SRC directory to try to create manually, you can also add the following configuration in BUILD.SBT.

ECLIPSEKEYS.CREATESRC: = Eclipsecreatesrc.default + Eclipsecreatesrc.resource

Anyway, it's simple.

SBT and Scala programs

Write the HELLOSBT program.

Write the HELLOSBT program in Src/main/scala, and the resulting Eclipse project is as follows:

Using SBT to compile the program

Enter compile under the SBT command line

Run the program with SBT

Enter run under the SBT command line

Importing an SBT project into INETLLJ idea

Project Import INETLLJ idea, you need to select the BUILD.SBT file instead of the project file. INETLLJ idea will display a dialog box.

Note There are some advanced options that you can set.

SBT has a Chinese version of the document: http://www.scala-sbt.org/0.13/tutorial/zh-cn/index.html

--------------------------------------more content to write--------------------------------------------

Build a Scala project with SBT

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.