sbt Library Dependencies et al

  • 3rd party library dependency (commonly resolved using maven or ivy)
  • hard wired jar filepath (less commonly used and is found in situations where the dependency is not managed by a maven like repository).
  • sub-project based dependency using dependsOn

Managed Dependencies

libraryDependencies += organization %% moduleName % version

Provided

libraryDependencies += "org.scalatest" %% "scalatest" % "3.1.0"     % "test" % Provided

exclude()

libraryDependencies += "log4j" % "log4j" % "1.2.15" exclude("javax.jms", "jms")

dependencyOverride

dependencyOverrides += "com.fasterxml.jackson.core" % "jackson-core" % "3.7.0-M11"

Unmanaged Dependencies

unmanagedBase := baseDirectory.value / "custom_lib"

Docker package

addSbtPlugin("se.marcuslonnberg" % "sbt-docker" % "1.8.2")
docker / dockerfile := {
val jarFile: File = (Compile / packageBin / sbt.Keys.`package`).value
val dependencies = (Compile / dependencyClasspathAsJars).value
val jarTarget = s"/app/${jarFile.getName}"
def addLibPath(files: Seq[File]) = files.map("/app/" + _.getName)
val classpathString = (Seq(jarTarget, "/app/config") ++ addLibPath(dependencies.files)).mkString(":")

val banner =
"""echo "Using JAVA_OPTS: ${JAVA_OPTS}"
|echo "Using args $@"
|""".stripMargin
val javaEnv = "java -Dfile.encoding=UTF-8 $JAVA_OPTS"
val args = """"$@""""

val content =
s"""#!/bin/bash
|$banner
|echo '${name.value} - v${version.value}'
|echo 'Built on ${LocalDateTime.now()} - ${git.gitHeadCommit.value.getOrElse("No head commit")}'
|$javaEnv -cp $classpathString $mainclass $args
|""".stripMargin

val programName = "my-app"

val startScript = file(s"""/tmp/$programName""")
IO.write(startScript, content, append = false)

new sbtdocker.Dockerfile {
from("amazoncorretto:11")
add(dependencies.files, "/app/")
add(startScript, s"""/$programName""")
run("chmod", "+x", s"""/$programName""")
add(jarFile, jarTarget)
run("mkdir", "-p", "/app/config")
run("touch", "/app/config/application.conf")
env("JAVA_OPTS", "-Xms8g")
env("JAVA_HOME", "/usr/lib/jvm/jre")
entryPoint(s"""/$programName""")
}
}
sbt 'project sub-project-1' docker dockerPush

Publishing to Maven

ThisBuild / resolvers += "Artifactory-release" at "https://test1.jfrog.io/artifactory/libs-release/"
ThisBuild / resolvers += "Artifactory-snapshot" at "https://test1.jfrog.io/artifactory/libs-snapshot/"
ThisBuild / credentials += Credentials(Path.userHome / ".sbt" / "credentials")
sbt 'project sub-project-1' publishLocal publish

Tip

resolvers += Resolver.file("local-ivy", file(Path.userHome.absolutePath + "/.ivy2/local"))(Resolver.ivyStylePatterns)

--

--

--

Listener and reader

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Our Agile Project Process — hmm? yet another & my problem

Clarksons’s Farm on Amazon Prime

Dynamic Pluggable Microservice Framework Lite

4.5 C++ Language Quick Start

Logical vs. Syntactical Errors in Code

Deploying Deep-Learning Models to Kubernetes on the IBM Cloud

Flutter App Development- An Introduction

Optimize a Jenkins Pipeline with AWS Codebuild

Accident Lawyer Sandy Utah

accident lawyer sandy utah

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Programmer

Programmer

Listener and reader

More from Medium

Testing AMQP Consumer reconnect with testcontainers

ANTLR + Scala: An introduction to Metaprogramming

Currying, the constructor of functions

Technology Adoption at Castlight