Scala and SBT Project

Scala and SBT Project

Get rid of build.sbt, place all the things into Build.scala and other scala files.

The best sample project is named as sillycat-graph. And I will also list my important configuration files.

build.properties
sbt.version=0.13.5

plugins.sbt - load the latest plugins
addSbtPlugin("io.spray" % "sbt-revolver" % "0.7.2")

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

addSbtPlugin("com.typesafe.sbt" % "sbt-scalariform" % "1.3.0")

addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "2.2.0")

resolvers += "Sonatype snapshots" at "https://oss.sonatype.org/content/repositories/snapshots/"

addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.7.0-SNAPSHOT")

Resolvers.scala - Place all the resolvers URLs here
import sbt._

object Resolvers {
  val local =    Resolver.defaultLocal
  val maven =    "MAVEN repo"         at "http://repo1.maven.org/maven2"
  val sonatype = "sonatype releases"  at "https://oss.sonatype.org/content/repositories/releases/"
  val sona_snap ="sonatype snapshots" at "https://oss.sonatype.org/content/repositories/snapshots/"
  val typsafe =  "typesafe repo"      at "http://repo.typesafe.com/typesafe/releases/"
  val spary =    "spray repo"         at "http://repo.spray.io/"
  val spary2 =   "Spray repo second"  at "http://repo.spray.cc/"
  val akka =     "Akka repo"          at "http://repo.akka.io/releases/"

  val myResolvers = Seq (local, maven, sonatype, sona_snap, typsafe, spary, spary2, akka)
}

Dependency.scala - all the jar dependencies
import sbt._

object Dependencies {
  val neo4j_version = "2.1.3"

  val neo4j   = "org.neo4j"   % "neo4j"   % neo4j_version
  val log4j   = "log4j"       % "log4j"   % "1.2.17"

  val baseDeps = Seq (
    neo4j,
    log4j
  )


Build.scala - The final import file to build all the things together
import sbt._
import sbt.Keys._
import Resolvers._
import Dependencies._
import sbtassembly.Plugin._
import sbtassembly.AssemblyUtils._
import AssemblyKeys._

object BuildSettings {
  val projectName = "sillycat-graph"
  val buildSettings = Seq(
    organization := "org.sillycat",
    version := "1.0.0",
    scalaVersion := "2.10.4",
    crossScalaVersions := Seq("2.10.2", "2.10.3", "2.10.4", "2.11.0", "2.11.1", "2.11.2"),
    scalacOptions ++= Seq()
  )
}

object ApplicationBuild extends Build {

  lazy val main = Project(
    BuildSettings.projectName,
    file("."),
    settings = BuildSettings.buildSettings ++ assemblySettings
    ++
    Seq(resolvers := myResolvers,
        libraryDependencies ++= baseDeps,
        mergeStrategy in assembly := mergeFirst
    )
  ) settings(
    mainClass in assembly := Some("com.sillycat.graph.app.ExecutorApp"),
    excludedJars in assembly <<= (fullClasspath in assembly) map { cp =>
      cp filter {_.data.getName == "compile-0.1.0.jar"}
    },
    artifact in (Compile, assembly) ~= { art =>
      art.copy(`classifier` = Some("assembly"))
    }
  )

  lazy val mergeFirst: String => MergeStrategy = {
    case PathList("javax", "servlet", xs @ _*) => MergeStrategy.first
    case PathList("org", "apache", "jasper", xs @ _*) => MergeStrategy.first
    case PathList("org", "fusesource", xs @ _*) => MergeStrategy.first
    case PathList("org", "apache", "commons", xs @ _*) => MergeStrategy.first
    case PathList("org", "apache", "commons", "beanutils", xs @ _*) => MergeStrategy.first
    case PathList("org", "apache", "commons", "collections", xs @ _*) => MergeStrategy.first
    case PathList("com", "esotericsoftware", "minlog", xs @ _*) => MergeStrategy.first
    case PathList("org", "eclipse", xs @ _*) => MergeStrategy.first
    case PathList("META-INF", xs @ _*) =>
      (xs map {_.toLowerCase}) match {
        case ("changes.txt" :: Nil ) =>
          MergeStrategy.discard
        case ("manifest.mf" :: Nil) | ("eclipsef.rsa" :: Nil) | ("index.list" :: Nil) | ("dependencies" :: Nil) =>
          MergeStrategy.discard
        case ps @ (x :: xs) if ps.last.endsWith(".sf") || ps.last.endsWith(".dsa") || ps.last.endsWith("pom.properties") || ps.last.endsWith("pom.xml") =>
          MergeStrategy.discard
        case "plexus" :: xs =>
          MergeStrategy.discard
        case "services" :: xs =>
          MergeStrategy.filterDistinctLines
        case ("spring.schemas" :: Nil) | ("spring.handlers" :: Nil) =>
          MergeStrategy.filterDistinctLines
        case ps @ (x :: xs) if ps.last.endsWith(".jnilib") || ps.last.endsWith(".dll") =>
          MergeStrategy.first
        case ps @ (x :: xs) if ps.last.endsWith(".txt") =>
          MergeStrategy.discard
        case ("notice" :: Nil) | ("license" :: Nil) | ("mailcap" :: Nil )=>
          MergeStrategy.discard
        case _ => MergeStrategy.deduplicate
      }
    case "application.conf" => MergeStrategy.concat
    case "about.html" => MergeStrategy.discard
    case "plugin.properties" => MergeStrategy.first
    case _ => MergeStrategy.first
  }

}

Tags: sbt build.sbt Build.scala basic project

References:
https://github.com/sbt/sbt-assembly
http://www.scala-sbt.org/0.13/docs/Full-Def-Example.html
https://github.com/scalamacros/sbt-example

http://stackoverflow.com/questions/18325181/suppress-main-class-in-sbt-assembly

猜你喜欢

转载自sillycat.iteye.com/blog/2108041
sbt