Se ha denunciado esta presentación.
Se está descargando tu SlideShare. ×

Sbt baby steps

Más Contenido Relacionado

Audiolibros relacionados

Gratis con una prueba de 30 días de Scribd

Ver todo

Sbt baby steps

  1. 1. SBT Baby steps January 2018
  2. 2. © 2018 The Quantium Group Pty Ltd. In Commercial Confidence > agenda 2 [info] build definition [info] key, task [info] resolvers [info] multi-project build [info] scope [info] scalacOptions [info] plugins we use [maybe[success]] Expected time: 45 min
  3. 3. –Wikipedia “sbt (Simple Build Tool) is an open-source build tool for Scala and Java projects…” © 2018 The Quantium Group Pty Ltd. In Commercial Confidence
  4. 4. 500 pages of docs © 2018 The Quantium Group Pty Ltd. In Commercial Confidence
  5. 5. > features of SBT [info] little or no configuration required for simple projects [info] scala-based build definition [info] incremental recompilation [info] packages and publishes jars, generates docs [info] supports testing with ScalaCheck, specs, ScalaTest [info] modularization [info] external project support (list a git repo as a dependency) [info] parallel task/test execution [info] library management support [info] more [success] Total time: 0 s, completed 27/01/2018 6:35:31 PM © 2018 The Quantium Group Pty Ltd. In Commercial Confidence
  6. 6. at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745) [error] (subs/*:projectDescriptors) Could not create file /home/svc_jenkins/jenkins_workspace/Substitutability/subs-build- pr/subs/target/streams/$global/projectDescriptors/$global/streams/outjava.io.IOExcepti on: No such file or directory [error] Total time: 0 s, completed Jan 15, 2018 2:39:24 AM Build step 'Build using sbt' changed build result to FAILURE Build step 'Build using sbt' marked build as failure [CHECKSTYLE] Skipping publisher since build result is FAILURE [WARNINGS] Skipping publisher since build result is FAILURE Publishing Scoverage XML and HTML report ... ERROR: Step ?Publish Scoverage Report? aborted due to exception: java.io.IOException: /home/svc_jenkins/jenkins_workspace/Substitutability/subs-build- pr/target/scala-2.11/scoverage-report not exists at org.jenkinsci.plugins.scoverage.ScoveragePublisher.copyReport(ScoveragePublisher.java: 114) at org.jenkinsci.plugins.scoverage.ScoveragePublisher.perform(ScoveragePublisher.java:66) …… Notified Stash for commit with id f83c0fecc73c477c18843208317f9523a9b17882 Finished: FAILURE
  7. 7. > the basics [info] build defined in objects extending Build [info] builds are composed from Projects. May be local (file) or remote (git url) [info] projects are a sequence of Settings [info] settings are key-initialization pairs © 2018 The Quantium Group Pty Ltd. In Commercial Confidence
  8. 8. © 2018 The Quantium Group Pty Ltd. In Commercial Confidence
  9. 9. sbt.version = 0.13.16 build.properties © 2018 The Quantium Group Pty Ltd. In Commercial Confidence
  10. 10. import sbt._ import sbt.Keys._ object ExampleBuild extends Build { val dependencies = Seq( "org.scalatest" %% "scalatest" % "1.9.1" % "test" ) lazy val exampleProject = Project("example", file(".")) settings( version := "0.1", organization := "com.companyname", scalaVersion := "2.12.4", libraryDependencies ++= dependencies ) } Extending the SBT Build object © 2018 The Quantium Group Pty Ltd. In Commercial Confidence
  11. 11. name := "example" version := "0.1" organization := "com.companyname" scalaVersion := "2.12.4" libraryDependencies += "org.scalatest" %% "scalatest" % "1.9.1" % "test" build.sbt file © 2018 The Quantium Group Pty Ltd. In Commercial Confidence
  12. 12. name := "example" version := "0.1" organization := "com.companyname" scalaVersion := "2.12.4" libraryDependencies += "org.scalatest" %% "scalatest" % "1.9.1" % "test" build.sbt file { } ——— —-———— key operator body © 2018 The Quantium Group Pty Ltd. In Commercial Confidence
  13. 13. build.sbt file © 2018 The Quantium Group Pty Ltd. In Commercial Confidence := replace existing += add one element ++= add Seq[T](…)
  14. 14. > keys [info] SettingKey[T] For static values name, version, organization [info] TaskKey[T] For (re-)computed values sources, compile [info] InputKey[T] For generating a Task from user input run-main my.main.class © 2018 The Quantium Group Pty Ltd. In Commercial Confidence
  15. 15. Custom Key lazy val hello = taskKey[Unit]("An example task") lazy val root = (project in file(".")) .settings( hello := { println("Hello!") } ) © 2018 The Quantium Group Pty Ltd. In Commercial Confidence
  16. 16. Definition of Task lazy val hello = taskKey[Unit]("An example task") lazy val root = (project in file(".")) .settings( hello := { println("Hello!") } ) © 2018 The Quantium Group Pty Ltd. In Commercial Confidence
  17. 17. > reload [info] Loading project definition from /Users/marina/IdeaProjects/example/project [info] Set current project to example (in build file:/Users/marina/IdeaProjects/example/) > hello Hello! [success] Total time: 0 s, completed 21/01/2018 9:40:41 PM > © 2018 The Quantium Group Pty Ltd. In Commercial Confidence
  18. 18. Int Task lazy val hello = taskKey[Int]("An example task") lazy val root = (project in file(".")) .settings( hello := { println(“Hello!") 2 } ) © 2018 The Quantium Group Pty Ltd. In Commercial Confidence
  19. 19. > show hello Hello! [info] 2 [success] Total time: 0 s, completed 21/01/2018 9:40:41 PM > inspect hello [info] Task: Int [info] Description: [info] An example task [info] Provided by: [info] {file:/Users/marina/IdeaProjects/example/}root/*:hello [info] Defined at: [info] /Users/marina/IdeaProjects/example/build.sbt:19 [info] Delegates: [info] *:hello [info] {.}/*:hello [info] */*:hello > > show, inspect © 2018 The Quantium Group Pty Ltd. In Commercial Confidence
  20. 20. > show package [info] Packaging /Users/marina/IdeaProjects/example/target/scala- 2.12/example_2.12-0.1.jar ... [info] Done packaging. [info] /Users/marina/IdeaProjects/example/target/scala- 2.12/example_2.12-0.1.jar [success] Total time: 0 s, completed 20/01/2018 10:04:01 PM > > show package © 2018 The Quantium Group Pty Ltd. In Commercial Confidence
  21. 21. import sbt._ object Dependencies { object Versions { val scala = "2.12.4" val scalatest = "3.0.1" val shapeless = "2.3.2" } val testLibs = Seq( "org.scalatest" %% "scalatest" % Versions.scalatest ).map(_ % "test") def mainLibs = Seq( "com.chuusai" %% "shapeless" % Versions.shapeless, "org.scala-lang" % "scala-reflect" % Versions.scala ) } Dependencies.scala © 2018 The Quantium Group Pty Ltd. In Commercial Confidence
  22. 22. import Dependencies._ name := "example" version := "0.1" scalaVersion := "2.12.4" organization := "com.companyname" libraryDependencies ++= mainLibs ++ testLibs build.sbt libraryDependencies ++= Seq( groupID % artifactID % revision, groupID % otherID % otherRevision ) < artifactID < revision < groupID © 2018 The Quantium Group Pty Ltd. In Commercial Confidence
  23. 23. import Dependencies._ name := "example" version := "0.1" scalaVersion := "2.12.4" organization := “com.companyname" resolvers += "Java.net Maven2 Repository" at "http://download.java.net/maven/2/" libraryDependencies ++= mainLibs ++ testLibs build.sbt https://www.scala-sbt.org/0.13/docs/Resolvers.html © 2018 The Quantium Group Pty Ltd. In Commercial Confidence
  24. 24. Proxy Repositories Nexus Artifactory Archiva OR © 2018 The Quantium Group Pty Ltd. In Commercial Confidence
  25. 25. -Dsbt.repository.config=<path-to-your-repo-file> OR Proxy Repositories © 2018 The Quantium Group Pty Ltd. In Commercial Confidence Nexus Artifactory Archiva
  26. 26. –Wikipedia Apache Ivy is a transitive package manager. Build tools and continuous integration servers regularly support or include Ivy: • sbt (included in dependency management) • Jenkins •… © 2018 The Quantium Group Pty Ltd. In Commercial Confidence
  27. 27. Ivy Maven sbt © 2018 The Quantium Group Pty Ltd. In Commercial Confidence
  28. 28. MacBook-Pro:IdeaProjects marina$ cd example MacBook-Pro:example marina$ cd ~/.ivy2 MacBook-Pro:.ivy2 marina$ ls cache exclude_classifiers exclude_classifiers.lock MacBook-Pro:.ivy2 marina$ cd cache/ MacBook-Pro:cache marina$ ls args4j com.chuusai com.eed3si9n com.github.ben-manes.caffeine com.github.cb372 com.google com.google.code.findbugs com.google.code.gson com.google.guava com.google.protobuf com.googlecode.json-simple com.jcraft com.lihaoyi com.lmax com.squareup.okhttp3 com.squareup.okio com.thoughtworks.paranamer com.trueaccord.lenses com.trueaccord.scalapb com.typesafe © 2018 The Quantium Group Pty Ltd. In Commercial Confidence
  29. 29. val setts = Seq( version := "0.1", scalaVersion := "2.12.4", organization := "com.companyname" ) libraryDependencies ++= mainLibs ++ testLibs lazy val root = Project( id = "example", base = file("."), dependencies = Seq( domain, magnolia ) ) .settings(setts) .settings(libraryDependencies ++= mainLibs ++ testLibs) lazy val magnolia = ProjectRef( uri("git://github.com/etaty/scalacheck-magnolia.git"), "scalacheck-magnolia" ) lazy val domain = Project( id = "example-domain", base = file("example-domain")) .settings(setts) .settings(moduleName := "domain") build.sbt © 2018 The Quantium Group Pty Ltd. In Commercial Confidence
  30. 30. © 2018 The Quantium Group Pty Ltd. In Commercial Confidence Build root domain magnolia
  31. 31. Cloning into ‘/Users/marina/.sbt/0.13/staging/8924b22f06679a54873e/scalacheck-magnolia'... [info] Loading project definition from /Users/marina/IdeaProjects/example/project [info] Loading project definition from /Users/marina/.sbt/0.13/staging/8924b22f06679a54873e/scalacheck-magnolia/project [info] Set current project to example (in build file:/Users/marina/IdeaProjects/example/) [info] Updating {git://github.com/etaty/scalacheck-magnolia.git}scalacheck-magnolia... [info] downloading https://repo1.maven.org/maven2/com/propensive/magnolia_2.12/0.6.1/magnolia_2.12-0.6.1.jar ... [info] [SUCCESSFUL ] com.propensive#magnolia_2.12;0.6.1!magnolia_2.12.jar (2011ms) [info] downloading https://repo1.maven.org/maven2/org/scalacheck/scalacheck_2.12/1.13.4/scalacheck_2.12- 1.13.4.jar ... [info] [SUCCESSFUL ] org.scalacheck#scalacheck_2.12;1.13.4!scalacheck_2.12.jar (4767ms) [info] Done updating. [info] Updating {file:/Users/marina/IdeaProjects/example/}example... [info] Done updating. [info] Compiling 2 Scala sources to /Users/marina/.sbt/0.13/staging/8924b22f06679a54873e/scalacheck-magnolia/target/scala- 2.12/classes... [info] 'compiler-interface' not yet compiled for Scala 2.12.4. Compiling... [info] Compilation completed in 12.795 s [success] Total time: 28 s, completed 19/01/2018 11:35:13 AM > compile © 2018 The Quantium Group Pty Ltd. In Commercial Confidence
  32. 32. Cloning into ‘/Users/marina/.sbt/0.13/staging/8924b22f06679a54873e/scalacheck-magnolia'... [info] Loading project definition from /Users/marina/IdeaProjects/example/project [info] Loading project definition from /Users/marina/.sbt/0.13/staging/8924b22f06679a54873e/scalacheck-magnolia/project [info] Set current project to example (in build file:/Users/marina/IdeaProjects/example/) [info] Updating {git://github.com/etaty/scalacheck-magnolia.git}scalacheck-magnolia... [info] downloading https://repo1.maven.org/maven2/com/propensive/magnolia_2.12/0.6.1/magnolia_2.12-0.6.1.jar ... [info] [SUCCESSFUL ] com.propensive#magnolia_2.12;0.6.1!magnolia_2.12.jar (2011ms) [info] downloading https://repo1.maven.org/maven2/org/scalacheck/scalacheck_2.12/1.13.4/scalacheck_2.12- 1.13.4.jar ... [info] [SUCCESSFUL ] org.scalacheck#scalacheck_2.12;1.13.4!scalacheck_2.12.jar (4767ms) [info] Done updating. [info] Updating {file:/Users/marina/IdeaProjects/example/}example... [info] Done updating. [info] Compiling 2 Scala sources to /Users/marina/.sbt/0.13/staging/8924b22f06679a54873e/scalacheck-magnolia/target/scala- 2.12/classes... [info] 'compiler-interface' not yet compiled for Scala 2.12.4. Compiling... [info] Compilation completed in 12.795 s [success] Total time: 28 s, completed 19/01/2018 11:35:13 AM © 2018 The Quantium Group Pty Ltd. In Commercial Confidence > compile
  33. 33. Cloning into ‘/Users/marina/.sbt/0.13/staging/8924b22f06679a54873e/scalacheck-magnolia'... [info] Loading project definition from /Users/marina/IdeaProjects/example/project [info] Loading project definition from /Users/marina/.sbt/0.13/staging/8924b22f06679a54873e/scalacheck-magnolia/project [info] Set current project to example (in build file:/Users/marina/IdeaProjects/example/) [info] Updating {git://github.com/etaty/scalacheck-magnolia.git}scalacheck-magnolia... [info] downloading https://repo1.maven.org/maven2/com/propensive/magnolia_2.12/0.6.1/magnolia_2.12-0.6.1.jar ... [info] [SUCCESSFUL ] com.propensive#magnolia_2.12;0.6.1!magnolia_2.12.jar (2011ms) [info] downloading https://repo1.maven.org/maven2/org/scalacheck/scalacheck_2.12/1.13.4/scalacheck_2.12- 1.13.4.jar ... [info] [SUCCESSFUL ] org.scalacheck#scalacheck_2.12;1.13.4!scalacheck_2.12.jar (4767ms) [info] Done updating. [info] Updating {file:/Users/marina/IdeaProjects/example/}example... [info] Done updating. [info] Compiling 2 Scala sources to /Users/marina/.sbt/0.13/staging/8924b22f06679a54873e/scalacheck-magnolia/target/scala- 2.12/classes... [info] 'compiler-interface' not yet compiled for Scala 2.12.4. Compiling... [info] Compilation completed in 12.795 s [success] Total time: 28 s, completed 19/01/2018 11:35:13 AM © 2018 The Quantium Group Pty Ltd. In Commercial Confidence > compile
  34. 34. © 2018 The Quantium Group Pty Ltd. In Commercial Confidence > Project
  35. 35. > scope 1. Keys can have different values in different scopes • sources in Compile • sources in Test 2. Keys can be scoped by tasks or configuration 3. Configurations can extend from each other • val Test = config("test") extend Compile
  36. 36. The subproject axis The configuration axis The task axis > scope
  37. 37. > scope Compile, Test task keys compile, package, run Project(id = "example-domain")
  38. 38. > referring to scopes in a build definition lazy val root = (project in file(".")) .settings( name := "hello" ) name in Compile := “hello" name in packageBin := “hello" name in (Compile, packageBin) := “hello"
  39. 39. > referring to scopes in a build definition lazy val root = (project in file(".")) .settings( name := "hello" ) name in Compile := "hello" name in packageBin := "hello" name in (Compile, packageBin) := “hello"
  40. 40. > referring to scopes in a build definition lazy val root = (project in file(".")) .settings( name := "hello" ) name in Compile := "hello" name in packageBin := "hello" name in (Compile, packageBin) := "hello"
  41. 41. > scalacOptions scalacOptions ++= Seq( "-encoding", "UTF-8", "-unchecked", "-deprecation", "-Ywarn-dead-code" )
  42. 42. > scalacOptions scalacOptions ++= Seq( "-encoding", "UTF-8", "-unchecked", "-deprecation", "-Ywarn-dead-code" )
  43. 43. > scalacOptions "-unchecked" "-deprecation", "-Ywarn-dead-code" ) def printListType(lst: List[AnyVal]) = lst match { case strings: List[String] => println(s"List of Strings $strList") case integers: List[Int] => println(s"List of Strings $intList") }
  44. 44. > scalacOptions scalacOptions ++= Seq( "-encoding", "UTF-8", "-unchecked", "-deprecation", "-Ywarn-dead-code" )
  45. 45. > scalacOptions "-deprecation" "-Ywarn-dead-code" ) NO OPTION [warn] there were three deprecation warnings; re-run with -deprecation for details WITH OPTION [warn] Foo.scala:451: method listAll in class Bar is deprecated: Don't use this method. Its super broken. [warn] Bar.listAll(user)
  46. 46. > scalacOptions scalacOptions ++= Seq( "-encoding", "UTF-8", "-unchecked", "-deprecation", "-Ywarn-dead-code" )
  47. 47. > scalacOptions "-Ywarn-dead-code" scala> def bar (a: Int): Int = { | return 0 | a + 10 | } :12: warning: dead code following this construct return 0 ^ bar: (a: Int)Int
  48. 48. > plugins.sbt addSbtPlugin("com.eed3si9n" % "sbt-man" % "0.1.0" ) // look up scaladoc addSbtPlugin("com.eed3si9n" % "sbt-dirty-money" % "0.1.0") // clean Ivy2's cache addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.6") // create a fat JAR of your project // with all of its dependencies © 2018 The Quantium Group Pty Ltd. In Commercial Confidence
  49. 49. > assembly • Merge Strategy • Shading • Excluding JARs and files © 2018 The Quantium Group Pty Ltd. In Commercial Confidence
  50. 50. > assembly •Merge Strategy Shading Excluding JARs and files © 2018 The Quantium Group Pty Ltd. In Commercial Confidence MergeStrategy.deduplicate verify all files have the same contents & error out otherwise MergeStrategy.first picks the first of the matching files in classpath order MergeStrategy.last picks the last one MergeStrategy.singleOrError bails out with an error message on conflict MergeStrategy.concat concatenates all matching files and includes the result MergeStrategy.filterDistinctLines also concatenates, but leaves out duplicates along the way MergeStrategy.rename renames the files originating from jar files MergeStrategy.discard discards matching files
  51. 51. •Merge Strategy Shading Excluding JARs and files > assembly © 2018 The Quantium Group Pty Ltd. In Commercial Confidence lazy val root = Project( id = "example", base = file("."), dependencies = Seq( domain, magnolia ) ) .settings(setts) .settings(libraryDependencies ++= mainLibs ++ testLibs) .settings(assemblyJarName in assembly := "example.jar") .settings( assemblyMergeStrategy in assembly := { case "application.conf" => MergeStrategy.concat case x => val oldStrategy = (assemblyMergeStrategy in assembly).value oldStrategy(x) } )
  52. 52. •Merge Strategy Shading Excluding JARs and files > assembly © 2018 The Quantium Group Pty Ltd. In Commercial Confidence > assembly [info] Including: scala-library-2.12.4.jar [info] Including: scala-compiler-2.12.4.jar … [info] Merging files... [warn] Merging 'META-INF/MANIFEST.MF' with strategy 'discard' [warn] Merging 'META-INF/maven/jline/jline/pom.properties' with strategy 'discard' [warn] Merging 'META-INF/maven/jline/jline/pom.xml' with strategy 'discard' [warn] Merging 'application.conf' with strategy 'concat' [warn] Merging 'rootdoc.txt' with strategy 'concat' [warn] Strategy 'concat' was applied to 2 files [warn] Strategy 'discard' was applied to 3 files [info] SHA-1: 642c5daa2bfc30a825f08fa72a51a990cac441fc [info] Packaging /Users/marina/IdeaProjects/example/target/scala-2.12/example. [info] Done packaging.
  53. 53. Merge Strategy •Shading Excluding JARs and files > assembly ShadeRule.rename("x.**" -> "y.@1", …) ShadeRule.zap("a.b.c") any matched class to be removed from the resulting jar file ShadeRule.keep("x.**") rule marks all matched classes as "roots" © 2018 The Quantium Group Pty Ltd. In Commercial Confidence
  54. 54. > stackoverflow.com
  55. 55. Merge Strategy •Shading Excluding JARs and files > assembly .settings( assemblyShadeRules in assembly := Seq( ShadeRule.rename("com.google.**" -> "org.moogle.@1").inProject ) ) © 2018 The Quantium Group Pty Ltd. In Commercial Confidence
  56. 56. •Merge Strategy •Shading •Excluding JARs and files > assembly libraryDependencies ++= Seq( ("org.apache.spark" %% "spark-core" % "0.8.0-incubating") .exclude("commons-logging", "commons-logging") ) assemblyMergeStrategy in assembly := { case PathList("about.html") => MergeStrategy.rename case x => val oldStrategy = (assemblyMergeStrategy in assembly).value oldStrategy(x) } © 2018 The Quantium Group Pty Ltd. In Commercial Confidence
  57. 57. > summary [info] Build -> Project -> Settings [info] s & tasks [info] Proxy Resolvers [info] Multi-project build [info] Scope [info] scalacOptions [info] Plugins we use [success] Total time: 45 min © 2018 The Quantium Group Pty Ltd. In Commercial Confidence
  58. 58. Thank you!

×