0

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...

Eran Medan
  • 44,555
  • 61
  • 184
  • 276
  • If the file's outside the current project where you do the mapping, it's likely a part of a jar, isn't it? You won't get a path then. – Jacek Laskowski May 17 '14 at 16:16
  • it's just a guess, but it seems like you're using the native packager, and you need a conf dir, which is exactly what play does. if you use play, then it might be better to put your code under `app` dir and config files under `conf` dir. play `dist` task will take care of the packaging for you... – gilad hoch May 20 '14 at 17:23
  • Thanks Gilad, but I'm not using play :) – Eran Medan May 20 '14 at 17:28

1 Answers1

1

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)
  }
}
Community
  • 1
  • 1
gilad hoch
  • 2,846
  • 2
  • 33
  • 57
  • Yes, I'm well aware of that :) question is how I get to the original file that I know is in my classpath. There got to be a nice way to that file. I guess I need to try getClass().getResource(...), just realized it... – Eran Medan May 20 '14 at 17:29
  • 1
    yes... have a look at similar case: http://stackoverflow.com/questions/20635556/classpath-resources-inside-one-jar-packaged-via-sbt/22940040#22940040 – gilad hoch May 20 '14 at 17:45
  • I did what you had in your edit, and it didn't work... since the files were packed as a JAR already. but your other answer in the link your provided seems promising, thanks! – Eran Medan May 20 '14 at 17:48
  • 1
    if your packaging this as a jar, you should use `mappings in packageBin` (or any other packager you might use like oneJar or assemply, ...) instead of `mappings in Universal` – gilad hoch May 20 '14 at 17:50