Groovy Map Minus

06 June 2017

Groovy is relatively intuitive for me: I can usually guess a method or operator and find it works as I had guessed.

Removing a key/value by the key from a Map conveniently eludes me though. I always guess that I should be able to minus a key from a Map to produce a new Map with that entry /value removed. That doesn’t work, so I needed to invent my own. I still not sure how to practically apply it everywhere though, or if it’s even worthwhile. In practice, I always end up writing the findAll inline.

@Category(Map)
class MapMinus {
    Map minus(Collection keys) {
        this.findAll { k, v -> ! (k in keys) }
    }
    Map minus(Object key) {
        this - [key]
    }
}

use (MapMinus) {
    assert [b: 2, c: 3,] == [a: 1, b: 2, c: 3,] - 'a'
    assert [b: 2, c: 3,] == [a: 1, b: 2, c: 3,] - ['a']

}


JBake with MarkupTemplateEngine

03 November 2016

JBake 2.5.0 got support for the MarkupTemplateEngine, so I wanted to give it a try, since structured code will be nicer than extreme scriptlet stuff that was happening in the original Groovy template example. The stock MTE example shows off MTE templates, but it also switched to Foundation in place of Bootstrap.

I’ve been looking forward to really learning Bootstrap for work and my personal projects, so I’m not looking to switch frameworks right now, so I rebuilt my own example project with MTE and Bootstrap based on the original Groovy/Bootstrap sample I had previously used.

Hopefully, I’ll get a little feedback and the JBake people will incorporate my contribution.


JBake

05 January 2016

I’ve moved the blog to a static site generated by JBake. The source for the content lives in my techblog project in Github, so I have a full versioning of my content for the small price of a git workflow.

I installed JBake using the familiar SDKMan that I already use to manage my Grails and Groovy installations. I initialized it with the Groovy templating engine and have started customizing the templates.

To make sure this thing is easy to update, I keep a local clone of the repo, so I can update it any time and push whenever I’m ready. I have a shell script scheduled to run on the server which basically does:

cd techblog
git fetch
git merge | grep "Already" > /dev/null || jbake

That little bit of code only runs jbake if the git pull doesn’t say "Already up-to-date". That provided me a simple little "continuous integration" hook that polls git for changes to trigger the build. I’ll probably use this trick in other places.

I brought all my old content from my old database into the new platform using a quick little Groovy script to dump out an HTML file for each article including the header of metadata for JBake’s use. While most of these old articles will remain HTML, I intend to use AsciiDoctor format for all the new stuff.

I’ve been collecting a long list of (mostly technical) articles to write, but replacing the old platform kept trumping my attempts to write. Hopefully, this move can open the flood gates, and eventually, I’ll break out another instance of it for the photography blog. JBake should make it easy and interesting to continue the blogs.


Quick and Easy Groovy for the Web

22 June 2011

Groovy can be used pretty easily to spin up some simple web pages in almost the same way one would hack out some PHP or JSP without going to the trouble to do an all-out Grails project.

The Groovy Servlet allows you to pack up the groovy-all-*.jar, a simple web.xml, and whatever *.groovy scripts you want and deploy it right into Tomcat as a plain WAR file. The Groovy Servlet page

Here's a bit of a script I put together to jump start a simple Groovlet project by packaging a WAR file from a directory of scripts. This isn't Groovy Servlet code itself, but just a command-line tool. (The Groovy Servlet page linked previously has examples for writing your own servlets.) This script will copy in the Groovy JAR and generate the basic web.xml to wire up the GroovyServlet to dynamically execute your scripts. I also have a downloadable copy of package_groovlet.groovy.

#!/usr/bin/env groovy

if (args.size() < 1) {
    print """\
        |Usage: package_groovlet.groovy <war-name>
        |Package the current directory into a Groovy Servlet war.
        |""".stripMargin()
    return
}

def war = args[0]
def embed = "${System.getenv()['GROOVY_HOME']}/embeddable"

def ant = new AntBuilder()

ant.sequential {
    delete(dir: 'build')
    mkdir(dir: 'build/WEB-INF/lib')
    copy(toDir: 'build/WEB-INF/lib') {
        fileset(dir: embed) {
            include(name: 'groovy-all-*.jar')
        }
    }
    copy(toDir: 'build') {
        fileset(dir: '.') {
            exclude(name: 'build/**')
        }
    }
}

new FileOutputStream('build/WEB-INF/web.xml').withWriter { webxml ->
    webxml.print """\
        <!DOCTYPE web-app PUBLIC
          "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
          "http://java.sun.com/dtd/web-app_2_3.dtd" >
        <web-app>
            <servlet>
                <servlet-name>Groovy</servlet-name>
                <servlet-class>groovy.servlet.GroovyServlet</servlet-class>
            </servlet>
            <servlet-mapping>
                <servlet-name>Groovy</servlet-name>
                <url-pattern>*.groovy</url-pattern>
            </servlet-mapping>
         </web-app>
     """.stripIndent()
}

ant.jar(destfile: "build/${war}", basedir: 'build')
println "Created build/${war}"


All the Posts

June 2017

November 2016

January 2016

June 2011

September 2010

January 2010

June 2008

April 2004