import java.nio.file.Files import java.nio.file.StandardCopyOption import java.time.LocalDateTime import java.time.temporal.ChronoUnit logger.info "开始配置aspectj" //D:\work\ideaworkspace23\ds.dts.daes\build\classes\java\main logger.info "inpath:" + project.sourceSets.main.output.classesDirs.singleFile.absolutePath // logger.info "destDir:" + project.sourceSets.main.output.classesDirs.singleFile.absolutePath //21 logger.info "target:" + project.targetCompatibility //21 logger.info "source:" + project.sourceCompatibility //D:\work\ideaworkspace23\ds.dts.daes\build\resources\main logger.info "output.resourcesDir:" + sourceSets.main.output.resourcesDir //D:\work\ideaworkspace23\ds.dts.daes\build\classes\java\main logger.info "java.destinationDirectory:" + sourceSets.main.java.destinationDirectory.get() //D:\work\ideaworkspace24\gly-project-sample\gly-app-samplerole\build\classes\java\main; // D:\work\ideaworkspace24\gly-project-sample\gly-app-samplerole\build\resources\main //logger.info "classpath:"+sourceSets.main.runtimeClasspath.asPath // 在 build.gradle 文件中定义一个属性来保存增量编译后的类文件路径 ext { lastCompileTime = System.currentTimeMillis() logger.info LocalDateTime.now().toString() + "上次编译的时间....... " incrementalClassesDir = project.sourceSets.main.output.classesDirs.singleFile } configurations { ajc aspects compile { extendsFrom aspects } } dependencies { implementation group: "org.aspectj", name: "aspectjrt", version: "${aspectjVersion}" implementation "org.aspectj:aspectjtools:${aspectjVersion}"//FIXME ajc "org.aspectj:aspectjtools:${aspectjVersion}" aspects "org.springframework:spring-aspects" } compileJava { LocalDateTime t1; doFirst { t1 = LocalDateTime.now(); println "开始编译......" options.encoding = 'UTF-8' options.compilerArgs += [//FIXME // '-Xlint:deprecation', // '-Xlint:unchecked', '-parameters' ] } doLast { // 获取本次编译后的类文件目录 File classesDir = project.sourceSets.main.output.classesDirs.singleFile // 获取上一次编译后的时间戳 def lastCompileTime = project.ext.lastCompileTime // 遍历增量编译后的类文件目录,获取本次增量编译的类文件 def incrementalClasses = fileTree(dir: classesDir, include: '**/*.class').files.findAll { file -> file.lastModified() > lastCompileTime } // 输出本次增量编译的类文件路径 incrementalClasses.each { file -> def relativePath = incrementalClassesDir.toPath().relativize(file.toPath()) println "需要增量编译的类: ${relativePath}" } // 更新上一次编译后的时间戳 project.ext.lastCompileTime = System.currentTimeMillis() // 创建新的inpath目录,只包含incrementalClasses集合中的文件 File preweaveInpathDir = new File(project.buildDir, "preweave") if (preweaveInpathDir.exists()) { def result = delete preweaveInpathDir println "清理目录${preweaveInpathDir}下的文件" + result ? "成功" : "失败" if (!result) { println "清理失败,请手动清理" } } preweaveInpathDir.mkdirs() // 复制增量编译的类文件到新目录,并保留原目录结构 incrementalClasses.each { file -> def relativePath = incrementalClassesDir.toPath().relativize(file.toPath()) def destFile = new File(preweaveInpathDir, relativePath.toString()) println("复制文件到preweave目录: ${relativePath}") destFile.parentFile.mkdirs() Files.copy(file.toPath(), destFile.toPath(), StandardCopyOption.REPLACE_EXISTING) } println "开始aspectj织入....... " LocalDateTime t3 = LocalDateTime.now(); ant.taskdef(resource: "org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties", classpath: configurations.ajc.asPath) ant.iajc(maxmem: "1024m", fork: "true", debug: "true", showweaveinfo: "true", Xlint: "-verbose", // inpath: project.sourceSets.main.output.classesDirs.singleFile.absolutePath, inpath: preweaveInpathDir, destDir: project.sourceSets.main.output.classesDirs.singleFile.absolutePath, aspectPath: configurations.aspects.asPath, classpath: project.sourceSets.main.runtimeClasspath.asPath, source: project.sourceCompatibility, target: project.targetCompatibility ) def result = delete preweaveInpathDir println "清理目录${preweaveInpathDir}下的文件......" if (!result) { println "清理${preweaveInpathDir}下的文件失败,请手动清理!!!" } LocalDateTime t4 = LocalDateTime.now(); println "完成编译和aspectj织入,编译耗时:" + ChronoUnit.MILLIS.between(t1, t3) + "ms" + ",织入耗时:" + ChronoUnit.MILLIS.between(t1, t4) + "ms"; } } compileTestJava { doLast { logger.info "aspectj 开始织入...... " ant.taskdef(resource: "org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties", classpath: configurations.ajc.asPath) ant.iajc(maxmem: "1024m", fork: "true", debug: "true", showweaveinfo: "true", Xlint: "-verbose", destDir: project.sourceSets.test.output.classesDirs.singleFile.absolutePath, aspectPath: configurations.aspects.asPath, inpath: project.sourceSets.test.output.classesDirs.singleFile.absolutePath, classpath: project.sourceSets.test.runtimeClasspath.asPath, source: project.sourceCompatibility, target: project.targetCompatibility ) } } project.configurations.all { configuration -> // logger.info ("****this configuration is ${configuration.name}") } logger.info "结束配置aspectj"