James Moore
James Moore

Reputation: 9026

How does an sbt plugin get a path to a file in the plugin?

I have an sbt (0.11.2) plugin that needs to get a path to text files inside the plugin. How do I do that? baseDirectory, sourceDirectories etc are set to the base of the project that's including the plugin, not the base of the plugin itself.

I'd like to provide a command to the plugin user that pulls defaults from a ruby file inside the plugin, and then allows the plugin user to override those defaults.

Upvotes: 5

Views: 503

Answers (1)

Heiko Seeberger
Heiko Seeberger

Reputation: 3722

Why don't you use good old Java's Class.getResource or Class.getResourceAsStream method? E.g. like this:

object TestPlugin extends Plugin {

  override def settings = super.settings ++ Seq(
    commands += testCommand
  )

  def testCommand = Command.command("test")(action)

  def action(state: State) = {
    try {
      val in = getClass.getResourceAsStream("/test.txt")
      val text = Source.fromInputStream(in).getLines mkString System.getProperty("line.separator")
      logger(state).info(text)
      in.close()
      state
    } catch {
      case e: Exception =>
        logger(state).error(e.getMessage)
        state.fail
    }
  }
}

Upvotes: 1

Related Questions