Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When writing SBT tasks in build.sbt, how do I use my library dependencies?

Tags:

scala

sbt

How do I add an SBT task to build.sbt that uses an external dependency?

e.g. I would like to write a task that utilises the AWS SDK client

libraryDependencies += "aws-sdk-name" % "etc. "%etc"

uploadTask := {
   val s3Client = new AmazonS3Client(...);
   s3Client.putObject(...)
}

However, there will understandably be compile errors because the dependency won't will be generated by sbt!

The docs for tasks are restricted to very simple use cases i.e. println(...).

A plugin seems a bit overkill to me for this so I am hoping there is another way.

Thanks!

like image 890
user2232887 Avatar asked Sep 08 '15 11:09

user2232887


People also ask

Where are dependencies stored in sbt?

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 SBT project, and SBT will find them automatically.

What is sbt dependency management?

Like in almost every build system, SBT allows you to define library dependencies which are resolved automatically, so you don't have to download and package required libraries by yourself.

What is transitive dependency in sbt?

SBT Dependencies SBT is the most usual build tool for Scala projects. As a project gets more complex, it will increase the number of dependencies. And each dependency brings other dependencies, called transitive dependencies. Eventually, a project can suffer from the JAR dependency hell.


1 Answers

sbt is a recursive build system, so just place the library dependency you need in your build into your project folder:

your-project/
    project/
        build-dependencies.sbt
    src/
        main/ # etc.
    build.sbt

build-dependencies.sbt

libraryDependencies += "aws-sdk-name" % "etc. "%etc"

build.sbt

// Or in project/SomeBuildFile.scala
uploadTask := {
  val s3Client = new AmazonS3Client(...);
  s3Client.putObject(...)
}
like image 74
Sean Vieira Avatar answered Sep 30 '22 12:09

Sean Vieira