使用 sbt-assembly 重命名文件 [英] Rename file using sbt-assembly
问题描述
我有一个使用 ConfigFactory
来设置应用程序配置的 Scala 项目.对于构建,我使用 sbt
(与 sbt-assembly
一起使用).
I have a scala project that uses the ConfigFactory
to set up the application configurations. For building I use sbt
(together with sbt-assembly
).
根据我是使用 sbt-assembly
创建程序集还是只是运行项目,我想使用不同的配置文件(application.conf
在运行时项目,assembly.conf
运行项目的程序集时).
Depending on whether I create an assembly with sbt-assembly
or whether I just run the project, I would like to use different config files (application.conf
when running the project, assembly.conf
when running the assembly of the project).
我想到使用 assemblyMergeStrategy
来达到这个目的:在组装 jar 时,我会丢弃 application.conf
并重命名 assembly.conf
>.我的想法是这样的:
I thought of using the assemblyMergeStrategy
for this purpose: When assembling the jar, I would discard the application.conf
and rename assembly.conf
. My idea was something like:
assemblyMergeStrategy in assembly := {
case PathList("application.conf") => MergeStrategy.discard
case PathList("assembly.conf") => MergeStrategy.rename
...
}
通过这个我想实现的是,在组装jar时,文件assembly.conf
被重命名为application.conf
,因此被使用ConfigFactory
,而原来的 application.conf
被丢弃.
By this I would like to achieve is that when assembling the jar, the file assembly.conf
is renamed to application.conf
and is therefore used by ConfigFactory
, whereas the original application.conf
is discarded.
上面的代码显然不起作用,因为我无法指定assembly.conf
应该重命名为哪个文件名.我怎样才能做到这一点?
The code above obviously does not work, as I cannot specify to what filename assembly.conf
should be renamed to. How can I achieve this?
推荐答案
您需要定义自己的 MergeStrategy(在 project
目录中),它将文件重命名为 application.conf
然后在汇编中重新定义assemblyMergeStrategy
丢弃原来的application.conf
并将MyMergeStrategy
应用到assembly.conf
:
You need to define your own MergeStrategy(in project
directory) that will rename files to application.conf
and then redefine assemblyMergeStrategy in assembly
to discard original application.conf
and apply MyMergeStrategy
to assembly.conf
:
import java.io.File
import sbtassembly.MergeStrategy
class MyMergeStrategy extends MergeStrategy{
override def name: String = "Rename to application.conf"
override def apply(tempDir: File, path: String, files: Seq[File]): Either[String, Seq[(File, String)]] = {
Right(files.map(_ -> "application.conf"))
}
}
然后在build.sbt中使用:
And then use in build.sbt:
val root = (project in file(".")).settings(Seq(
assemblyMergeStrategy in assembly := {
case PathList("application.conf") => MergeStrategy.discard
case PathList("assembly.conf") => new MyMergeStrategy()
case x =>
val oldStrategy = (assemblyMergeStrategy in assembly).value
oldStrategy(x)
}
))
这仅适用于您的情况,但对于更复杂的情况,我会阅读他们在 sbt-native-packager 中的做法:https://www.scala-sbt.org/sbt-native-packager/recipes/package_configuration.html
This will do just for your case but for more complicated cases I would read how they do it in sbt-native-packager: https://www.scala-sbt.org/sbt-native-packager/recipes/package_configuration.html
这篇关于使用 sbt-assembly 重命名文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!