【问题标题】:How does one combine ScalaFXML with Gradle?如何将 ScalaFXML 与 Gradle 结合起来?
【发布时间】:2018-12-08 01:21:13
【问题描述】:

让我先设定我的最终目标: 我想构建一个仅 scala 的 gui 应用程序,我可以使用 scene builder 进行设计,然后将 gluon 移植到我想要的任何平台。

我发现,需要在 Scala 中使用 JavaFXPorts 才能实现这一目标。我还发现我需要使用 gradle 才能成功应用胶子,或者看起来是这样(如果我错了,请纠正我,因为这就是头痛的开始)

简而言之,这意味着:ScalaFXML 和 Gradle 需要协同工作,但如何?

我发现了一些有趣的项目,但遗憾的是,这些项目都没有达到我的标准:

  • This 一个使用 JavaFX 而不是 ScalaFX
  • This 在项目中添加了 Java 而不是使用纯 scala

经过长时间的搜索,我找到了project,这几乎是正确的。可悲的是,这是在 sbt 中构建的,而不是在 gradle 中构建的。 尽管如此,我还是将该项目用作基础/示例,因为这是第一个以易于理解的方式使用sfxml

我有一个 gradle 脚本,几乎可以工作

buildscript {
    repositories {
        mavenLocal()
        jcenter()
    }

    dependencies {
        // during build, we depend on the jfxmobile-plugin
        classpath 'org.javafxports:jfxmobile-plugin:1.3.10'
    }
}

plugins {
    id 'java'
    id 'scala'
    id 'application'
}
apply plugin: 'org.javafxports.jfxmobile'

configurations {
    scalaCompiler
    scalaCompilerPlugin
}
configurations.scalaCompiler.transitive = false


compileScala.targetCompatibility = "1.8"


mainClassName = 'sfxml.Main'

repositories {
    mavenCentral()
    jcenter()
    maven {
        url 'http://nexus.gluonhq.com/nexus/content/repositories/releases'
    }
}

dependencies {
    compile 'org.scala-lang:scala-library:2.12.6'
    compile 'org.scala-lang:scala-compiler:2.12.6'

    compile 'com.gluonhq:particle:1.1.3'

    compile group: 'org.scalafx', name: 'scalafx_2.12', version: '8.0.144-R12'
    compile group: 'org.scalafx', name: 'scalafxml-core-sfx8_2.12', version: '0.4'
    compile group: 'org.scalamacros', name: 'paradise_2.12.6', version: '2.1.1'

    scalaCompiler "org.scalamacros:paradise_2.12.6:2.1.1"

    testCompile group: 'junit', name: 'junit', version: '4.12'
}

tasks.withType(ScalaCompile) {
    scalaCompileOptions.additionalParameters = [
            "-Xplugin:" + configurations.scalaCompilerPlugin.asPath,
            "-Ymacro-debug-lite"
    ]
    options.compilerArgs = ["-Xdebug", "-agentlib:jdwp=transport=dt_socket,server=n,suspend=y,address=9999"]
}

在最终找到所有正确的存储库(或者我认为)之后,在从 IDEA 导入过程中没有标记任何错误,但最后我收到以下错误:

Error:(10, 7) macro annotation could not be expanded (the most common reason for that is that you need to enable the macro paradise plugin; another possibility is that you try to use macro annotation in the same compilation run that defines it)
class AdoptionFormPresenter(private val sizeTextField: TextField,

但正如您可能已经知道的那样 - 如果您已经阅读了 gradle 脚本 - 我已经尝试过启用该插件。那么如何解决这个问题并实现开头所述的目标呢?

顺便说一句:我已经找到了以下解决方案,但那些也没有工作

  • This 使用 maven,但我想使用 gradle,所以... thx google?
  • This 很遗憾不起作用
  • This 可悲的是......也不起作用:/

脚注:所有 SDK 在本文发布时都是新下载的

【问题讨论】:

    标签: scala gradle scalafx


    【解决方案1】:

    所以在重新访问我的 gradle 脚本并检查前面提到的工作方案后,我发现了我的错误。

    我没有包含来自here 的完整答案。因此,在编辑我的 build.gradle 以仅包含必要的内容后,我得到了以下内容:

    plugins {
        id 'java'
        id 'scala'
    }
    apply plugin: 'org.javafxports.jfxmobile'
    
    configurations {
        scalaCompiler
    }
    configurations.scalaCompiler.transitive = false
    
    
    compileScala.targetCompatibility = "1.8"
    
    repositories {
        mavenCentral()
        jcenter()
    }
    
    dependencies {
        compile 'org.scala-lang:scala-library:2.12.6'
    
        compile group: 'org.scalafx', name: 'scalafx_2.12', version: '8.0.144-R12'
        compile group: 'org.scalafx', name: 'scalafxml-core-sfx8_2.12', version: '0.4'
    
        scalaCompiler "org.scalamacros:paradise_2.12.6:2.1.1"
    
        testCompile group: 'junit', name: 'junit', version: '4.12'
    }
    
    def String scalaCompilerOptions="-Xplugin:$configurations.scalaCompiler.singleFile.path"
    compileScala.scalaCompileOptions.additionalParameters = [scalaCompilerOptions]
    compileTestScala.scalaCompileOptions.additionalParameters = [scalaCompilerOptions]
    

    该脚本将使用 gradle 而不是 sbt 运行 this project

    旁注: 如果您收到错误“无法加载资源:AdoptionForm.fxml”,请阅读以下post

    我还没有完成我之前提出的目标,因为我现在还没有使用胶子。但是这篇文章的主要问题已经解决了,所以我将其标记为正确答案。

    如果我让它工作,我可能会在以后添加胶子集成。

    【讨论】:

      猜你喜欢
      • 2012-03-17
      • 2017-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-04
      • 2021-06-03
      • 2020-11-20
      相关资源
      最近更新 更多