我在gradles的task rootProject文件中添加了以下build.gradle,如下所示。我打算将AspectJ编织到已编译的类中。

plugins {
        id "io.freefair.aspectj.post-compile-weaving" version "5.1.1"
    }

configurations {
    ajc
    aspects
    compile {
        extendsFrom aspects
    }
}

ext {
    aspectjVersion = '1.9.2'
}

dependencies {
    implementation gradleApi() // for custom gradle task/pugin development

    compile project('my-project')
    compile project('my-project1')
    compile project('my-project2')
    // ...

    ajc "org.aspectj:aspectjtools:1.9.2"
    aspects project('my-project')
    aspects project('my-project1')
    // ...
}

compileJava {
    doLast {
        aspectj project.sourceSets.main.output.classesDirs.getAsPath(),
                configurations.aspects.asPath,
                project.sourceSets.main.output.classesDirs.getAsPath(),
                project.sourceSets.main.runtimeClasspath.asPath
    }
}

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
    )
}
但是,我收到了错误Could not find method aspectj() for arguments。我在这里想念什么?
* What went wrong:
Execution failed for task ':compileJava'.
> Could not find method aspectj() for arguments [/Users/my-project/build/classes/java/main,

/Users//my-project/lib/lib-jars/httpcomponents-client-4.5.11/httpclient-4.5.11.jar:/Users//my-project/lib/lib-jars/aws/aws-sdk-modules-2.0.jar,:/...

/Users//my-project/build/classes/java/main,

/Users//my-project/my-sub-project/build/libs/my-sub-project-8.2.jar:/Users//.gradle/caches/modules-2/files-2.1/org.aspectj/aspectjrt/1.9.6/1651849d48659e5703adc2599e694bf67b8c3fc4/aspectjrt-1.9.6.jar,:/...]
Gradle版本详细信息如下:
------------------------------------------------------------
Gradle 6.6
------------------------------------------------------------

Build time:   2020-08-10 22:06:19 UTC
Revision:     d119144684a0c301aea027b79857815659e431b9

Kotlin:       1.3.72
Groovy:       2.5.12
Ant:          Apache Ant(TM) version 1.10.8 compiled on May 10 2020
JVM:          1.8.0_152 (Oracle Corporation 25.152-b16)
OS:           Mac OS X 10.15.6 x86_64

最佳答案

这是一段代码,帮助我摆脱了这个问题。

def aspectj = {

ant.taskdef(resource:"org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties", classpath: configurations.ajc.asPath)

ant.iajc(
        maxmem: "1024m", fork: "true", Xlint: "ignore",
        destDir: sourceSets.main.output.classesDirs.asPath,
        aspectPath: configurations.aspects.asPath,
        sourceRootCopyFilter: "**/*.java",
        classpath: configurations.compile.asPath,
        source: project.sourceCompatibility,
        target: project.targetCompatibility
      ){
         sourceroots{
            sourceSets.main.java.srcDirs.each{
               if(!it.absolutePath.contains("/src/main/java")) {
                    pathelement(location: it.absolutePath)
                }
            }
         }
    }
 }

09-27 06:10