SBT builds Scala eclipse development

Source: Internet
Author: User
Tags jboss artifactory maven central scala ide sonatype

Scala Eclipse SBT Application development

General steps for building eclipse to develop Scala applications

First, the Environment preparation:

1, scala:http://www.scala-lang.org/

2. Scala IDE for eclipse:scala-ide.org

3, sbt:http://www.scala-sbt.org/

4. SBT Eclipse:https://github.com/typesafehub/sbteclipse typesafe an SBT for Eclipse helper that can help build eclipse

5. SBT assembly:https://github.com/sbt/sbt-assembly releases an SBT plugin for the application.

My, Scala version is 2.10.3, the SBT version is 0.13

Ii. SBT generates the Scala Eclipse project:

We want to develop Scala apps in Eclipse and conform to the file structure of the SBT Publisher (similar to the MAVEN architecture), and in addition to creating file structures by hand, you can also use the SBT Eclipse configuration method.

2.1. Add SBT Eclipse Plugin

There are 2 ways to configure:

One is to ~/.sbt/0.13/plugins//build.sbt configure Addplugin in, which is a global plug-in that is used for all SBT projects on this machine.

The other is a different plugins for each project, it is the plug-in configuration in each project and directory under PROJECT/PLUGINS.SBT.

PLUGINS.SBT inside content configuration, add plugin:

Addsbtplugin ("com.typesafe.sbteclipse"% "sbteclipse-plugin"% "2.4.0")
2.2. Generate Eclipse Project files

Then go to the root of SBT and successfully enter SBT and run the Eclipse command to generate eclipse-related files such as the. Classpath of the eclipse:

You can see that the directory structure with MAVEN is similar:

Src
├──main
│├──java
│└──scala
└──test
├──java
└──scala

Found no resouces directory:

In the BUILD.SBT with the directory, add:

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

Execute SBT Eclipse again

src/
├──main
│├──java
│├──resources
│└──scala
└──test
├──java
├──resources
└──scala

2.3. Import in Eclipse

We entered eclipse and imported existing project into workspace with the Import Wizard.

III. Development and deployment

Here's a practical example to illustrate the general steps developed in Scala, the recent use of JSON in Scala, using Json4s, the JSON Lib, to develop an example of parsing JSON, JSON4S address: https://github.com/json4s/json4s

3.1. Add dependencies

If we want to use third-party classes, we need to add dependencies, and Gav coordinates, this is more familiar, but we need to edit the root directory of the BUILD.SBT file, add dependencies:

Here name,version,scalaversion to pay attention to each interval row, the other is also, otherwise it will be wrong.

Librarydependencies is the place to add dependencies: we add 2.

Resolvers is the warehouse address, where multiple configurations are configured.

Name: = "SHENGLI_TEST_SBT"

Version: = "1.0"

Scalaversion: = "2.10.3"

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

Librarydependencies ++= Seq (
"Org.json4s" percent "json4s-native"% "3.2.10",
"Org.json4s" percent "Json4s-jackson"% "3.2.10"
)

resolvers ++= Seq (
HTTPS is unavailable for Maven Central
"Maven Repository" at "Http://repo.maven.apache.org/maven2",
"Apache Repository" at "https://repository.apache.org/content/repositories/releases",
"JBoss Repository" at "https://repository.jboss.org/nexus/content/repositories/releases/",
"MQTT Repository" at "https://repo.eclipse.org/content/repositories/paho-releases/",
"Cloudera Repository" at "http://repository.cloudera.com/artifactory/cloudera-repos/",
For Sonatype Publishing
"Sonatype-snapshots" at "Https://oss.sonatype.org/content/repositories/snapshots",
"Sonatype-staging" at "https://oss.sonatype.org/service/local/staging/deploy/maven2/",
Also check the local Maven repository ~/.m2
Resolver.mavenlocal
)

When you run SBT Eclipse again, the dependent jar packages are automatically loaded into the classpath:

3.2. Test procedure

A simple program to parse the JSON, the program is very simple, here is not explained.

Package Com.shengli.json
Import Org.json4s._
Import Org.json4s.jsondsl._
Import Org.json4s.jackson.jsonmethods._

