Calmdevelopment

Keep calm and code. Or do some other stuff.


An approach to a continuous jbake workflow.

I want to write a blog Post and watch the results of baking the static pages immediately within my browser, every time I save the document.

Create a jbake gradle project

Open a terminal and create a new jbake project.

mkdir jbake-continuous-example
cd jbake-continuous-example
mkdir -p src/jbake
cd src/jbake
jbake -i -t groovy
cd ../..

Create the build.gradle file with the following content:

buildscript {
  repositories {
      maven { url "http://dl.bintray.com/calmdev/gradle-plugins" }
      mavenLocal()
      jcenter()
  }

  dependencies {
    classpath 'me.champeau.gradle:jbake-gradle-plugin:0.2.1'
    classpath 'org.akhikhl.gretty:gretty:1.4.0'
  }
}

apply plugin: 'me.champeau.jbake'
apply plugin: 'war'
apply plugin: 'org.akhikhl.gretty'

gretty {
  httpPort = 8085
  contextPath = "/"
  extraResourceBases = ["$buildDir/jbake"]
}

jbake{
  version = "2.5.0-SNAPSHOT"
  asciidoctorjVersion = "1.5.4.1"
  configuration['db.store'] = "local" (1)
  configuration['db.path']= "build/cache" (2)
  configuration['site.host'] = "localhost"
  configuration['render.tags'] = true
  configuration['site.contextPath'] = "/"
  configuration['asciidoctor.attributes'] = [
    "imagesdir=/img",
    "source-highlighter=prettify"
  ]
}
1 The default setting is memory for an in-memory database.
2 It is maybe not the best idea to store the database directory to the build directory. Running gradle clean wipes the build directory and everything is lost. In my case I want to wipe the cached database clean. clean means clean…​

That’s it. A simplified gradle based jbake project, which adds a jettyRun task to serve the output of the bake task.

Bake it

To bake your site run

gradle bake

Serve it

To serve the baked content run

gradle jettyRun

The task starts a jetty server on localhost at the configured Port (8085).

:prepareInplaceWebAppFolder UP-TO-DATE
:createInplaceWebAppFolder UP-TO-DATE
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:prepareInplaceWebAppClasses UP-TO-DATE
:prepareInplaceWebApp UP-TO-DATE
:jettyRun
17:35:28 INFO  Jetty 9.2.10.v20150310 started and listening on port 8085
17:35:28 INFO   runs at:
17:35:28 INFO    http://localhost:8085/
Press any key to stop the server.

It blocks the terminal until you hit any key, as it’s output tells you.

jettyRun
Figure 1. jetty serving a baked website

gradle continuous build feature

Since gradle 2.5 the long awaited feature for continuous build/task execution has arrived. If you run a tasks with gradle -t <task…​>, gradle watches for changes of the task inputs and executes the task if one of the inputs did so.

That is great. Maybe we can use that to bake our static website whenever the file we are editing changes.

jbake continuous

So lets start the bake task as a continous build.

gradle -t bake

Starting a new Gradle Daemon for this build (subsequent builds will be faster).
Continuous build is an incubating feature.
:bake UP-TO-DATE

BUILD SUCCESSFUL

Total time: 6.152 secs

Waiting for changes to input files of tasks... (ctrl-d to exit)

Ok. Thats promising.

I added a new File. Saved it. And…​taaataaaaa…​the change was recognized and the task got executed again.

Change detected, executing build...

:bake

BUILD SUCCESSFUL

Total time: 5.302 secs

Waiting for changes to input files of tasks... (ctrl-d to exit)

It works!!!1!

Amazing. It works. And it wasn’t that hard. Now I can start a jetty server, hack on my blog post, and watch the changes every time I save the document.

Everything I need is

  • a Terminal with two windows

    • one for the jettyRun task

    • one for the continuous jbake task

  • a browser

  • an editor

Info

  • jbake gradle plugin Version 0.2.1

    I use a version published to a private repository at bintray.

  • jbake Version 2.5.0-SNAPSHOT (current master)

    No risk no fun…​ :P

2014 - 2017 | Mixed with Foundation v6.2.3 | Baked with JBake v2.5.1