Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

2 месяцев назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import java.time.LocalDateTime
  2. import java.time.temporal.ChronoUnit
  3. import java.nio.file.Files
  4. import java.nio.file.StandardCopyOption
  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. sourceSets {
  28. main {
  29. // output.resourcesDir = file('build/preweave/resources/main')
  30. // java.destinationDirectory.set(file('build/preweave/java/main'))
  31. // output.resourcesDir = file('bin/resources/main')
  32. // java.destinationDirectory.set(file('bin/main'))
  33. }
  34. test {
  35. // output.resourcesDir = file('bin/test')
  36. // // Compiled Java classes should use this directory
  37. // java.destinationDirectory.set(file('bin/test'))
  38. }
  39. }
  40. configurations {
  41. ajc
  42. aspects
  43. compile {
  44. extendsFrom aspects
  45. }
  46. }
  47. dependencies {
  48. implementation group: "org.aspectj", name: "aspectjrt", version: "${aspectjVersion}"
  49. implementation "org.aspectj:aspectjtools:${aspectjVersion}"//FIXME
  50. ajc "org.aspectj:aspectjtools:${aspectjVersion}"
  51. aspects "org.springframework:spring-aspects"
  52. }
  53. compileJava {
  54. LocalDateTime t1;
  55. doFirst {
  56. t1 = LocalDateTime.now();
  57. println "开始编译......"
  58. options.encoding = 'UTF-8'
  59. options.compilerArgs += [//FIXME
  60. '-Xlint:deprecation',
  61. '-Xlint:unchecked',
  62. '-parameters'
  63. ]
  64. }
  65. doLast {
  66. // 获取本次编译后的类文件目录
  67. File classesDir = project.sourceSets.main.output.classesDirs.singleFile
  68. // 获取上一次编译后的时间戳
  69. def lastCompileTime = project.ext.lastCompileTime
  70. // 遍历增量编译后的类文件目录,获取本次增量编译的类文件
  71. def incrementalClasses = fileTree(dir: classesDir, include: '**/*.class').files.findAll { file ->
  72. file.lastModified() > lastCompileTime
  73. }
  74. // 输出本次增量编译的类文件路径
  75. incrementalClasses.each { file ->
  76. def relativePath = incrementalClassesDir.toPath().relativize(file.toPath())
  77. println "需要增量编译的类: ${relativePath}"
  78. }
  79. // 更新上一次编译后的时间戳
  80. project.ext.lastCompileTime = System.currentTimeMillis()
  81. // 创建新的inpath目录,只包含incrementalClasses集合中的文件
  82. File preweaveInpathDir = new File(project.buildDir, "preweave")
  83. if (preweaveInpathDir.exists()) {
  84. def result = delete preweaveInpathDir
  85. println "清理目录${preweaveInpathDir}下的文件" + result ? "成功" : "失败"
  86. if (!result) {
  87. println "清理失败,请手动清理"
  88. }
  89. }
  90. preweaveInpathDir.mkdirs()
  91. // 复制增量编译的类文件到新目录,并保留原目录结构
  92. incrementalClasses.each { file ->
  93. def relativePath = incrementalClassesDir.toPath().relativize(file.toPath())
  94. def destFile = new File(preweaveInpathDir, relativePath.toString())
  95. println("复制文件到preweave目录: ${relativePath}")
  96. destFile.parentFile.mkdirs()
  97. Files.copy(file.toPath(), destFile.toPath(), StandardCopyOption.REPLACE_EXISTING)
  98. }
  99. println "开始aspectj织入....... "
  100. LocalDateTime t3 = LocalDateTime.now();
  101. ant.taskdef(resource: "org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties", classpath: configurations.ajc.asPath)
  102. ant.iajc(maxmem: "1024m", fork: "true", debug: "true", showweaveinfo: "true", Xlint: "-verbose",
  103. // inpath: project.sourceSets.main.output.classesDirs.singleFile.absolutePath,
  104. inpath: preweaveInpathDir,
  105. destDir: project.sourceSets.main.output.classesDirs.singleFile.absolutePath,
  106. aspectPath: configurations.aspects.asPath,
  107. classpath: project.sourceSets.main.runtimeClasspath.asPath,
  108. source: project.sourceCompatibility,
  109. target: project.targetCompatibility
  110. )
  111. def result = delete preweaveInpathDir
  112. println "清理目录${preweaveInpathDir}下的文件......"
  113. if (!result) {
  114. println "清理${preweaveInpathDir}下的文件失败,请手动清理!!!"
  115. }
  116. LocalDateTime t4 = LocalDateTime.now();
  117. println "完成编译和aspectj织入,编译耗时:" + ChronoUnit.MILLIS.between(t1, t3) + "ms" + ",织入耗时:" + ChronoUnit.MILLIS.between(t1, t4) + "ms";
  118. }
  119. }
  120. compileTestJava {
  121. doLast {
  122. logger.info "aspectj 开始织入...... "
  123. ant.taskdef(resource: "org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties", classpath: configurations.ajc.asPath)
  124. ant.iajc(maxmem: "1024m", fork: "true", debug: "true", showweaveinfo: "true", Xlint: "-verbose",
  125. destDir: project.sourceSets.test.output.classesDirs.singleFile.absolutePath,
  126. aspectPath: configurations.aspects.asPath,
  127. inpath: project.sourceSets.test.output.classesDirs.singleFile.absolutePath,
  128. classpath: project.sourceSets.test.runtimeClasspath.asPath,
  129. source: project.sourceCompatibility,
  130. target: project.targetCompatibility
  131. )
  132. }
  133. }
  134. project.configurations.all { configuration ->
  135. // logger.info ("****this configuration is ${configuration.name}")
  136. }
  137. logger.info "结束配置aspectj"