Sunday, December 30, 2012

Scala JavaFX Archetype

The scala-javafx-archetype helps you set up an environment for coding JavaFX with Scala.

Waiting at The Stairs
Waiting at The Stairs von aalfath bei Flickr
Last week I've published an archetype for maven to generate a simple maven project using Scala and JavaFX on GitHub.

In my opinion it is most important to have such gadgets to ease the burden of setting up an environment to start coding. I know from myself that you'll quickly lose interest in exploring a technology (at least in your sparetime ;-) ) if you have to solve a plethora of things before you actually come to the point which interests you.

There are also other archetypes which do roughly the same, like the one of Schlichtherle IT Services. In its latest version it uses still Scala 2.8.2 and JDK 1.6, which is not what I wanted. Looking at the generated pom, it uses also Scalacheck which I didn't need and old versions of the scala-maven-plugin.

Besides in the meantime, Daniel Zwolenski released a great maven plugin for building JavaFX applications which I wanted to support.

Thirdly, I just wanted to know how to get a maven artifact to the central repository. Being (among other things) a 10y+ Java developer, it just amazes me how some projects bootstrap themselves just by calling mvn package at the right time and the right place.


Thus my decision to implement this Hello World archetype, which, by the way, took less time than to write this blog post.

In hindsight, to my surprise, the process of getting into maven central was not really that hard, although you have to know quite some stuff and should have at least a glimpse of an idea what all related technologies are good for.

Alors ...

What is a maven archetype?

In short, Archetype is a Maven project templating toolkit. An archetype is defined as an original pattern or model from which all other things of the same kind are made. The names fits as we are trying to provide a system that provides a consistent means of generating Maven projects. Archetype will help authors create Maven project templates for users, and provides users with the means to generate parameterized versions of those project templates.
For a thorough explanation see the introduction documentation on the original apache site. 

Just a little warning: The documentation on maven is fragmented, there are bits and pieces written from different authors for different versions of the maven product itself. As such, in my opinion, it is sometimes confusing and harder than it should be to follow the main point. 

On the other hand it is a hard problem to provide good documentation, maybe you can contribute

How to create an archetype?


You can do it the hard way (and maybe you should do it at least once to learn) like described here, but in the meantime the awesome maven guys provided a tooling to generate a maven archetype project out of existing ones. 

That means if you have your maven project already set up, it is a piece of cake to create an archetype project. The magic spell is  mvn archetype:create-from-project.

I would advise you to try out this command and look into the target/generated-sources/.
You'll discover that even creating an archetype is almost for free. More information on that stuff is provided here.

Keep in mind that you will have to have at least two pom.xml, one for your artifact (your template) and one for the archetype itself. Those two poms will have different responsibilities and hence different dependencies, parents and so on. Tip:  create first a properly working prototype of your template, and only then start to "archetype" it. Typically, your archetype's pom will be rather simple, whilst the pom of your template project can be very complex.

You can provide your own parameters and do lots of things to parameterize your archetype, but this is out of scope for now.

How to get my open source project into Maven Central?


Archetypes are deployed like any other artifact in the central repository, there is no special difference to pay attention to. That means you should follow the guide to releasing artifacts in maven central.

In short, you just have to create an account on the sonatype nexus service and configure your settings.xml and your pom with a fixed parent. By "extending" the Sonatype OSS Parent POM you'll get magically everything you need to publish your project to public available Snapshot and Release Repositories.

For example, if you want to publish a Snapshot version of your application, a simple mvn deploy suffices.

To publish a release, you have to enter three commands:

  • mvn release:clean
  • mvn release:prepare
  • mvn release:perform

After that, there is some manual step (which can also be automated) and then your artifact will be hosted on Maven Central, which means that for every maven user on the world your library is readily available by providing your groupId, artifactid and version number.

But wait - where is JavaFX here?


Neither JavaFX nor Scala have anything to do with maven archetypes or deploying artifacts to maven central. You could write an artifact for your next great project idea using your favorite technologies, just like I did for the scala-javafx-archetype.

This archetype sets up a properly configured maven project using Scala and JavaFX, to try yourself you need a recent JDK and maven installed. Moreover you will need the JavaFX libraries in your classpath, with JDK8 this will be the default.

If this applies to you, you can jumpstart into JavaFX development with Scala by entering the following in your command line:

mvn archetype:generate -DarchetypeGroupId=net.ladstatt -DarchetypeArtifactId=scala-javafx-archetype -DarchetypeVersion=0.1

For more information visit the github repository.

No comments:

Post a Comment

Post a Comment