Add specific directory and its content to Universa

2019-06-16 10:45发布

I am switching from maven to sbt for a Scala project I am working on. I used to work with the maven assembly plugin where you can map any directory in the workspace to a target directory in the assembly. I didn't find any equivalent in sbt-native-package, it worth provide this feature for the Universe kind.

I understood that everything that is present in the universal subdirectory is copied to the package as such, and it works like a charm, but I lack something like the following snippet.

mappings in Universal += {
  directory("my/local/dir") -> "static/dirInPackage"
}

I would like to know if there is already a way to do that, in such case, I would be happy to know how to do it, and I propose my help to commit documentation for that part if you want. If there is no way to do this kind of customization, I will be happy to propose a patch for that after having discussed specifications.

By the way, great job, your packager is working very well, thanks !

4条回答
劳资没心,怎么记你
2楼-- · 2019-06-16 11:00

From https://github.com/sbt/sbt-native-packager

If you'd like to add additional files to the installation dir, simply add them to the universal mappings:

import com.typesafe.sbt.SbtNativePackager.Universal

mappings in Universal += {
 file("my/local/conffile") -> "conf/my.conf"
}
查看更多
对你真心纯属浪费
3楼-- · 2019-06-16 11:02

You could use a simple map on top of the directory method result.

==> directory method documentation: MappingsHelper.directory

For example: // Packaging the content of /src/main/resources under conf add the following:

mappings in Universal ++= (directory("src/main/resources").map(t => (t._1, t._2.replace("resources", "conf"))))
查看更多
戒情不戒烟
4楼-- · 2019-06-16 11:06

This one seems to be the simplest example that worked for me

Takes all files in res/scripts/ and puts it in the bin/ directory when unzipped.

// In build.sbt
mappings in Universal <++= (packageBin in Compile) map { jar =>
  val scriptsDir = new java.io.File("res/scripts/")
  scriptsDir.listFiles.toSeq.map { f =>
    f -> ("bin/" + f.getName)
  }
}

If you choose a file that's not created, it will be created for you, for example assets/ will make a new assets folder with the files. If you want files inside of this one using this approach you'll have to make a new Seq at least that's what I did. Here's my example

assets/
├── scripts
│   └── install_dependencies.sh
└── urbangrizzly.database

and the appropriate build.sbt section:

mappings in Universal <++= (packageBin in Compile) map { jar =>
    val assetsDir = new java.io.File("assets/")
    val scriptsDir = new java.io.File("assets/scripts")
    assetsDir.listFiles.toSeq.map { files =>
        files -> ("assets/" + files.getName)
    } ++ scriptsDir.listFiles.toSeq.map { files =>
        files -> ("assets/scripts/" + files.getName)
    }
}

If you need more, just keep using the ++ operator to concatenate the lists

查看更多
我想做一个坏孩纸
5楼-- · 2019-06-16 11:15

After having discussed with the sbt-native-manager team and a first "rejected" pull request, here is the way to do this directory mapping in the build.sbt file (see pull request https://github.com/sbt/sbt-native-packager/pull/160 which provides mode detailed documentation) :

mappings in Universal <++= (packageBin in Compile, target ) map { (_, target) =>
    val dir = target / "scala-2.10" / "api"
    (dir.***) pair relativeTo(dir.getParentFile)
} 

To reduce verbosity of the above snippet, there is an issue (https://github.com/sbt/sbt-native-packager/issues/161) to propose a more human readable way to express this directory mapping:

mappings in Universal ++= allFilesRelativeTo(file(target / "scala-2.10" / "api"))
查看更多
登录 后发表回答