英文:
Gradle+AspectJ Could not find method aspectj() for arguments "'aspectj' cannot be applied to '(?, java.lang.String, ?, ?)'"
问题
我已经在gradle的rootProject的build.gradle文件中添加了以下task,如下所示。我计划将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() // 用于自定义gradle任务/插件开发
    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。我在这里漏掉了什么?
* 出现了什么问题:
任务 ':compileJava' 执行失败。
> 无法找到方法 aspectj() 以进行参数传递[/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
------------------------------------------------------------
构建时间:2020-08-10 22:06:19 UTC
修订版:d119144684a0c301aea027b79857815659e431b9
Kotlin:1.3.72
Groovy:2.5.12
Ant:Apache Ant(TM)版本1.10.8,于2020年5月10日编译
JVM:1.8.0_152(Oracle Corporation 25.152-b16)
操作系统:Mac OS X 10.15.6 x86_64
英文:
I've added following task in gradles's rootProject build.gradle file as below.  I'm planning to weave AspectJ to compiled classes.
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
    )
}
But, I am getting an error Could not find method aspectj() for arguments. What am I missing here?
* 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 Version Details are as below:
------------------------------------------------------------
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
答案1
得分: 0
这是一段帮助我解决这个问题的代码。
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)
                }
            }
         }
    }
 }
英文:
Here is pice of code helped me to get rid of this issue.
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)
                }
            }
         }
    }
 }
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论