0

我正在使用 sbt、sbt-native-packager 和 sbt-proguard 为开发、登台和生产环境等 3 个环境制作包。我想做的是能够指定是否使用proguard来制作一个包。例如,以下命令使用 proguard 制作包:

$ sbt -Dforceobfuscate=true -Denv=dev rpm:pakageBin

但以下命令制作没有 proguard 的包

$ sbt -Denv=dev rpm:packageBin

我尝试了两种设置。一是系统属性条件覆盖任务,二是动态任务覆盖任务。

1.Just通过系统属性覆盖任务。

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 没有设置,但我不确定如何使用动态任务设置设置。

我想让上面的命令运行良好。任何解决方案都可以。

4

1 回答 1

1

我找到了解决方案。Seq[Setting[_]]解决方案是根据条件制作 proguard值。

lazy val customProguardSettings = 
  if (sys.props.get("forceobfuscate").isDefined) {
    proguardSettings ++ Seq( ... )
  } else {
    Nil
  }

customProguardSettings
于 2017-07-30T08:12:09.620 回答