AspectJ + Gradle configuration

I have been struggled with this for a while, so this the config I use and works well.

In your configuration do this.

configurations {
    ajc
    aspects
    aspectCompile
    compile{
        extendsFrom aspects
    }
}

In your dependencies use the following configuration. Spring dependencies are not needed if you are not using spring fwk.

dependencies {

    //Dependencies required for aspect compilation
    ajc "org.aspectj:aspectjtools:$aspectjVersion"
    aspects "org.springframework:spring-aspects:$springVersion"
    aspectCompile  "org.springframework:spring-tx:$springVersion"
    aspectCompile  "org.springframework:spring-orm:$springVersion"
    aspectCompile  "org.hibernate.javax.persistence:hibernate-jpa-2.1-api:$hibernateJpaVersion"

}

compileJava {
    sourceCompatibility="1.7"
    targetCompatibility="1.7"
    //The following two lines are useful if you have queryDSL if not ignore
    dependsOn generateQueryDSL
    source generateQueryDSL.destinationDir

    dependsOn configurations.ajc.getTaskDependencyFromProjectDependency(true, "compileJava")

    doLast{
        ant.taskdef( resource:"org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties", classpath: configurations.ajc.asPath)
        ant.iajc(source:"1.7", target:"1.7", destDir:sourceSets.main.output.classesDir.absolutePath, maxmem:"512m", fork:"true",
                aspectPath:configurations.aspects.asPath,
                sourceRootCopyFilter:"**/.svn/*,**/*.java",classpath:configurations.compile.asPath){
            sourceroots{
                sourceSets.main.java.srcDirs.each{
                    pathelement(location:it.absolutePath)
                }
            }
        }
    }
}

I dont use the plugin I use the ant and aspectj compiler to do that, probably there will be an easy way


Just want to add the so called "official" plugin for AspectJ mentioned by Archie.

Here's some gradle script example on how to do it:

apply plugin: 'java'



sourceCompatibility = '1.8'
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'

if (!hasProperty('mainClass')) {
    ext.mainClass = 'com.aspectz.Main'
}
buildscript {
  repositories {
    maven {
      url "https://plugins.gradle.org/m2/"
    }
  }
  dependencies {
    classpath "gradle.plugin.aspectj:gradle-aspectj:0.1.6"
    //classpath "gradle.plugin.aspectj:plugin:0.1.1"
    //classpath "gradle.plugin.aspectj:gradle-aspectj:0.1.1"
  }
}


ext {
    aspectjVersion = '1.8.5'
}

apply plugin: "aspectj.gradle"


repositories {
    mavenCentral()
}

dependencies {

    testCompile group: 'junit', name: 'junit', version: '4.10'
    compile("log4j:log4j:1.2.16")
    compile("org.slf4j:slf4j-api:1.7.21")
    compile("org.slf4j:slf4j-log4j12:1.7.21")
    compile("org.aspectj:aspectjrt:1.8.5")
    compile("org.aspectj:aspectjweaver:1.8.5")


}

However, it seems that it only supports Java 8 and above. As when you use java 7 to build it, it got error :

java.lang.UnsupportedClassVersionError: aspectj/AspectJGradlePlugin : Unsupported major.minor version 52.0

Looks like there is a new "official" gradle plugin for AspectJ:

https://plugins.gradle.org/plugin/aspectj.gradle

Unfortunately the documentation is minimal. I haven't tried it myself.