Integrating twitter4j and CQ/WEM

Sometimes on a project you know that you just have to write a blogpost, because you struggled too long with an issue to get it resolved.

The goal :
We wanted to use the twitter4j api inside on our Java-classes in the CQ5.4 installation.

The development was ok, and via maven we used the following dependency to make the code work :

<dependency>
<groupId>org.twitter4j</groupId>
<artifactId>twitter4j-core</artifactId>
<version>[2.2,)</version>
</dependency>

So far so good, deployment is then the next step. We use the “maven-bundle-plugin” to generate the manifest file for the OSGi-bundle.
After the deployment, the following errors showed up in the system-console (http://localhost:4502/system/console/bundles/)

twitter4j — Cannot be resolved
twitter4j.conf — Cannot be resolved

And the status of the bundle was set to “Installed” and not “Resolved”

These errors were caused because I didn’t put the twitter4j packages in the <Export-Package> element of the “maven-bundle-plugin”.

Here an example how I added the twitter4j in the <Export-Package> element.

<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Export-Package>
twitter4j.*
</Export-Package>
</instructions>
</configuration>
</plugin>

Next round, Next deployment

The previous two errors were solved, but I got three(!) new ones instead :

com.google.appengine.api.urlfetch — Cannot be resolved
dalvik.system — Cannot be resolved
twitter4j.internal.http.alternative — Cannot be resolved

Hmmm, that was not the result I was hoping for.

Now I had two options :
1. Finding out all the depedencies for dalvik, appengine etc, which you know will be a difficult route to take
2. Or getting to the bottom of it, and make it right with only the twitter4j dependency.
So I went for the second option :-)

The solution

After a number of searches, deployments and coffees I discovered that the three depedencies are actually optional and not needed.
You can specify this via this instruction in the <Import-Package>-element “;resolution:=optional”

<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Export-Package>
twitter4j.*
</Export-Package>
<Import-Package>
*,
twitter4j.internal.http.alternative;resolution:=optional,
dalvik.system;resolution:=optional,
com.google.appengine.api.urlfetch;resolution:=optional
</Import-Package>
</instructions>
</configuration>
</plugin>

And this did the trick, my bundle is set to “Resolved” and the twitter4j-api is working inside WEM/CQ!

VN:F [1.9.22_1171]
Was this helpful? Please rate the content.
Rating: 9.0/10 (1 vote cast)
Integrating twitter4j and CQ/WEM, 9.0 out of 10 based on 1 rating

About Feike Visser

Feike Visser (@heervisscher) is an experienced consultant working in the EMEA-region. Feike has done live implementations at large enterprises like NATO, BBC, Philips, Deutsche Bank, HMRC, HSBC.
This entry was posted in ADEP, Experience Services. Bookmark the permalink.

2 Responses to Integrating twitter4j and CQ/WEM

  1. J. Brown says:

    Thanks for the post. Just to mention another approach we commonly use, which is to deploy the twitter4j as it’s own OSGi bundle and then it can be available for use in all code packages deployed.

    net.homeip.yusuke
    twitter4j
    2.1.0


    Then bundled with maven-bundle-plugin and installed to Felix with maven-sling-plugin. Note that above is using a slightly older version of twitter4j and may be why we have not yet run into the optional dependency issue you hit.

  2. Eischen says:

    well great piece of info, thanks for posting