Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AspectJ + Gradle + Lombok does not work

There's a solution in ANT regarding this, but how do we accomplish this with gradle? Is it possible to do this via post-compilation weaving. Meaning compile with lombok to get all the generated delombok code, then have the aspect weave on this generated delombok code instead of aspectJ wiping it out?

These SO posts below don't seem to have anything conclusive about how to fix handle this?

Lombok does not work with AspectJ? Gradle + RoboBinding with AspectJ + Lombok are not compatible together

DiscussionThreadhttp://aspectj.2085585.n4.nabble.com/AspectJ-with-Lombok-td4651540.html

Thank you, Setzer

like image 701
Setzer Avatar asked Jul 11 '15 18:07

Setzer


Video Answer


1 Answers

Actually this question is quite old, but since a came across the same issue I want to share my solution anyway.

The best solution I found is this. In deed there's no built-in support for AspectJ in Gradle and the existing plugins (e.g., Gradle AspectJ plugin) do not work with Lombok. So the solution is to enable compile-time weaving in you code manually. A ready to go gradle.build for Java 8 is this

buildscript {
    repositories {
        jcenter()
        maven { url 'http://repo.spring.io/plugins-release' }
    }

    dependencies {

    }
}

apply plugin: 'idea' // if you use IntelliJ
apply plugin: 'java'

ext {
    aspectjVersion = '1.8.9'
    springVersion = '4.2.1.RELEASE'
}

repositories {
    jcenter()
}

configurations {
    ajc
    aspects
    compile {
        extendsFrom aspects
    }
}

dependencies {
    compile "org.aspectj:aspectjrt:$aspectjVersion"
    compile "org.aspectj:aspectjweaver:$aspectjVersion"

    ajc "org.aspectj:aspectjtools:$aspectjVersion"
    aspects "org.springframework:spring-aspects:$springVersion"
}

def aspectj = { destDir, aspectPath, inpath, classpath ->
    ant.taskdef(resource: "org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties",
            classpath: configurations.ajc.asPath)

    ant.iajc(
            maxmem: "1024m", fork: "true", Xlint: "ignore",
            destDir: destDir,
            aspectPath: aspectPath,
            inpath: inpath,
            classpath: classpath,
            source: project.sourceCompatibility,
            target: project.targetCompatibility
    )
}

compileJava {
    doLast {
        aspectj project.sourceSets.main.output.classesDir.absolutePath,
                configurations.aspects.asPath,
                project.sourceSets.main.output.classesDir.absolutePath,
                project.sourceSets.main.runtimeClasspath.asPath
    }
}

compileTestJava {
    dependsOn jar

    doLast {
        aspectj project.sourceSets.test.output.classesDir.absolutePath,
                configurations.aspects.asPath + jar.archivePath,
                project.sourceSets.test.output.classesDir.absolutePath,
                project.sourceSets.test.runtimeClasspath.asPath
    }
}

Additional explanation you can find in the article already mentioned above. The build.gradle given here is an updated version of the one given in the article to allow the usage of Java 8 and AspectJ version 1.8.9 and in addition all the unnecessary things are removed.

like image 165
Fable Avatar answered Oct 08 '22 02:10

Fable