sedovalx / gradle-aspectj-binary

Gradle plugin for AspectJ binary weaving
16 stars 6 forks source link
aspectj gradle gradle-plugin kotlin

gradle-aspectj-binary

Disclamer: I don't use this plugin in my projects anymore and currently don't plan to support it in foreseeable future. In general, it works well but a bit outdated (Gradle, AspectJ versions). Feel free to fork it and adopt for your needs.

Build Status Download

Gradle plugin for AspectJ binary weaving. It works similar to https://github.com/jcabi/jcabi-maven-plugin doing the weaving on the weaving over already compiled classes. It is particularly helpful if the source code is written in a language different from Java. For example, Kotlin, as it produces fully compatible Java bytecode. It goes without saying that the plugin works for Java sources as well.

Requirements

Usage

Here is an example project in the examples folder. Basically you need to add the plugin to your build script

  buildscript {
    repositories {
      jcenter()
    }
    dependencies {
      classpath "com.github.sedovalx.gradle:gradle-aspectj-binary:$pluginVersion"
    }
  }

  apply plugin: 'com.github.sedovalx.gradle-aspectj-binary'

weaveClasses task becomes available after that. By default, if the java plugin is not disabled via configuration (see below), there are pre-configured tasks dependencies:

  weaveClasses.dependsOn compileJava
  classes.dependsOn weaveClasses

so you can just run the build and have all your aspects in the main source set applied.

You need to weave both aspects and classes where aspects should be applied. So if you have aspect classes in a project A and classes to be weaved in a project B you should add the weaveClasses task to the build process of both projects. See the examples project for details.

Available configuration

The plugins can be configured with the following parameters

  aspectjBinary {
    applyJavaPlugin = true
    weaveClasses {
      ajcSourceSets = [project.sourceSets.main]
      outputDir = project.file(...)
      source = '1.7'  
      target = '1.7'
      additionalAjcParams = ['-proceedOnError']  
      writeToLog = true
    } 
  }

Parameters:

Clean local build

The examples project depends on a version of the plugin. In case of a clean build no plugin version exists in the repository. So I use a little bit hacky way to do the trick.

 $ echo "include 'plugin'" > settings.gradle
 $ ./gradlew clean :plugin:publishMavenJavaPublicationToMavenLocal
 $ echo "include 'plugin', 'examples', 'examples:aspects', 'examples:app'" > settings.gradle
 $ ./gradlew :examples:app:run