Gradle+AspectJ Could not find method aspectj() for arguments "'aspectj' cannot be applied to '(?, java.lang.String, ?, ?)'"

huangapple go评论139阅读模式
英文:

Gradle+AspectJ Could not find method aspectj() for arguments "'aspectj' cannot be applied to '(?, java.lang.String, ?, ?)'"

问题

我已经在gradle的rootProjectbuild.gradle文件中添加了以下task,如下所示。我计划将AspectJ织入已编译的类中。

  1. plugins {
  2. id "io.freefair.aspectj.post-compile-weaving" version "5.1.1"
  3. }
  4. configurations {
  5. ajc
  6. aspects
  7. compile {
  8. extendsFrom aspects
  9. }
  10. }
  11. ext {
  12. aspectjVersion = '1.9.2'
  13. }
  14. dependencies {
  15. implementation gradleApi() // 用于自定义gradle任务/插件开发
  16. compile project('my-project')
  17. compile project('my-project1')
  18. compile project('my-project2')
  19. // ...
  20. ajc "org.aspectj:aspectjtools:1.9.2"
  21. aspects project('my-project')
  22. aspects project('my-project1')
  23. // ...
  24. }
  25. compileJava {
  26. doLast {
  27. aspectj project.sourceSets.main.output.classesDirs.getAsPath(),
  28. configurations.aspects.asPath,
  29. project.sourceSets.main.output.classesDirs.getAsPath(),
  30. project.sourceSets.main.runtimeClasspath.asPath
  31. }
  32. }
  33. def aspectj = { destDir, aspectPath, inpath, classpath ->
  34. ant.taskdef(resource: "org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties",
  35. classpath: configurations.ajc.asPath)
  36. ant.iajc(
  37. maxmem: "1024m", fork: "true", Xlint: "ignore",
  38. destDir: destDir,
  39. aspectPath: aspectPath,
  40. inpath: inpath,
  41. classpath: classpath,
  42. source: project.sourceCompatibility,
  43. target: project.targetCompatibility
  44. )
  45. }

但是,我遇到了一个错误Could not find method aspectj() for arguments。我在这里漏掉了什么?

  1. * 出现了什么问题:
  2. 任务 ':compileJava' 执行失败。
  3. > 无法找到方法 aspectj() 以进行参数传递[/Users/my-project/build/classes/java/main
  4. /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,:/...
  5. /Users//my-project/build/classes/java/main,
  6. /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版本详细信息如下:

  1. ------------------------------------------------------------
  2. Gradle 6.6
  3. ------------------------------------------------------------
  4. 构建时间:2020-08-10 22:06:19 UTC
  5. 修订版:d119144684a0c301aea027b79857815659e431b9
  6. Kotlin1.3.72
  7. Groovy2.5.12
  8. AntApache Ant(TM)版本1.10.8,于2020510日编译
  9. JVM1.8.0_152Oracle Corporation 25.152-b16
  10. 操作系统: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.

  1. plugins {
  2. id "io.freefair.aspectj.post-compile-weaving" version "5.1.1"
  3. }
  4. configurations {
  5. ajc
  6. aspects
  7. compile {
  8. extendsFrom aspects
  9. }
  10. }
  11. ext {
  12. aspectjVersion = '1.9.2'
  13. }
  14. dependencies {
  15. implementation gradleApi() // for custom gradle task/pugin development
  16. compile project('my-project')
  17. compile project('my-project1')
  18. compile project('my-project2')
  19. // ...
  20. ajc "org.aspectj:aspectjtools:1.9.2"
  21. aspects project('my-project')
  22. aspects project('my-project1')
  23. // ...
  24. }
  25. compileJava {
  26. doLast {
  27. aspectj project.sourceSets.main.output.classesDirs.getAsPath(),
  28. configurations.aspects.asPath,
  29. project.sourceSets.main.output.classesDirs.getAsPath(),
  30. project.sourceSets.main.runtimeClasspath.asPath
  31. }
  32. }
  33. def aspectj = { destDir, aspectPath, inpath, classpath ->
  34. ant.taskdef(resource: "org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties",
  35. classpath: configurations.ajc.asPath)
  36. ant.iajc(
  37. maxmem: "1024m", fork: "true", Xlint: "ignore",
  38. destDir: destDir,
  39. aspectPath: aspectPath,
  40. inpath: inpath,
  41. classpath: classpath,
  42. source: project.sourceCompatibility,
  43. target: project.targetCompatibility
  44. )
  45. }

But, I am getting an error Could not find method aspectj() for arguments. What am I missing here?

  1. * What went wrong:
  2. Execution failed for task ':compileJava'.
  3. > Could not find method aspectj() for arguments [/Users/my-project/build/classes/java/main,
  4. /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,:/...
  5. /Users//my-project/build/classes/java/main,
  6. /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:

  1. ------------------------------------------------------------
  2. Gradle 6.6
  3. ------------------------------------------------------------
  4. Build time: 2020-08-10 22:06:19 UTC
  5. Revision: d119144684a0c301aea027b79857815659e431b9
  6. Kotlin: 1.3.72
  7. Groovy: 2.5.12
  8. Ant: Apache Ant(TM) version 1.10.8 compiled on May 10 2020
  9. JVM: 1.8.0_152 (Oracle Corporation 25.152-b16)
  10. OS: Mac OS X 10.15.6 x86_64

答案1

得分: 0

这是一段帮助我解决这个问题的代码。

  1. def aspectj = {
  2. ant.taskdef(resource:"org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties", classpath: configurations.ajc.asPath)
  3. ant.iajc(
  4. maxmem: "1024m", fork: "true", Xlint: "ignore",
  5. destDir: sourceSets.main.output.classesDirs.asPath,
  6. aspectPath: configurations.aspects.asPath,
  7. sourceRootCopyFilter: "**/*.java",
  8. classpath: configurations.compile.asPath,
  9. source: project.sourceCompatibility,
  10. target: project.targetCompatibility
  11. ){
  12. sourceroots{
  13. sourceSets.main.java.srcDirs.each{
  14. if(!it.absolutePath.contains("/src/main/java")) {
  15. pathelement(location: it.absolutePath)
  16. }
  17. }
  18. }
  19. }
  20. }
英文:

Here is pice of code helped me to get rid of this issue.

  1. def aspectj = {
  2. ant.taskdef(resource:"org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties", classpath: configurations.ajc.asPath)
  3. ant.iajc(
  4. maxmem: "1024m", fork: "true", Xlint: "ignore",
  5. destDir: sourceSets.main.output.classesDirs.asPath,
  6. aspectPath: configurations.aspects.asPath,
  7. sourceRootCopyFilter: "**/*.java",
  8. classpath: configurations.compile.asPath,
  9. source: project.sourceCompatibility,
  10. target: project.targetCompatibility
  11. ){
  12. sourceroots{
  13. sourceSets.main.java.srcDirs.each{
  14. if(!it.absolutePath.contains("/src/main/java")) {
  15. pathelement(location: it.absolutePath)
  16. }
  17. }
  18. }
  19. }
  20. }

huangapple
  • 本文由 发表于 2020年8月25日 21:06:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63579514.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定