Gradle+AspectJ 找不到方法 aspectj() 参数“'aspectj' 不能应用于 '(?, java.lang.String, ?, ?)'”
Gradle+AspectJ Could not find method aspectj() for arguments "'aspectj' cannot be applied to '(?, java.lang.String, ?, ?)'"
我在 gradles 的 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() // 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)
}
}
}
}
}
我在 gradles 的 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() // 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)
}
}
}
}
}