Scala eclipse sbt Application Development
Scala has a relatively complete Eclipse IDE (Scala IDE for Eclipse). for developers who do not want to migrate from eclipse to the Iea platform, it is particularly important to compile and package Scala applications in Eclipse to develop Scala applications conveniently, quickly, and effectively. Sbt is a building tool similar to Maven. We will use it to build the publishing program.
This article describes how to build an Eclipse Scala application and how to use the sbt tool to create a project file in eclipse and compile, package, and deploy the program.
Here is a memo, which also makes a small contribution for beginners to avoid detours.
I. 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 assistant, can help generate eclipse
5. Sbt Assembly: a sbt plug-in for the https://github.com/sbt/sbt-assembly release application.
The above lists all the software environments required for development:
My Scala version is 2.10.3 and Sbt version is 0.13
Ii. scala eclipse project generated by sbt:
We want to develop scala applications in Eclipse and conform to the file structure of the sbt release program (similar to the Maven structure). In addition to creating the file structure manually, we can also use the sbt eclipse configuration method.
2.1 Add the sbt eclipse plug-in
There are two configuration methods:
One is in~/.sbt/0.13/plugins//build.sbt
Configure addPlugin. This is a global plug-in, that is, it is used for all sbt projects on the local machine.
The other is plugins of different projects, which are configured in the project/plugins. sbt of each project and directory.
For example, test_sbt:
victor@victor-ubuntu:~/workspace/test_sbt$ pwd/home/victor/workspace/test_sbt
victor-ubuntu:~/workspace/test_sbt$ tree ..├── build.sbt└── project └── plugins.sbt1 directory, 2 files
Configure the content in plugins. sbt and add the plug-in:
addSbtPlugin(com.typesafe.sbteclipse % sbteclipse-plugin % 2.4.0)
2.2 generate an eclipse project file
Then go to the root directory sbt and enter sbt. Run the eclipse command to generate eclipse-related files such as. classpath:
Victor @ victor-ubuntu :~ /Workspace/test_sbt $ lltotal 28drwxrwxr-x 5 victor 4096 August 4 00:38. /drwxrwxr-x 8 victor 4096 August 4 00:28 .. /-rw-r -- 1 victor 0 August 4 00:38 build. sbt-rw-r -- 1 victor 589 August 4 00:38. classpathdrwxrwxr-x 4 victor 4096 August 4 00:38 project/-rw-r -- 1 victor 362 August 4 00:38. projectdrwxrwxr-x 4 victor 4096 August 4 00:38 src/drwxrwxr-x 4 victor 4096 August 4 00:38 target/
The directory structure is similar to that of maven:
victor@victor-ubuntu:~/workspace/test_sbt$ tree srcsrc├── main│ ├── java│ └── scala└── test ├── java └── scala
No resouces directory found:
Add:
EclipseKeys.createSrc := EclipseCreateSrc.Default + EclipseCreateSrc.Resource
Then execute sbt eclipse
victor@victor-ubuntu:~/workspace/test_sbt$ tree src/src/├── main│ ├── java│ ├── resources│ └── scala└── test ├── java ├── resources └── scala
2.3 import Eclipse
Go to Eclipse and use the import existing project into workspace in the import Wizard to import data.
So far, our project structure has been built, and we can develop scala sbt projects just like the java maven project.
Iii. Development and deployment
The following preparation with a practical example to demonstrate the general steps of Scala Development, recently used in scala json, json4s json lib to develop a parsing json example, json4s address: https://github.com/json4s/json4s
3.1 add dependency
If we want to use a third-party class, we need to add dependencies and GAV coordinates. However, we need to edit the build. sbt file in the root directory and add dependencies:
Here name, version, scalaVersion should pay attention to each interval of one line, the others are also, otherwise there will be an error.
LibraryDependencies is the place where dependencies are added: we add two.
Resolvers is the repository address. Multiple repository addresses are configured here.
As follows:
name := shengli_test_sbtversion := 1.0scalaVersion := 2.10.3EclipseKeys.createSrc := EclipseCreateSrc.Default + EclipseCreateSrc.ResourcelibraryDependencies ++= Seq( org.json4s %% json4s-native % 3.2.10, org.json4s %% 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)
If you run sbt eclipse again, the dependent jar package is automatically loaded to classpath:
3.2 Test Procedure
A simple Json parsing program, the program is very simple, I will not explain it here.
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, 45, 34, 23, 3, 5)), Winner(54, List(52, 3, 12, 11, 18, 22))) 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 far, we can Run the Run as Scala Application in eclipse, but how can we add dependencies for packaging and publishing?
3.3. Assembly
Do you still remember the assembly in Spark? That is for release. sbt itself supports commands such as clean compile package, but the one jar package with dependency is still more mature in assembly mode.
Sbt itself command: Reference 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 (inTargetDirectory ). |
Compile |
Compiles the main sources (inSrc/main/scalaAndSrc/main/javaDirectories ). |
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 * |
Runs the main class for the project in the same virtual machine as sbt. |
Package |
Creates a jar file containing the files inSrc/main/resourcesAnd the classes compiled fromSrc/main/scalaAndSrc/main/java. |
Help |
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/*. sbtFiles). Needed if you change the build definition. |
Assembly:
Assembly is a plug-in, so you need to configure it in plugins. sbt under the project. The contents of 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 plug-in configuration, you also need to configure build. sbt under the Directory and support assembly. Add the following to the file header:
import AssemblyKeys._assemblySettings
The content of the build. sbt file is as follows:
import AssemblyKeys._assemblySettingsname := shengli_test_sbtversion := 1.0scalaVersion := 2.10.3EclipseKeys.createSrc := EclipseCreateSrc.Default + EclipseCreateSrc.ResourcelibraryDependencies ++= Seq( org.json4s %% json4s-native % 3.2.10, org.json4s %% 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 for release:
> 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 Aug 4, 2014 1:26:11 AM
We found the file was published to/home/victor/workspace/test_sbt/target/scala-2.10/shengli_test_sbt-assembly-1.0.jar
Run the following command to check whether the task is successful:
Run:
Java-cp/home/victor/workspace/test_sbt/target/scala-2.10/shengli_test_sbt-assembly-1.0.jar com. shengi. json. JsonSbtTest
victor@victor-ubuntu:~/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:23,numbers:[2,45,34,23,3,5]},{winner-id:54,numbers:[52,3,12,11,18,22]}]}}{ lotto : { lotto-id : 5, winning-numbers : [ 2, 45, 34, 23, 7, 5, 3 ], winners : [ { winner-id : 23, numbers : [ 2, 45, 34, 23, 3, 5 ] }, { winner-id : 54, numbers : [ 52, 3, 12, 11, 18, 22 ] } ] }}
Confirm that the publishing result is successful.
Iv. Summary
This article describes the general steps of using Sbt in Eclipse to build and develop Scala programs, and explains the entire process with examples.
Use the sbt eclipse plug-in to generate the sbt file directory structure, and use the sbt eclipse command to generate and update jar package dependencies.
Use the assebly plug-in to package and release scala applications.