Multi-project AspectJ builds with Gradle and Eclipse

Using Gradle for build/CI and Eclipse for development is a nice ecosystem with reasonable integration, but things get a bit trickier when we add multi-project builds and AspectJ into the mix. This post steps through some of the manual steps required to get it all working together.


Note: I am using the built-in gradle eclipse plugin, but not the eclipse gradle plugin

The multi-project build

For reasons beyond the scope of this post, I’m using three projects, in order of dependency:

model – a rich domain model
persistence – a project which uses AspectJ to layer a set of generic persistence-aware superclasses on top of model
modelp – a project which takes the emitted classes from persistence and adds all the necessary persistence plumbing, such as hibernate mappings, optimized DAOs, etc.

Gradle configuration

Details irrelevant to the multi-project configuration are omitted.

persistence project:

dependencies {
	ajInpath project(path: ':model', transitive: false)

The persistence project will then emit all of the classes from the model project woven with the aspects from persistence. Note that the upstream dependencies of model are not woven, nor are they automatically available to the persistence project. We need to use the normal gradle dependency mechanisms if we want to do that.

modelp project


dependencies {
	ajInpath project(path: ':persistence', transitive: false)

Eclipse configuration

So far so good. Gradle is pretty clever about wiring up multi-project builds. Eclipse is a little less clever, or maybe just different. So after

gradle eclipse

we still have some manual steps to do to recreate this setup in Eclipse.

AspectJ setup

Edited 7th June 2016, thanks to Daniel’s very helpful comment

Here we come to the first difference between Eclipse and Gradle. If we add the upstream project to the inpath, AspectJ will try to weave all of that project’s referenced libraries as well. In effect, Eclipse is missing the “transitive: false” argument we used in Gradle. This is (mostly) harmless (probably), but it’s slow and can throw spurious errors. So instead of adding the whole upstream project to the inpath, we add the project’s emitted class folder.

Together with adding the AspectJ nature to the project, the gradle code to configure eclipse looks like this for the modelp project:

eclipse {

	project {
		natures = ['org.eclipse.ajdt.ui.ajnature','org.eclipse.jdt.core.javanature']
		buildCommand 'org.eclipse.ajdt.core.ajbuilder'
	// Add the inpath entry to the classpath
	classpath {
		file {
			withXml {
				def node = it.asNode();
				node.appendNode("classpathentry",  [kind:"lib", path:"/model/bin"])
					.appendNode("attributes", [:])
					.appendNode("attribute", [name:"org.eclipse.ajdt.inpath", value:"org.eclipse.ajdt.inpath"]);


Dependent project setup

We still need the upstream project and its libraries to be available to the Eclipse compiler. The gradle eclipse plugin will take care of this if we have a normal compile project dependency in our gradle build (e.g. compile project(":model")), but we don’t necessarily need that for our gradle build. If we only have the inpath dependency the gradle eclipse plugin will miss it, so in Eclipse we also need to add the upstream project as a required project in the Java Build Path, like so:


Export exclusions

By default, adding the AspectJ nature to an Eclipse project causes it to export the AspectJ runtime (aspectjrt-x.x.x.jar). As all three of these projects are AspectJ projects, we end up with multiply defined runtimes, so we need to remove the runtime from the export list of the upstream projects.

Gradle is much better than Eclipse at dealing with complex dependency graphs. In particular, if an upstream project depends on an older version of a jar and a downstream project depends on a newer version of the same jar, the newer version will win. In Eclipse, both jars will be included in the classpath, with all the corresponding odd behaviour. So you might also need to tweak the export exclusions to avoid these situations.

Run configuration

Once you’ve cleaned up the exports from upstream projects, Eclipse will cheerfully ignore your exclusions when creating run or debug configurations, for example when running a JUnit test. This seems to be a legacy behaviour that has been kept for backward compatibility, but fortunately you can change it at a global level in the Eclipse preferences:


Make sure the last item, “only include exported classpath entries when launching”, is checked. Note that this applies to Run configurations as well, not just Debug configurations.


The manual Eclipse configuration needs to be redone whenever you do a gradle cleanEclipse eclipse, but usually not after just a plain gradle eclipse. It only takes a few minutes to redo from scratch, but it can be a hassle if you forget a step. Hence this blog post.

2 thoughts on “Multi-project AspectJ builds with Gradle and Eclipse

  1. I don’t know, if this would help you, but why not configuring everything in the Gradle file?
    The Gradle Eclipse Plugin is capable of generating many things within the .project and .classpath files. I used this during my curiosity phase of testing Gluon’s javafxports project.


    apply plugin: ‘java’
    apply plugin: ‘eclipse’

    // some properties are set up here…

    eclipse {
    pathVariables ‘GRADLE_HOME’: file(‘/home/daniel/programs/gradle-2.3’)

    project {
    name = ‘Test’
    comment = ‘JavaFX Test App’

    natures = [‘org.eclipse.xtext.ui.shared.xtextNature’, ‘org.eclipse.jdt.core.javanature’]

    buildCommand ‘org.eclipse.xtext.ui.shared.xtextBuilder’

    classpath {

    plusConfigurations += [ configurations.compile ]

    // containers ‘org.eclipse.fx.ide.jdt.core.JAVAFX_CONTAINER’

    defaultOutputDir = file(‘build-eclipse’)

    downloadSources = true
    downloadJavadoc = true

    file {

    withXml {

    def node = it.asNode()

    node.appendNode(“classpathentry kind=\”con\” path=\”org.eclipse.fx.ide.jdt.core.JAVAFX_CONTAINER\””)
    node.appendNode(“classpathentry kind=\”lib\” path=\”${_androidSdk}/platforms/${_androidTarget}/android.jar\””)

    // jdt {
    // sourceCompatibility = 1.8
    // targetCompatibility = 1.7

    // file {
    // whenMerged { jdt
    // }
    // … more jdt related stuff …
    // withProperties { properties ->
    // }
    // }
    // }

    In the end, this generated a project with ready to use JavaFX & Xtext configuration. The result is

    .classpath (obviously the dependent libraries are generated by the dependencies part of Gradle, I don’t need to take care of that):

    .project (see the Xtext part):

    JavaFX Test App




    You just need to know, what you actually need in your target .classpath and .project. The rest is well-enough documented on the Gradle Eclipse plugin sites.

Leave a Reply

Your email address will not be published. Required fields are marked *