How does one combine ScalaFXML with Gradle?

时光怂恿深爱的人放手 提交于 2019-12-13 02:35:54

问题


Let me set up my end goal first: I want to build a scala only gui application, that I can design with scene builder and later port with gluon to any platform I want to.

I was able to find out, that one needs to use JavaFXPorts in Scala to make that happen. I also found out that I need to use gradle to apply gluon successfully, or so it seems (please correct me if I am wrong because that's where the headache starts)

So in short that means: ScalaFXML and Gradle need to work together, but how?

I've found some interesting projects, but sadly none of these hit my criteria just right:

  • This one used JavaFX instead of ScalaFX
  • This one added Java to the project instead of using pure scala

After a long time searching I found a project, that was almost right. Sadly, this was built in sbt and not in gradle. None the less I used that project as a base/example, as this was the first to use sfxml in way that was easily understood.

I have a gradle script, that almost works

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"]
}

After finally finding all the right repositories (or so I thought) no errors were marked down during the import process from IDEA, but at last I got the following error:

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,

But as you might have figured out - if you've read the gradle script - I already tried enabling the plugin. So how do I fix this and achieve the goal stated at the beginning?

Btw: I already found the following solutions, but those also didn't work

  • This uses maven, but I want to use gradle, soo... thx google?
  • This sadly doesn't work
  • This sadly... also does not work :/

Footnote: All SDKs are freshly downloaded at the time of this posting


回答1:


So after revisiting my gradle script and checking against the previously mentioned work arrounds, I found my mistake.

I didn't include the complete answer from here. So after editing my build.gradle to only include the necessary things, I got the following:

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]

That script will run this project with gradle instead of sbt.

Side note: If you get the error "Cannot load resource: AdoptionForm.fxml" read the following post

I didn't complete my previously stated goal just yet, since I haven't used gluon for now. But the main problem of this post is solved, so I will mark this as the right answer.

I might add the gluon integration later if I get it to work.



来源:https://stackoverflow.com/questions/51091699/how-does-one-combine-scalafxml-with-gradle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!