If you need to use GroovyFX with theJavaFX v2.1 developer preview then you will have to use a snapshot of GroovyFX v0.2. I'll show an example of that later in this post.
Grabbing Grapes
Having GroovyFX in Maven Central (thanks to Sonatype's OSS hosting!) makes it simple to use GroovyFX in everything from simple test scripts to larger projects. Consider the simple script below, which uses Groovy's Grab annotation, a part of the Grape system.
This script can be easily run from the command line using
groovy -classpath $JAVAFX_HOME/rt/lib/jfxrt.jar helloGroovyFX.groovyPro Tip: Fellow GroovyFX project member Dierk König suggests using the following tip (as discussed on this page) to make your JavaFX libraries available to all of your Groovy scripts:
mkdir ~/.groovy/lib ln -s $JAVAFX_HOME/rt/lib/* ~/.groovy/lib/Then you can just type:
groovy groovyFXHello.groovyProject Builds
GroovyFX can become part of a larger Gradle or Maven project by simply including it in your build file's dependencies as shown by this Gradle script.
The build script uses the
JAVAFX_HOME
environment variable to locate the JavaFX libraries. It also declares a dependency on GroovyFX 0.1 from the Maven Central repository.The script also incorporate's Dierk's makeDirs task. Once you've copied this script to the root directory of your new project, you can run
gradlew makeDirs
to create a standard Maven/Gradle project structure that includes directories like src/main/
and src/test
.Next you can copy the GroovyFX Hello World script shown above into your src/main/groovy directory (minus the @Grab annotation, since we now explicitly declare the dependency on GroovyFX in our Gradle build script). Then run
gradlew clean run
and you should see your new project compile and run, producing the window shown at the top of this post.The Maven script is left as an exercise to the masochistic reader.
Using GroovyFX 0.2-SNAPSHOT
You will need to grab a snapshot of the latest GroovyFX 0.2 library if you want to use it with the latest JavaFX 2.1 developer previews. Because we also have a snapshot version of the latest 0.2 changes available from Sonatype's repository, using it is a... um, snap... as shown by the following modifications to the previous Gradle build script.
One thing to be aware of is that the startup syntax in GroovyFX 0.2 has changed a bit. You no longer need to create an explicit reference to SceneGraphBuilder since one is created for you and passed as the delegate to your start closure. The script below shows our Hello World example modified to be compatible with GroovyFX 0.2.
That's all there is too it! Please try out these new releases and let us know if you have any problems or suggestions for improvements.
You should use the shorter @Grab variant:
ReplyDelete@Grab('org.codehaus.groovyfx:groovyfx:0.1')
Good point, I've updated the Gist. Thanks!
ReplyDelete