使用 SBT,您如何在运行时执行具有不同 Setting[T] 值的任务? [英] Using SBT, how do you execute a task with a different Setting[T] value at runtime?

查看:20
本文介绍了使用 SBT,您如何在运行时执行具有不同 Setting[T] 值的任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目构建定义中,Android 范围中的 SettingKey useProguard 设置为 true.这是我默认想要的.但是,当我执行一项特定任务时,我希望 useProguard 为 false.Android 范围内的所有内容都来自 sbt-android-plugin.

In my project build definition the SettingKey useProguard in the Android scope is set to true. This is what I want by default. When I execute one particular task, however, I want useProguard to be false. Everything in the Android scope comes from the sbt-android-plugin.

我不确定如何最好地解决这个问题.从我读到的内容来看,命令似乎可以完成工作,因为它可以执行与当前会话所看到的状态不同的任务.我试图像这样创建这样的命令:

I'm not sure how best to solve this problem. From what I read it seems like a command can get the job done, since it can execute a task with a different state than what your current session sees. I tried to create such a command like so:

def buildWithoutProguard = Command.command("build-without-proguard") { state =>
  val extracted = Project.extract(state)
  import extracted._

  val transformed = session.mergeSettings :+ (useProguard in Android := false)
  val newStructure = Load.reapply(transformed, structure)
  val newState = Project.setProject(session, newStructure, state)
  Project.evaluateTask(buildAndRun, newState)
  state
}

我正在将命令附加到我的项目设置并运行build-without-proguard"命令会根据需要执行 buildAndRun 任务.但是, useProguard 仍然是 true 而不是我所期望的 false.

I'm appending the command to my project settings and running the 'build-without-proguard' command executes the buildAndRun task as desired. However, useProguard is still true instead of false as I would expect.

首先,这整个方法对我来说感觉很沉重.假设更改 sbt-android-plugin 不是一个选项,那么我还能如何解决这个问题?

First, this whole approach feels heavy handed to me. Assuming changing sbt-android-plugin isn't an option here then how else would I solve this problem?

第二,为什么这种方法不能正常工作?

Second, why doesn't this approach work as is?

推荐答案

据我从您的问题中了解到,您希望 依赖项 的设置有所不同,具体取决于依赖项的内容.这是没有意义的 - 依赖关系要么得到满足,要么不满足,依赖于它的东西没有进入等式.

From what I understand from your question, you want the setting to be different for a dependency depending on what is depending on it. This doesn't make sense -- a dependency either is satisfied or it isn't, and what depends on it doesn't come into the equation.

您的解决方案对我来说似乎很满意.另一种方法是制作两个项目,指向同一个源,但具有不同的 proguard 设置和不同的目标,因此一个使用 proguard 构建,另一个不使用 proguard,两者都将保持它们的状态.然后你可以做任何你想做的事情,只是切换项目.

Your solution seems satisfactory to me. An alternative would be making two projects, pointing to the same source, but with different proguard settings and different target, so one would build with and the other without proguard, and both would keep their state. You'd then do whatever you want just switching the projects.

这篇关于使用 SBT,您如何在运行时执行具有不同 Setting[T] 值的任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