Building OSGi applications that use AspectJ with Tycho / Maven 3

I just pushed a template for creating AspectJ applications with Tycho and will give you a quick tour of it here. You’ll find the template on github [1]

The template contains four infrastructure projects. One defines an OSGi command to calculate faculties and another contains an aspect to measure the duration of the calculation.

Let’s take a closer look. com.eclipsesource.weaving.demo.releng contains the Maven parent pom.xml file which defines the modules of the application:


com.eclipsesource.weaving.demo.platform – contains the target definition


com.eclipsesource.weaving.demo.feature – defines the application scope


com.eclipsesource.weaving.demo.repository contains the product definition. One very important VM argument, almost hidden from the debug arguments, registers the framework weaving extension:



The weaving service has to be started early.


As well as the four infrastructure projects, the template contains the application code:
com.eclipsesource.weaving.demo.faculty – provides an OSGi console command to calculate faculty.

public class FacultyCommandProvider implements CommandProvider {
  public void _faculty(CommandInterpreter ci) {
    String argument = ci.nextArgument();
    int argumentAsInteger = Integer.valueOf(argument).intValue();
    System.out.println("Faculty of " + argumentAsInteger + " is "
          + recur(argumentAsInteger));

com.eclipsesource.weaving.demo.chronometry – contains the Aspect to measure the faculty execution time.

public aspect CommandProviderChronometer {
  void around(CommandInterpreter ci):
        execution(void CommandProvider+._faculty(CommandInterpreter))
          &amp;&amp; args(ci) {
    long start = System.nanoTime();
    long duration = System.nanoTime() - start;
    System.out.println("Result calculated in " + (duration / 1000) + " micro seconds");

The following snippet from the project’s pom.xml file tells Tycho to use the aspectj compiler:


We build the application the Tycho way, from the command line with Maven 3.x:

> cd releng
> mvn package
[INFO] --- tycho-p2-director-plugin:0.12.0:archive-products (archive-products) @ com.eclipsesource.sandbox.weaving.demo.repository ---
[INFO] Building zip: .../
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO] ..... SUCCESS [0.002s]
[INFO] ... SUCCESS [0.269s]
[INFO] .... SUCCESS [1.995s]
[INFO]  SUCCESS [0.482s]
[INFO] .... SUCCESS [0.533s]
[INFO] . SUCCESS [7.262s]
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------

We can now unzip the artifact and launch the application:

> unzip

The following output shows that weaving was successful:

osgi> [org.eclipse.equinox.weaving.aspectj] info Starting AspectJ weaving service ...
[com.eclipsesource.weaving.demo.faculty] info register aspect
[org.eclipse.equinox.weaving.aspectj] info weaving bundle 'com.eclipsesource.weaving.demo.faculty'
[com.eclipsesource.weaving.demo.faculty] weaveinfo Join point 'method-execution(void'
in Type '' (
advised by around advice from '' (CommandProviderChronometer.aj:8)

Running the OSGi console command shows the measurement in action:

osgi> faculty 23
Faculty of 23 is 862453760
Result calculated in 1165 micro seconds


2 Responses to “Building OSGi applications that use AspectJ with Tycho / Maven 3”

  1. Hendy Irawan says:


    Thank you for publishing your AspectJ template on GitHub!

    I used it to post an article on Tycho here:

    I hope you don’t mind :-)

  2. Florian Waibel says:

    My pleasure!
    I’m glad you found the article helpful.

2 responses so far

Written by . Published in Categories: Planet Eclipse, Planet OSGi