Eran Medan
Eran Medan

Reputation: 45765

How to get the file location of a file in the classpath

I have the following scenario, I have some common text file (a config file template) that many projects need to include as part of their build

The easiest way is to put that file in a main/resources folder of some project and include that project via a dependency.

However I need that file not to be just in the classpath, it needs to be in a folder outside of the classpath, e.g. /conf

I know I can use the mapping option, and I know the right side of the mapping, but what is the left side?

e.g.

libraryDependencies += //some project that has /main/resouces/foo.conf in it   

mappings in Universal += classpathToFile("main/resources/foo.conf)  -> "conf/foo-external.conf"

What should I put instead of classpathToFile?


EDIT: I guess I can iterate over the entire classpath e.g.

mappings in Universal ++= {
  val cp: Seq[File] = (fullClasspath in Runtime).value.files
  cp.filter(_.name.endsWith(".conf")).map(f => f -> "bin/" + f.name)
}

But I'm not sure this is the best way...

Upvotes: 0

Views: 161

Answers (1)

gilad hoch
gilad hoch

Reputation: 2866

mappings is a sequence of Tuple2[File,String], where the file part indicates some file (any file), and the string part, is a path in the generated zip file, which the file will be packaged to.

for instance:

mappings in Universal += (file("build.sbt"),"foo/build.sbt")

this means the build.sbt file in your root project will be packaged into the generated zip file under a folder named foo. the file could be any file you want.

EDIT:

also, a better way to define what you did there would be:

mappings in Universal <++= (fullClasspath in Runtime) map {
  cp => {
    cp.files.filter(_.name.endsWith(".conf")).map(f => f -> "bin/" + f.name)
  }
}

Upvotes: 1

Related Questions