Unfortunately, there's no built-in support for AspectJ in Gradle. But there are a lot of resources, even plugins (e.g., Gradle AspectJ plugin), how to get them working together. The only complaint I have about them is that they substitute the AspectJ compiler and bytecode weaver for the native Java compiler. It doesn't work sometimes. For example, I like Lombok but it and the Aspectj compile-and-weaving process are at odds. So I had to change the solution described in Working With Gradle, Spring Aspects and Compile-time Weaving a bit.
The main idea is the same. We introduce new configurations:
You can find a working example here.
The main idea is the same. We introduce new configurations:
configurations {
ajc
aspects
compile {
extendsFrom aspects
}
}
and add required dependencies:
compile "org.aspectj:aspectjrt:$aspectjVersion" compile "org.aspectj:aspectjweaver:$aspectjVersion" ajc "org.aspectj:aspectjtools:$aspectjVersion" aspects "org.springframework:spring-aspects:$springVersion"Then we define a closure:
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
)
}
and change the standard Compile tasks:
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
}
}
That's all. Lombok and AspectJ have been reconciled.You can find a working example here.