SpringOne 2GX 2011

Chicago, October 25-28, 2011

Griffon 0.9 sneak peek: application archetypes

Posted by: Andres Almiray on 07/02/2010
Yesterday saw the first post regarding the new features that can be found in the upcoming Griffon 0.9 release. Those features were inspired by Grails and Gradle. Today I'd like to talk about another neat feature, this time inspired by Maven. Hold on! don't go away just yet! I promise it's a good one, honest.

Since the beginning a developer was able to create new artifacts using a predefined template, usually located somewhere in the Griffon distribution install directory. Developers also had the choice to provide their own templates local to an application, or even bundle them in plugins. While all this works fine and dandy the missing piece was being able to tweak the initial MVC group that is generated when an application is created, simply because no local templates are available as the application has not been created yet; same thing with plugins.

Application archetypes fill that void.

Application archetypes are simple zip files with an application descriptor and templates. Despite this, Griffon provides a few scripts that let you manage archetypes: create-archetype, package-archetype, install-archetype, uninstall-archetype. Here's how the default archetype descriptor (application.groovy) looks
import griffon.util.Metadata

includeTargets << griffonScript("_GriffonPlugins") includeTargets << griffonScript("_GriffonInit") includeTargets << griffonScript("CreateMvc" )  target(createApplicationProject: 'Creates a new application project') {     createProjectWithDefaults()     createMVC()      // to install plugins do the following     // Metadata md = Metadata.getInstance("${basedir}/application.properties")     // installPluginExternal md, pluginName, pluginVersion     //     // pluginVersion is optional } setDefaultTarget(createApplicationProject)
Let's deconstruct it piece by piece. An archetype descriptor awfully resembles your typical Griffon gant script. As a matter of fact it is your typical Griffon gant script with a predefined default target name. The name should not be changed as it is there by convention. This target creates a project using the default settings then creates the first MVC group. Notice the commented out code, it tells you that you can also install plugins during the application creation phase, ain't that great?

Templates are stored relative to the archetype descriptor under templates/artifacts. You're free to modify and add as many templates as needed. You can also add other files that can be copied to the application, like a LICENSE.TXT or README for example, just remember you must write the copying code yourself in the archetype descriptor.

Archetypes are registered with the application metadata so that when a new artifact is created the appropriate template is used. However if you wish to override this setting while creating an artifact then specify an -archetype=<name> argument to the create-* script. The following is the template search order
  • local to the application -> $basedir/src/templates/artifacts
  • provided by a plugin -> $pluginHome/*/src/templates/artifacts
  • local archetype -> ~/.griffon/<version>/archetypes/<name>/templates/artifacts
  • global archetype -> $griffonHome/archetypes/<name>/templates/artifacts
There is a default archetype that will be used as fallback if no suitable template/archetype is found. The default archetype is the sole one provided by Griffon 0.9. Future releases may provide additional archetypes.

The archetype install options are very similar to plugins, you can install them from a zip file location or from an URL. There is no central management for archetypes at the moment as there is for plugins.

That's all for now. Keep on Groovying!

About Andres Almiray

Andres Almiray

Andres is a Java/Groovy developer and Java Champion, with more than 11 years of experience in software design and development. He has been involved in web and desktop application developments since the early days of Java. He has also been teacher of computer science courses in the most prestigious education institute in Mexico. His current interests include Groovy and Swing. He is a true believer of open source and has participated in popular projects like Groovy, Griffon, JMatter and DbUnit, as well as starting his own projects (Json-lib, EZMorph, GraphicsBuilder, JideBuilder). Founding member and current project lead of the Griffon framework. He blogs periodically at http://jroller.com/aalmiray. You can find him on twitter too as @aalmiray. He likes to spend time with his beloved wife, Ixchel, when not hacking around.

More About Andres »

NFJS, the Magazine

December Issue Now Available
  • BDD and REST

    by Brian Sletten
  • Mocks and Stubs in Groovy Tests

    by Kenneth Kousen
  • Algorithms for Better Text Search Results

    by John Griffin
  • Knowns and Unknowns of Scrum and Agile

    by Brian Tarbox
Learn More »