Scala Eclipse SBT Application development

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

Because Scala has a more complete Eclipse IDE (Scala IDE for Eclipse), it's easy, fast, and convenient for Dev who doesn't want to migrate from Eclipse to the IEA platform. It is particularly important to compile and package the development of Scala applications under Eclipse. SBT is a build tool similar to MAVEN, which we will use to build the publishing program.

This article describes the general steps for building an Eclipse development Scala application, together with an example that demonstrates how the SBT tool creates project files in Eclipse and compiles packaged deployment programs.

Here to do a memo, but also for beginners less detours and make a little contribution.

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.

The above listed are the software environments that are required for development:

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.

such as TEST_SBT:

[Email protected]:~/workspace/test_sbt$ PWD/HOME/VICTOR/WORKSPACE/TEST_SBT
victor-ubuntu:~/workspace/test_sbt$ Tree. ├──build.sbt└──project    └──plugins.sbt1 directory, 2 files
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:

[Email protected]:~/workspace/test_sbt$ lltotal 28drwxrwxr-x 5 Victor Victor 4096  August  4 00:38./drwxrwxr-x 8 Victo R Victor 4096  August  4 00:28.. /-rw-rw-r--1 Victor Victor    0  August  4 00:38 build.sbt-rw-rw-r--1 Victor Victor  589  August  4 00:38. Classpathdrwxrwxr-x 4 Victor Victor 4096  August  4 00:38 project/-rw-rw-r--1 Victor Victor  362  August  4 00:38 projectdrwxrwxr-x 4 Victor Victor 4096  August  4 00:38 src/drwxrwxr-x 4 Victor Victor 4096  August  4 00 : target/
You can see that the directory structure with MAVEN is similar:
[Email protected]:~/workspace/test_sbt$ tree srcsrc├──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
[Email protected]:~/workspace/test_sbt$ tree src/src/├──main│├──java│├──resources│└──scala└──test    ├──ja VA    ├──resources    └──scala

2.3. Import in Eclipse

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


At this point, our project structure is complete and we can develop the Scala SBT project just like the Java Maven project.

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.

As follows:

Name: = "SHENGLI_TEST_SBT" Version: = "1.0" scalaversion: = "2.10.3" ECLIPSEKEYS.CREATESRC: = Eclipsecreatesrc.default + Eclipsecreatesrc.resourcelibrarydependencies ++= Seq (  "org.json4s" percent "json4s-native"% "3.2.10",  " Org.json4s "percent" Json4s-jackson "%" 3.2.10 ") resolvers ++= Seq (     //HTTPS is unavailable for Ma Ven 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    & nbsp 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.jsonimport 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 (23, List (2, 3, 5)), Winner (Si, List (3, one, one, and up)))  val Lotto = Lotto (5, List (2, 45, 34, 23, 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 fromSRC/MAIN/SCALA andSrc/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._assemblysettingsname: = "SHENGLI_TEST_SBT" Version: = "1.0" scalaversion: = "2.10.3" ECLIPSEKEYS.CREATESRC: = Eclipsecreatesrc.default + eclipsecreatesrc.resourcelibrarydependencies ++= Seq ("org.json4s "percent" json4s-native "%" 3.2.10 "," org.json4s "percent" Json4s-jackson "%" 3.2.10 ") resolvers ++= Seq (//HTTPS is Unavaila  ble 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.O Rg/nexus/content/repositories/releases/"," MQTT Repository "at" Https://repo.eclipse.org/content/repositories/pa Ho-releases/"," Cloudera Repository "at" http://repository.cloudera.com/artifactory/cloudera-repos/",//For so Natype Publishing//"Sonatype-snapshots" at "https://oss.sonatype.org/content/repositories/snapshots",//"so Natype-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 ' is applied to a fi Le[warn] strategy ' discard ' was applied to a File[warn] strategy ' rename ' were applied to 2 files[info] SHA-1: d4e76d7b5554 8fb2a6819f2b94e37daea9421684[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
We found the file published to/home/victor/workspace/test_sbt/target/scala-2.10/shengli_test_sbt-assembly-1.0.jar.

Let's execute it and see if it works:

Perform:

Java-cp/home/victor/workspace/test_sbt/target/scala-2.10/shengli_test_sbt-assembly-1.0.jar Com.shengli.json.JsonSbtTest

[Email protected]:~/workspace/test_sbt$ java-cp/home/victor/workspace/test_sbt/target/scala-2.10/shengli_test_ Sbt-assembly-1.0.jar com.shengli.json.jsonsbttest{"Lotto": {"Lotto-id": 5, "winning-numbers": [2,45,34,23,7,5,3], " Winners ": [{" Winner-id ":" Numbers ": [2,45,34,23,3,5]},{" Winner-id ": si," numbers ": [52,3,12,11,18,22]}]}}{  ] Lotto ": {    " Lotto-id ": 5,    " Winning-numbers ": [2, 7, 5, 3],    " winners ": [{      " Winner-id ": 2  3,      "Numbers": [2, 3, 5]    }, {      "Winner-id": si,      "numbers": [, 3, one, one, one]    } ]  }}

Confirmed that the results of the release were successful.

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.

--eof--

Original articles, reproduced please specify from: http://blog.csdn.net/oopsoom/article/details/38363369


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.