Kennis Blogs Creating a Dynamic Plugin with DotCMS and Maven

Creating a Dynamic Plugin with DotCMS and Maven

Avisi recently started using dotCMS. We are currently moving our old website from Wordpress to DotCMS. On the old website there is a Twitter feed which we got from a Wordpress Plugin. For DotCMS we had to create our own widget because their given Twitter plugin makes use of the old Twitter API.


Getting the DotCMS jar in Maven

I will walk you through our process of creating a twitter plugin.


In DotCMS you have Dynamic Plugins which are basically just OSGI plugins. If you follow their walkthrough you have to fiddle with Ant. At Avisi we use Maven so I created a plugin with Maven. The first step to get this working is making the DotCMS jar available for maven. If you download the the Community Edition and unpack it you can find the jar at




To use this jar with maven you have to install this jar locally:

mvn install:install-file -Dfile=dotcms_X.X.X.jar -DgroupId=com.dotcms \
-DartifactId=dotcms -Dversion=X.X.X -Dpackaging=jar


Creating the pom.xml

The next step was creating a pom.xml for our maven project with the following dependencies:






The code for the Dynamic Plugin

To show a twitter feed we shall make a ViewTool that will be accessible from velocity templates.

package nl.avisi.dotcms.plugin.twitter;


import java.util.List;

import twitter4j.*;
import twitter4j.conf.ConfigurationBuilder;

public class TwitterTool implements ViewTool {

private Twitter twitter;

public void init(Object initData) {
ConfigurationBuilder cb = new ConfigurationBuilder();
TwitterFactory tf = new TwitterFactory(;
twitter = tf.getInstance();

public List search(String queryString, String count) throws TwitterException {
Query query = new Query(queryString);
QueryResult result =;
return result.getTweets();



This is pretty simplistic example, but it does the job. The next step is configuring the ViewTool so it will become available in all your velocity templates under the key "avisiTwitterTool".


package nl.avisi.dotcms.plugin.twitter;


public class TwitterToolInfo extends ServletToolInfo {

public String getKey () {
return "avisiTwitterTool";

public String getScope () {
return ViewContext.APPLICATION;

public String getClassname () {
return TwitterTool.class.getName();

public Object getInstance ( Object initData ) {

TwitterTool viewTool = new TwitterTool();
viewTool.init( initData );

return viewTool;



The next step is actually activating the Dynamic Plugin, this can be done by creating a BundleActivator which will register and unregister our new TwitterViewTool:


package nl.avisi.dotcms.plugin.twitter;

import com.dotmarketing.osgi.GenericBundleActivator;

import org.osgi.framework.BundleContext;

public class Activator extends GenericBundleActivator {

public void start ( BundleContext bundleContext ) throws Exception {
//Initializing services...

//Registering the ViewTool service
registerViewToolService( bundleContext, new TwitterToolInfo() );

public void stop ( BundleContext bundleContext ) throws Exception {



This is all the necessary code to get a TwitterViewTool in dotCMS.


Package the Dynamic plugin

To actually get this code working as a Dynamic Plugin we need to make some changes in the pom.xml. This is necessary because the jar we create will become an OSGI plugin, a jar that's get's packaged as an OSGI plugin is called a bundle. To make a bundle from our jar we need to create a MANIFEST.MF file that contains all the information that the OSGI container needs to load our Dynamic Plugin. Luckily we get some help from maven in creating this file in the form of a maven plugin, I added the following to my pom.xml




The most important part of this configuration are the bundle activator property that lists our Activator class and the Import-Package property that lists all the dotCMS classes we want to use. You can view the complete pom.xml here.


Loading the Dynamic Plugin in DotCMS

Congratulations, you've just created your first Dynamic Plugin for dotCMS with maven. Go ahead and load the plugin in your dotCMS instance and start creating awesome widgets with your Twitter Feeds. Below is a little code snippet you can use to render tweets:


<ul class="list-group avisi-footer-content">
#foreach($tweet in $$query, $numberOfResults))
<li class="list-group-item">
#set($url = "!/${tweet.User.ScreenName}/status/$tweet.Id")
<a href="$url">$tweet.Text</a> <br />
<span class="text-muted">@$tweet.User.ScreenName</span>

Happy coding!