Object Jsonsbttest extends application{


Case Class Winner (Id:long, Numbers:list[int])
Case Class Lotto (Id:long, Winningnumbers:list[int], Winners:list[winner], drawdate:option[java.util.date])

Val winners = list (Winner, List (2, 3, 5)), Winner (List (52, 3, 12, 11, 18, 22)))
Val Lotto = Lotto (5, List (2, 7, 5, 3), winners, None)
Val json =
("Lotto"
("Lotto-id", lotto.id) ~
("Winning-numbers", lotto.winningnumbers) ~
("Draw-date", Lotto.drawDate.map (_.tostring)) ~
("Winners"
Lotto.winners.map {w =
(("Winner-id", w.id) ~
("Numbers", w.numbers)))

println (Compact (render (JSON)))
println (Pretty (Render (JSON)))
}

So now we can run the run as Scala application in Eclipse, but how do we rely on the package release?

3.3, Assembly

Do you remember the assembly in spark? That's the kind of command that's published, which SBT natively supports, such as the clean compile package, but the one jar that relies on it is assembly more mature.

SBT itself commands: Refer to http://www.scala-sbt.org/0.13/tutorial/Running.html and http://www.scala-sbt.org/0.13/docs/ Command-line-reference.html

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.
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 compiled 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.

Assembly :

Assembly is used as a plug-in, so to be configured in the PLUGINS.SBT under Project, the contents of this PLUGINS.SBT file are as follows:

resolvers + = Resolver.url ("artifactory", url ("Http://scalasbt.artifactoryonline.com/scalasbt/sbt-plugin-releases" )) (Resolver.ivystylepatterns)

resolvers + = "Typesafe Repository" at "http://repo.typesafe.com/typesafe/releases/"

Addsbtplugin ("com.typesafe.sbteclipse"% "sbteclipse-plugin"% "2.4.0")

Addsbtplugin ("com.eed3si9n"% "sbt-assembly"% "0.11.2")

In addition to the configuration of the plug-in, you also need to configure the directory with the BUILD.SBT, support assembly, the file header to join:

Import Assemblykeys._
Assemblysettings

The contents of this BUILD.SBT file are as follows:

Import Assemblykeys._
Assemblysettings

Name: = "SHENGLI_TEST_SBT"

Version: = "1.0"

Scalaversion: = "2.10.3"

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

Librarydependencies ++= Seq (
"Org.json4s" percent "json4s-native"% "3.2.10",
"Org.json4s" percent "Json4s-jackson"% "3.2.10"
)

resolvers ++= Seq (
HTTPS is unavailable for Maven Central
"Maven Repository" at "Http://repo.maven.apache.org/maven2",
"Apache Repository" at "https://repository.apache.org/content/repositories/releases",
"JBoss Repository" at "https://repository.jboss.org/nexus/content/repositories/releases/",
"MQTT Repository" at "https://repo.eclipse.org/content/repositories/paho-releases/",
"Cloudera Repository" at "http://repository.cloudera.com/artifactory/cloudera-repos/",
For Sonatype Publishing
"Sonatype-snapshots" at "Https://oss.sonatype.org/content/repositories/snapshots",
"Sonatype-staging" at "https://oss.sonatype.org/service/local/staging/deploy/maven2/",
Also check the local Maven repository ~/.m2
Resolver.mavenlocal
)

Run the SBT assembly command to publish:

> assembly
[INFO] Updating {FILE:/HOME/VICTOR/WORKSPACE/TEST_SBT/}TEST_SBT ...
[INFO] Resolving org.fusesource.jansi#jansi;1.4 ...
[INFO] Done updating.
[INFO] Compiling 1 Scala Source to/home/victor/workspace/test_sbt/target/scala-2.10/classes ...
[Warn] There were 1 deprecation warning (s); Re-run with-deprecation for details
[Warn] one warning found
[INFO] Including:scala-compiler-2.10.0.jar
[INFO] Including:scala-library-2.10.3.jar
[INFO] Including:json4s-native_2.10-3.2.10.jar
[INFO] Including:json4s-core_2.10-3.2.10.jar
[INFO] Including:json4s-ast_2.10-3.2.10.jar
[INFO] Including:paranamer-2.6.jar
[INFO] Including:scalap-2.10.0.jar
[INFO] Including:jackson-databind-2.3.1.jar
[INFO] Including:scala-reflect-2.10.0.jar
[INFO] Including:jackson-annotations-2.3.0.jar
[INFO] Including:json4s-jackson_2.10-3.2.10.jar
[INFO] Including:jackson-core-2.3.1.jar
[INFO] Checking every *.class/*.jar file ' s SHA-1.
[INFO] Merging files ...
[Warn] Merging ' Meta-inf/notice ' with strategy ' rename '
[Warn] Merging ' meta-inf/license ' with strategy ' rename '
[Warn] Merging ' Meta-inf/manifest. MF ' with strategy ' discard '
[Warn] Merging ' rootdoc.txt ' with strategy ' concat '
[Warn] Strategy ' concat ' was applied to a file
[Warn] Strategy ' discard ' was applied to a file
[Warn] Strategy ' Rename ' was applied to 2 files
[INFO] SHA-1: d4e76d7b55548fb2a6819f2b94e37daea9421684
[INFO] Packaging/home/victor/workspace/test_sbt/target/scala-2.10/shengli_test_sbt-assembly-1.0.jar ...
[INFO] Done packaging.
[Success] Total time:39 S, completed 4, 1:26:11 AM

Iv. Summary

This article describes the general steps of using SBT to build a Scala program in Eclipse and explains the entire process with an example.

Using the SBT Eclipse plugin to generate the SBT file directory structure, the SBT Eclipse command generates an update jar package dependency.

Use the assebly plugin to package and publish Scala apps.

SBT builds Scala eclipse development

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.