You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

aspectj.gradle 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import java.nio.file.Files
  2. import java.nio.file.StandardCopyOption
  3. import java.time.LocalDateTime
  4. import java.time.temporal.ChronoUnit
  5. logger.info "开始配置aspectj"
  6. //D:\work\ideaworkspace23\ds.dts.daes\build\classes\java\main
  7. logger.info "inpath:" + project.sourceSets.main.output.classesDirs.singleFile.absolutePath
  8. //
  9. logger.info "destDir:" + project.sourceSets.main.output.classesDirs.singleFile.absolutePath
  10. //21
  11. logger.info "target:" + project.targetCompatibility
  12. //21
  13. logger.info "source:" + project.sourceCompatibility
  14. //D:\work\ideaworkspace23\ds.dts.daes\build\resources\main
  15. logger.info "output.resourcesDir:" + sourceSets.main.output.resourcesDir
  16. //D:\work\ideaworkspace23\ds.dts.daes\build\classes\java\main
  17. logger.info "java.destinationDirectory:" + sourceSets.main.java.destinationDirectory.get()
  18. //D:\work\ideaworkspace24\gly-project-sample\gly-app-samplerole\build\classes\java\main;
  19. // D:\work\ideaworkspace24\gly-project-sample\gly-app-samplerole\build\resources\main
  20. //logger.info "classpath:"+sourceSets.main.runtimeClasspath.asPath
  21. // 在 build.gradle 文件中定义一个属性来保存增量编译后的类文件路径
  22. ext {
  23. lastCompileTime = System.currentTimeMillis()
  24. logger.info LocalDateTime.now().toString() + "上次编译的时间....... "
  25. incrementalClassesDir = project.sourceSets.main.output.classesDirs.singleFile
  26. }
  27. configurations {
  28. ajc
  29. aspects
  30. compile {
  31. extendsFrom aspects
  32. }
  33. }
  34. dependencies {
  35. implementation group: "org.aspectj", name: "aspectjrt", version: "${aspectjVersion}"
  36. implementation "org.aspectj:aspectjtools:${aspectjVersion}"//FIXME
  37. ajc "org.aspectj:aspectjtools:${aspectjVersion}"
  38. aspects "org.springframework:spring-aspects"
  39. }
  40. compileJava {
  41. LocalDateTime t1;
  42. doFirst {
  43. t1 = LocalDateTime.now();
  44. println "开始编译......"
  45. options.encoding = 'UTF-8'
  46. options.compilerArgs += [//FIXME
  47. // '-Xlint:deprecation',
  48. // '-Xlint:unchecked',
  49. '-parameters'
  50. ]
  51. }
  52. doLast {
  53. // 获取本次编译后的类文件目录
  54. File classesDir = project.sourceSets.main.output.classesDirs.singleFile
  55. // 获取上一次编译后的时间戳
  56. def lastCompileTime = project.ext.lastCompileTime
  57. // 遍历增量编译后的类文件目录,获取本次增量编译的类文件
  58. def incrementalClasses = fileTree(dir: classesDir, include: '**/*.class').files.findAll { file ->
  59. file.lastModified() > lastCompileTime
  60. }
  61. // 输出本次增量编译的类文件路径
  62. incrementalClasses.each { file ->
  63. def relativePath = incrementalClassesDir.toPath().relativize(file.toPath())
  64. println "需要增量编译的类: ${relativePath}"
  65. }
  66. // 更新上一次编译后的时间戳
  67. project.ext.lastCompileTime = System.currentTimeMillis()
  68. // 创建新的inpath目录,只包含incrementalClasses集合中的文件
  69. File preweaveInpathDir = new File(project.buildDir, "preweave")
  70. if (preweaveInpathDir.exists()) {
  71. def result = delete preweaveInpathDir
  72. println "清理目录${preweaveInpathDir}下的文件" + result ? "成功" : "失败"
  73. if (!result) {
  74. println "清理失败,请手动清理"
  75. }
  76. }
  77. preweaveInpathDir.mkdirs()
  78. // 复制增量编译的类文件到新目录,并保留原目录结构
  79. incrementalClasses.each { file ->
  80. def relativePath = incrementalClassesDir.toPath().relativize(file.toPath())
  81. def destFile = new File(preweaveInpathDir, relativePath.toString())
  82. println("复制文件到preweave目录: ${relativePath}")
  83. destFile.parentFile.mkdirs()
  84. Files.copy(file.toPath(), destFile.toPath(), StandardCopyOption.REPLACE_EXISTING)
  85. }
  86. println "开始aspectj织入....... "
  87. LocalDateTime t3 = LocalDateTime.now();
  88. ant.taskdef(resource: "org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties", classpath: configurations.ajc.asPath)
  89. ant.iajc(maxmem: "1024m", fork: "true", debug: "true", showweaveinfo: "true", Xlint: "-verbose",
  90. // inpath: project.sourceSets.main.output.classesDirs.singleFile.absolutePath,
  91. inpath: preweaveInpathDir,
  92. destDir: project.sourceSets.main.output.classesDirs.singleFile.absolutePath,
  93. aspectPath: configurations.aspects.asPath,
  94. classpath: project.sourceSets.main.runtimeClasspath.asPath,
  95. source: project.sourceCompatibility,
  96. target: project.targetCompatibility
  97. )
  98. def result = delete preweaveInpathDir
  99. println "清理目录${preweaveInpathDir}下的文件......"
  100. if (!result) {
  101. println "清理${preweaveInpathDir}下的文件失败,请手动清理!!!"
  102. }
  103. LocalDateTime t4 = LocalDateTime.now();
  104. println "完成编译和aspectj织入,编译耗时:" + ChronoUnit.MILLIS.between(t1, t3) + "ms" + ",织入耗时:" + ChronoUnit.MILLIS.between(t1, t4) + "ms";
  105. }
  106. }
  107. compileTestJava {
  108. doLast {
  109. logger.info "aspectj 开始织入...... "
  110. ant.taskdef(resource: "org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties", classpath: configurations.ajc.asPath)
  111. ant.iajc(maxmem: "1024m", fork: "true", debug: "true", showweaveinfo: "true", Xlint: "-verbose",
  112. destDir: project.sourceSets.test.output.classesDirs.singleFile.absolutePath,
  113. aspectPath: configurations.aspects.asPath,
  114. inpath: project.sourceSets.test.output.classesDirs.singleFile.absolutePath,
  115. classpath: project.sourceSets.test.runtimeClasspath.asPath,
  116. source: project.sourceCompatibility,
  117. target: project.targetCompatibility
  118. )
  119. }
  120. }
  121. project.configurations.all { configuration ->
  122. // logger.info ("****this configuration is ${configuration.name}")
  123. }
  124. logger.info "结束配置aspectj"