Gradle + AspectJ无法为参数“'aspectj'无法应用于'?,java.lang.String,?,?'”找到方法aspectj

问题描述

我在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 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)
                }
            }
         }
    }
 }

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...