JBake is a “Java based, open source, static site generator” (http://jbake.org/, on github https://github.com/jbake-org/jbake). It is a great choice if you want to create static HTML websites. No slow loading of dynamic content anymore. Additionally, if a site is created on load by JavaScript, issues with indexing and search engine optimization/SEO might occur. This is not a problem with static sites. At the same time different template engines like FreeMarker (http://freemarker.org/) help you modularize the website. You only have to define menu and footer once and they will be inserted automatically.
JBake was perfect for us to create a microsite with 15 different pages and lots of reappearing elements. We integrated JBake into Maven to create a comfortable release process. To do this we used the plug-in jbake-maven-plugin
(https://github.com/ingenieux/jbake-maven-plugin). This sample project shows the fundamental integration: https://github.com/ingenieux/jbake-sample.
While integrating JBake into Maven you have to consider a few things. For our site we used Maven-3.0.5, JDK-1.7.0-71 and jbake-maven-plugin-0.0.3, because we had version conflicts with other combinations (newer version of jbake-maven-plugin
need Maven-3.1.1).
This is the plug-in configuration of the pom.xml
<plugin> <groupId>br.com.ingenieux</groupId> <artifactId>jbake-maven-plugin</artifactId> <version>0.0.3</version> <executions> <execution> <id>default-generate</id> <phase>generate-resources</phase> <goals> <goal>generate</goal> </goals> </execution> </executions> <configuration> <inputDirectory>${project.basedir}/src/main/resources</inputDirectory> <outputDirectory>${project.build.directory}/classes</outputDirectory> </configuration> <dependencies> <!-- for freemarker templates (.ftl) --> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.20</version> </dependency> </dependencies> </plugin>
In detail:
The dependencies need the definition of the template engine, FreeMarker in this case.
<dependencies> <!-- for freemarker templates (.ftl) --> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.20</version> </dependency> </dependencies>
The configuration of the directories is very important. Per default the jbake-maven-plugin
uses the path src/main/jbake
. This doesn’t go well with the default path src/main/resources
, which is used by lots of other plug-ins. Additionally, all the freshly baked goods are supposed to end up in the /classes
directory.
<configuration> <inputDirectory>${project.basedir}/src/main/resources</inputDirectory> <outputDirectory>${project.build.directory}/classes</outputDirectory> </configuration>
After that everything can be packaged and we can ignore all the files that aren’t needed anymore. To do this we configured the jar-plugin (or zip, depending on what you use). The source files of /assets
, /content
and /templates
can be excluded (the .less
files are from LESS integration, not from JBake. The template-draft.html
is an internal template that shouldn’t end up on the server).
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.4</version> <configuration> <excludes> <exclude>**/less</exclude> <exclude>**/*.less</exclude> <exclude>**/assets</exclude> <exclude>**/assets/**</exclude> <exclude>**/content</exclude> <exclude>**/content/**</exclude> <exclude>**/templates</exclude> <exclude>**/templates/**</exclude> <exclude>**/template-draft.html</exclude> </excludes> </configuration> </plugin>
And that was it. With a mvn clean install
we can easily bake together all content files and the result is a static site.
Happy baking!
Filed under: Enterprise Applications Tagged: HTML, integration, Java, jbake, Maven, static site, web application
