AspectJPlugin.groovy 3.9 KB

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