user2232887
user2232887

Reputation: 309

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

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!

Upvotes: 8

Views: 1042

Answers (1)

Sean Vieira
Sean Vieira

Reputation: 159855

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(...)
}

Upvotes: 7

Related Questions