Thursday, April 28, 2016

Maven + Lucee, Part 2

Maven + Lucee, Part 2

So in part 1, we walked through setting up a WAR application with Lucee 5 embedded.  In this post, I will walk through creating a Maven project to produce a LAR artifact from a set of utility CFCs.

So you may ask, what is a LAR?  It stands for Lucee ARchive, and it is a zip archive of your CFML components and templates.  They come in two flavors: regular archives and component archives.  Regular archives are mapped with virtual paths and can be made web-accessible - think webjars for CFML templates.  Component archives are more similar to JARs as you know them, and are useful for bundling APIs or utility libraries for reuse.

Some other interesting features about LAR archives is that they contain the actual Java class files generated from your CFML code.  This is important for two reasons.  It means that the Lucee engine does not have to do its JIT compilation.  It also means that LAR files allow you to build source-less deploys if you are wanting to provide commercially-licensed packages.

So let's get started.  For this tutorial, I will again be using CFEclipse with M2E.  I will also be using a custom maven plugin I've been working on to provide the lar packaging type, which simplifies this process greatly.

Creating a project

Typically for a LAR project, I start by creating a simple Maven Java project:
  1. File > New > Maven Project (or just Project, then search for Maven Project)
  2. Check the box labeled Create a simple project (skip archetype selection)
  3. Click next to continue filling out the project information, and click Finish when you are done.
Once you have your project created, you need to reconfigure your source folders.
  1. Rename the src/main/java folder to src/main/lucee. The easiest way to do this is by clicking the folder, hitting F2 and entering lucee in the dialog.
  2. For the time being, delete the src/test folders.  I am not aware of any plugins that are compatible with surefire that can do automated CFML testing (although that is next on my list).
Now it is time to configure the project to use the lar-maven-plugin.  As of now, the plugin is only available as a snapshot release.

To make use of this project, simply add the following to your pom.xml:

  <packaging>lar</packaging>

  <build>
    <plugins>
        <plugin>
            <groupId>org.jdsnet.maven.lucee</groupId>
            <artifactId>lar-maven-plugin</artifactId>
            <version>0.1.0-SNAPSHOT</version>
            <extensions>true</extensions>
        </plugin>
    </plugins>
  </build>

  <pluginRepositories>
    <pluginRepository>
      <id>dajester2013-snapshots</id>
      <url>http://dajester2013.github.io/maven-snapshots/</url>
    </pluginRepository>
  </pluginRepositories>

Your project is now set up.  By default, the plugin is configured to build a component archive.  To build a regular archive, you can add the following configuration to the plugin:

  <configuration>
    <larType>mapping</larType>
    <larVirtualPath>/my-mapping</larVirtualPath>
  </configuration>

You can use M2E's completions to see other options.

Building your project

At this point, your project is ready to start adding your CFML source.  You can begin creating packages/components/etc. within your lucee source directory.  When you are ready to build, you can right-click on the project and select Run As > maven install.  This will execute the packaging phase, and install the .lar artifact in your local repository.

Hopefully this will prove helpful to the community moving forward!  Any questions feel free to ask below, or check out the plugin project at https://github.com/dajester2013/lucee-maven-plugin/.