AspectJPlugin.groovy 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package aspectj
  2. import org.gradle.api.Project
  3. import org.gradle.api.Plugin
  4. import org.gradle.api.tasks.TaskAction
  5. import org.gradle.api.logging.LogLevel
  6. import org.gradle.api.file.FileCollection
  7. import org.gradle.api.tasks.SourceSet
  8. import org.gradle.api.DefaultTask
  9. import org.gradle.api.GradleException
  10. import org.gradle.plugins.eclipse.EclipseClasspath
  11. import org.gradle.plugins.eclipse.EclipseProject
  12. import org.gradle.plugins.eclipse.model.ProjectDependency
  13. /**
  14. *
  15. * @author Luke Taylor
  16. */
  17. class AspectJPlugin implements Plugin<Project> {
  18. void apply(Project project) {
  19. if (!project.hasProperty('aspectjVersion')) {
  20. throw new GradleException("You must set the property 'aspectjVersion' before applying the aspectj plugin")
  21. }
  22. if (project.configurations.findByName('ajtools') == null) {
  23. project.configurations.add('ajtools')
  24. project.dependencies {
  25. ajtools "org.aspectj:aspectjtools:${project.aspectjVersion}"
  26. compile "org.aspectj:aspectjrt:${project.aspectjVersion}"
  27. }
  28. }
  29. if (project.configurations.findByName('aspectpath') == null) {
  30. project.configurations.add('aspectpath')
  31. }
  32. project.tasks.add(name: 'compileJava', overwrite: true, description: 'Compiles AspectJ Source', type: Ajc) {
  33. dependsOn project.processResources
  34. sourceSet = project.sourceSets.main
  35. inputs.files(sourceSet.java.srcDirs)
  36. outputs.dir(sourceSet.classesDir)
  37. aspectPath = project.configurations.aspectpath
  38. }
  39. project.tasks.add(name: 'compileTestJava', overwrite: true, description: 'Compiles AspectJ Test Source', type: Ajc) {
  40. dependsOn project.processTestResources, project.compileJava, project.jar
  41. sourceSet = project.sourceSets.test
  42. inputs.files(sourceSet.java.srcDirs)
  43. outputs.dir(sourceSet.classesDir)
  44. aspectPath = project.files(project.configurations.aspectpath, project.jar.archivePath)
  45. }
  46. project.tasks.withType(EclipseProject).all {
  47. whenConfigured { p ->
  48. p.natures.add(0, 'org.eclipse.ajdt.ui.ajnature')
  49. p.buildCommands = [new org.gradle.plugins.eclipse.model.BuildCommand('org.eclipse.ajdt.core.ajbuilder',[:])]
  50. }
  51. }
  52. project.tasks.withType(EclipseClasspath).all {
  53. whenConfigured { classpath ->
  54. def entries = classpath.entries.findAll { it instanceof ProjectDependency}.findAll { entry ->
  55. def projectPath = entry.path.replaceAll('/',':')
  56. project.rootProject.findProject(projectPath).plugins.findPlugin(AspectJPlugin)
  57. }
  58. entries.each { entry->
  59. entry.entryAttributes.put('org.eclipse.ajdt.aspectpath','org.eclipse.ajdt.aspectpath')
  60. }
  61. }
  62. }
  63. }
  64. }
  65. class Ajc extends DefaultTask {
  66. SourceSet sourceSet
  67. FileCollection aspectPath
  68. Ajc() {
  69. logging.captureStandardOutput(LogLevel.INFO)
  70. }
  71. @TaskAction
  72. def compile() {
  73. logger.info("Running ajc ...")
  74. ant.taskdef(resource: "org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties", classpath: project.configurations.ajtools.asPath)
  75. ant.iajc(classpath: sourceSet.compileClasspath.asPath, fork: 'true', destDir: sourceSet.classesDir.absolutePath,
  76. source: project.convention.plugins.java.sourceCompatibility,
  77. target: project.convention.plugins.java.targetCompatibility,
  78. aspectPath: aspectPath.asPath, sourceRootCopyFilter: '**/*.java', showWeaveInfo: 'true') {
  79. sourceroots {
  80. sourceSet.java.srcDirs.each {
  81. pathelement(location: it.absolutePath)
  82. }
  83. }
  84. }
  85. }
  86. }