AspectJPlugin.groovy 4.2 KB

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