AspectJPlugin.groovy 4.2 KB

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