如何在SBT中检测JavaFX运行时jar [英] How to detect JavaFX runtime jar in sbt

查看:57
本文介绍了如何在SBT中检测JavaFX运行时jar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的是在开头定义javaHome,默认是从环境变量或从固定字符串开始.然后,稍后,我将使用该字符串.

What I'd like to do is define javaHome in the beginning, either from an environment variable or from a fixed string as a default. Then, later, I would use that string.

这是我尝试过的:

javaHome := Some(file("/Library/Java/JavaVirtualMachines/jdk1.7.0_10.jdk/Contents/Home"))

及以后

unmanagedJars in Compile <+= (javaHome) { fn => Attributed.blank( fn + "/jre/lib/jfxrt.jar") }

我尝试阅读[reference] [1],但它确实比Scala本身复杂.请帮忙.

I have tried to read the [reference][1] but it's really more complex than Scala itself. Please help.

我可以通过在构建文件中间使用固定字符串来绕过此操作,但通常我不喜欢这样.

I can bypass this by having a fixed string in the midst of the build file, but generally I don't like such.

平台是OS X,sbt 0.12.1,JDK 7u10,Scala 2.9.2

Platform is OS X, sbt 0.12.1, JDK 7u10, Scala 2.9.2

自从找到解决方案以来,我已经删除了错误消息,并进行了两次(不成功的)尝试,因为它们只会造成混淆.

Since I found a solution, I have removed error messages and two further (unsuccessful) tries since they would only confuse.

推荐答案

终于完成了.看来问题部分是在理解sbt值模型(及其对自定义运算符的过度使用!),部分是在与sbt中使用的类型达成共识.

Got this finally done. It seems the problem was partly in understanding the sbt value model (and its pure overuse of custom operators!), partly in getting to terms with the types used in sbt.

我将相应地编辑问题,但这是我的发现(和解决方案).

I will edit the question accordingly, but here are my findings (and solution).

  1. sbt值是预先考虑的.它们不是我无法设置myOwn的方式的变量. javaHome是sbt的已知值(或实际上是一项任务?).

  1. sbt values are pre-meditated. They are not variables in the way that I cannot set myOwn. The javaHome is a known value (or actually, a task?) of sbt.

话虽如此,我想知道为什么sbt不能在为我找到合适的JDK方面做得很好.哦,很好.

Having said that, I wonder why sbt cannot do a good job in finding a suitable JDK for me. Oh, well.

提示:val xxx: Nothing = expression的使用是一种很棒的工具,用于查找各种事物的类型.一个人不能拥有类型为Nothing的实例,因此它总是会失败,并带有一条很好的错误消息,通知expression类型(未知).

Tip: Use of val xxx: Nothing = expression is a great tool in finding the types of various things. One cannot have an instance of type Nothing so that always fails, with a nice error message informing the (unknown) type of the expression.

这是我的javaHome检测代码(来自build.sbt):

Here comes my javaHome detection code (from build.sbt):

//---
// Note: Wouldn't 'sbt' be able to provide us a nice default for this (the following logic
//      would deserve to be automatic, not in a project build script). AK 4-Jan-2013
//
javaHome := {
  var s = System.getenv("JAVA_HOME")
  if (s==null) {
    // tbd. try to detect JDK location on multiple platforms
    //
    // OS X: "/Library/Java/JavaVirtualMachines/jdk1.xxx.jdk/Contents/Home" with greatest id (i.e. "7.0_10")
    //
    s= "/Library/Java/JavaVirtualMachines/jdk1.7.0_10.jdk/Contents/Home"
  }
  //
  val dir = new File(s)
  if (!dir.exists) {
    throw new RuntimeException( "No JDK found - try setting 'JAVA_HOME'." )
  }
  //
  Some(dir)  // 'sbt' 'javaHome' value is ': Option[java.io.File]'
}

这是使用它的代码段(在同一文件中),确保我们可以访问jfxrt.jar(JavaFX 2.x运行时). Oracle(或sbt)无论如何都应将其放在类路径中,因此可以省去所有这些.

And here is the snippet (later in the same file) that uses it, making sure we have access to jfxrt.jar (the JavaFX 2.x runtime). Oracle (or sbt) should make this be on the classpath anyways, so all of this could be spared.

//---
// JavaFX
//
// Note: We shouldn't even need to say this at all. Part of Java 7 RT (since 7u06) and should come from there (right)
//      The downside is that now this also gets into the 'one-jar' .jar package (where it would not need to be,
//      and takes 15MB of space - of the 17MB package!) AKa 1-Nov-2012
//
unmanagedJars in Compile <+= javaHome map { jh /*: Option[File]*/ =>
  val dir: File = jh.getOrElse(null)    // unSome
  //
  val jfxJar = new File(dir, "/jre/lib/jfxrt.jar")
  if (!jfxJar.exists) {
    throw new RuntimeException( "JavaFX not detected (needs Java runtime 7u06 or later): "+ jfxJar.getPath )  // '.getPath' = full filename
  }
  Attributed.blank(jfxJar)
} 

请注意,sbt不允许条目内有空行.为了避免这种情况,我在需要一些空格的地方使用了缩进的//行.

Note that sbt does not allow empty lines within an entry. To avoid this I've used indented // lines where I wanted some white space.

这篇关于如何在SBT中检测JavaFX运行时jar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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