我正在使用sbt,sbt-native-packager和sbt-proguard为3个环境(如开发,登台和生产环境)创建软件包。我想做的是能够指定是否使用proguard来制作包。例如,以下命令使用proguard生成包:
$ sbt -Dforceobfuscate=true -Denv=dev rpm:pakageBin
但是以下命令使包没有proguard
$ sbt -Denv=dev rpm:packageBin
我尝试了两种设置。一个是按系统属性条件覆盖任务,两个是用动态任务覆盖任务。
1.只需按系统属性覆盖任务。
ProguardKeys.proguard in Proguard := {
if (sys.props.get("forceobfuscate").isDefined) {
( ProguardKeys.proguard in Proguard ).value
} else {
Seq(new File("dummy"))
}
}
此设置始终运行proguard任务(我不知道为什么)。 此外,我尝试了以下设置,以确保覆盖任务可以停止任务。
ProguardKeys.proguard in Proguard := {
if (sys.props.get("forceobfuscate").isDefined) {
// ( ProguardKeys.proguard in Proguard ).value
Seq(new File("dummy"))
} else {
Seq(new File("dummy"))
}
}
确实,上面的设置总是跳过proguard任务。
2.使用动态任务超越任务。
def obfuscateOrNot = Def.taskDyn {
if (sys.props.get("forceobfuscate").isDefined) {
Def.task {
( ProguardKeys.proguard in Proguard ).value
}
} else {
Def.task {
Seq(new File("dummy"))
}
}
}
ProguardKeys.proguard in Proguard := {
obfuscateOrNot.value
}
然后运行命令:
$ sbt rpm:packageBin
好的,这次跳过proguard任务。但是当我运行命令时:
$ sbt -Dforceobfuscate=true rpm:packageBin
我收到以下错误:
sbt.Init$RuntimeUndefined: References to undefined settings at runtime.
sbt.Init$RuntimeUndefined: References to undefined settings at runtime.
setting(ScopedKey(Scope(Select(ProjectRef(file:/work/backend/,backend)),Select(ConfigKey(proguard)),Global,Global),proguard)) at RangePosition(/work/backend/build.sbt,LineRange(244,247)) referenced from ScopedKey(Scope(Select(ProjectRef(file:/work/backend/,backend)),Select(ConfigKey(proguard)),Global,Global),proguard)
at sbt.Init$class.sbt$Init$$handleUndefined(Settings.scala:511)
at sbt.Init$$anon$4.apply(Settings.scala:516)
at sbt.Init$$anon$4.apply(Settings.scala:516)
at sbt.$tilde$greater$$anon$6.apply(TypeFunctions.scala:39)
at sbt.$tilde$greater$$anon$6.apply(TypeFunctions.scala:39)
at sbt.std.FullInstance$$anonfun$flatten$1$$anonfun$apply$3.apply(TaskMacro.scala:51)
at sbt.std.FullInstance$$anonfun$flatten$1$$anonfun$apply$3.apply(TaskMacro.scala:51)
at scala.Function1$$anonfun$compose$1.apply(Function1.scala:47)
at sbt.$tilde$greater$$anonfun$$u2219$1.apply(TypeFunctions.scala:40)
at sbt.std.Transform$$anon$4.work(System.scala:63)
at sbt.Execute$$anonfun$submit$1$$anonfun$apply$1.apply(Execute.scala:228)
at sbt.Execute$$anonfun$submit$1$$anonfun$apply$1.apply(Execute.scala:228)
at sbt.ErrorHandling$.wideConvert(ErrorHandling.scala:17)
at sbt.Execute.work(Execute.scala:237)
at sbt.Execute$$anonfun$submit$1.apply(Execute.scala:228)
at sbt.Execute$$anonfun$submit$1.apply(Execute.scala:228)
at sbt.ConcurrentRestrictions$$anon$4$$anonfun$1.apply(ConcurrentRestrictions.scala:159)
at sbt.CompletionService$$anon$2.call(CompletionService.scala:28)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
[error] (proguard:proguard) sbt.Init$RuntimeUndefined: References to undefined settings at runtime.
[error] setting(ScopedKey(Scope(Select(ProjectRef(file:/work/backend/,backend)),Select(ConfigKey(proguard)),Global,Global),proguard)) at RangePosition(/work/backend/build.sbt,LineRange(244,247)) referenced from ScopedKey(Scope(Select(ProjectRef(file:/work/backend/,backend)),Select(ConfigKey(proguard)),Global,Global),proguard)
[error] Total time: 1 s, completed 2017/07/29 12:09:24
我认为错误说没有设置proguard但我不知道如何使用动态任务设置设置。
我想让上面的命令运作良好。任何解决办法都没问题。
答案 0 :(得分:1)
我找到了解决方案。解决方案是根据条件制定proguard .Select
值。
.Select(o => new SelectListItem() { Text = o.GetDescription(), Value = o.ToString() }),