Why my custom task is executed at the wrong time?

Hi, please be gentle as I am new to Gradle… I have this custom task

task weave(dependsOn:processResources) {
    ant.taskdef( resource:"org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties", classpath: configurations.ajc.asPath)
    ant.iajc(classpath:configurations.compile.asPath, inpath:sourceSets.main.output.classesDir,
         destdir:'build/weaved-classes', aspectpath:'lib/my-aspect.jar')
}

that I use to weave my classes with my aspects (in my-aspect.jar).

then I have this

war {
    classpath 'build/weaved-classes'
    dependsOn += weave
}

to package the war file correctly. When I first run gradle war it produces a war file, everything seems good:

:compileJava
:processResources
:classes
:weave
:war
  BUILD SUCCESSFUL

but classes in the war file are not weaved… if I re-run gradle war I got:

:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:weave UP-TO-DATE
:war
  BUILD SUCCESSFUL

and now my classes are weaved correctly.

What am I doing wrong? Thank you

One thing that strikes me a bit odd is having both, the original classes and weaved classes in the same war. Is this intentional?

Hi, no it’s not intentional. I introduced the ‘build/weaved-classes’ dir because using the same dir as inpath and destdir was not working. How can I do to package only weaved classes? Or eliminate the need of the additional ‘build/weaved-classes’ dir?

One thing I’ve noticed on a first glance is that your weave task uses iajc at configuration time. This should be done during the execution phase. You should change your weave task to:

task weave(dependsOn:processResources) {
    doLast{
ant.taskdef( resource:"org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties", classpath: configurations.ajc.asPath)
    ant.iajc(classpath:configurations.compile.asPath, inpath:sourceSets.main.output.classesDir,
         destdir:'build/weaved-classes', aspectpath:'lib/my-aspect.jar')
    }
}

regards, René

You could have a look at the aspectj plugin I’ve wrote a while ago for handling aspects issues. It replaces the normal compileJava plugin with one that uses the iajc compiler. You can find more details and examples at http://wiki.gradle.org/display/GRADLE/Plugins#Plugins-AspectJplugin

regards, René

Thank you so much! This solved my problem

Actually I almost copied the code from you blog post, thank you again! Bye