1. SBT doesn’t include a command to create a new project
A shell script to build a Scala SBT project directory structure
http://alvinalexander.com/sbtmkdirs
#!/bin/sh mkdir -p src/{main,test}/{java,resources,scala} mkdir lib project target # create an initial build.sbt file echo 'name := "MyProject" version := "1.0" scalaVersion := "2.10.0"' > build.sbt
2.
From the root directory of the project, you can compile the project:
$ sbt compile
Run the project:
$ sbt run
Package the project as a jar file"
$ sbt package
Run all test class in the test folder:
$ sbt test
Run a single test case
$ set test-only Classname
Generate the files Eclipse needs
$ sbt eclipse
Generate the files IntelliJ needs
$ sbt gen-idea
3. Dependencies Management
If you have JAR files (unmanaged dependencies) that you want to use in your project, simply copy them to the lib folder in the root directory of your project, and SBT will find them automatically.
The
groupID
, artifactID
, revision
, and configuration
strings correspond to what Ivy
requires to retrieve the module you want.libraryDependencies += groupID % artifactID % revision % configuration
To add multiple managed dependencies to your project, define them as a
Seq
in your build.sbt file: libraryDependencies ++= Seq( "net.sourceforge.htmlcleaner" % "htmlcleaner" % "2.4", "org.scalatest" % "scalatest_2.10" % "1.9.1" % "test", "org.foobar" %% "foobar" % "1.8" )
The
%%
method adds your project’s Scala
version to the end of the artifact name. 4. Use an SBT plug-in sbt-assembly to build a single, complete JAR file that can be executed with a simple
java
command. This requires that
Java is installed on client systems.(1) Create file plugins.sbt in the "project" folder:
resolvers += Resolver.url("artifactory", url("http://scalasbt.artifactoryonline.com/scalasbt/sbt-plugin-releases"))(Resolver.ivyStylePatterns) addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.11.2")
Note: the folder structure is: MyProject/project/plugins.sbt
(2) Add these two lines to the top of your build.sbt file
import AssemblyKeys._ assemblySettings
(3) $ sbt assembly
5. You have multiple
main
methods
in objects in your project, and you want to specify which main
method should be run when you type
sbt run
// set the main class for packaging the main jar mainClass in (Compile, packageBin) := Some("com.alvinalexander.testproject.Foo")
sbtmkdirs.sh
#!/bin/bash #------------------------------------------------------------------------------ # Name: sbtmkdirs # Version: 1.2 # Purpose: Create an SBT project directory structure with a few simple options. # Author: Alvin Alexander, http://alvinalexander.com # License: Creative Commons Attribution-ShareAlike 2.5 Generic # http://creativecommons.org/licenses/by-sa/2.5/ #------------------------------------------------------------------------------ declare -r TRUE=0 declare -r FALSE=1 # takes a string and returns true if it seems to represent "yes" function isYes() { local x=$1 [ $x = "y" ] && echo $TRUE; return [ $x = "Y" ] && echo $TRUE; return [ $x = "yes" ] && echo $TRUE; return echo $FALSE } echo "This script creates an SBT project directory beneath the current directory." while [ $TRUE ]; do echo "" read -p "Directory/Project Name (MyFirstProject): " directoryName directoryName=${directoryName:-MyFirstProject} read -p "Create .gitignore File? (Y/n): " createGitignore createGitignore=${createGitignore:-y} read -p "Create README.md File? (Y/n): " createReadme createReadme=${createReadme:-y} echo "" echo "-----------------------------------------------" echo "Directory/Project Name: $directoryName" echo "Create .gitignore File?: $createGitignore" echo "Create README.md File?: $createReadme" echo "-----------------------------------------------" read -p "Create Project? (Y/n): " createProject createProject=${createProject:-y} [ "$(isYes $createProject)" = "$TRUE" ] && break done mkdir -p ${directoryName}/src/{main,test}/{java,resources,scala,assembly} mkdir ${directoryName}/lib ${directoryName}/target #mkdir ${directoryName}/project # optional mkdir -p ${directoryName}/src/main/config #mkdir -p ${directoryName}/src/{main,test}/{filters,assembly} #mkdir -p ${directoryName}/src/site #--------------------------------- # create an initial build.sbt file #--------------------------------- echo "name := \"$directoryName\" organization := \"com.paytmlabs\" version := \"1.0\" scalaVersion := \"2.11.6\" val sparkVersion = \"1.2.0\" libraryDependencies <<= scalaVersion { scala_version => Seq( // Spark and Spark Streaming \"org.apache.spark\" %% \"spark-core\" % sparkVersion, \"org.apache.spark\" %% \"spark-mllib\" % sparkVersion, \"org.apache.spark\" %% \"spark-streaming\" % sparkVersion, \"org.apache.spark\" %% \"spark-streaming-kafka\" % sparkVersion, // Kafka \"org.apache.kafka\" %% \"kafka\" % \"0.8.1.1\", // for serialization of case class \"com.novus\" %% \"salat\" % \"1.9.8\", // Joda dates for Scala \"com.github.nscala-time\" %% \"nscala-time\" % \"1.2.0\" ) } resolvers += \"typesafe repo\" at \"http://repo.typesafe.com/typesafe/releases/\" scalacOptions += \"-deprecation\" " > ${directoryName}/build.sbt #------------------------------ # create .gitignore, if desired #------------------------------ if [ "$(isYes $createGitignore)" = "$TRUE" ]; then echo "bin/ project/ target/ .cache .classpath .project .settings" > ${directoryName}/.gitignore fi #----------------------------- # create README.me, if desired #----------------------------- if [ "$(isYes $createReadme)" = "$TRUE" ]; then touch ${directoryName}/README.md fi echo "" echo "Project is successfully created!"
Reference:
http://www.scala-sbt.org/0.13/tutorial/Library-Dependencies.html
http://alvinalexander.com/sbtmkdirs
No comments:
Post a Comment