Skip to content

Commit

Permalink
#2 Added a more sophisticated example where aspect classes are spread…
Browse files Browse the repository at this point in the history
… over multiple Gradle projects
  • Loading branch information
sedovalx committed Jan 14, 2020
1 parent f7b62e1 commit 495ce94
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 62 deletions.
28 changes: 20 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ doing the weaving on the weaving over already compiled classes. It is particular
}
apply plugin: 'com.github.sedovalx.gradle-aspectj-binary'
`weaveClasses` task becomes available after that. It makes sense to add it as a dependency for the `classes` task
`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
Expand All @@ -37,19 +38,30 @@ doing the weaving on the weaving over already compiled classes. It is particular

> 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.
process of both projects. See the `examples` project for details.

### Available configuration

For now the `weaceClasses` task can be configured with the next parameters
The plugins can be configured with the following parameters

weaveClasses {
source = '1.7'
target = '1.7'
writeToLog = true
aspectjBinary {
applyJavaPlugin = true
weaveClasses {
ajcSourceSets = [project.sourceSets.main]
outputDir = project.file(...)
source = '1.7'
target = '1.7'
writeToLog = true
}
}
Parameters:
- `applyJavaPlugin` should the `java` plugin be applied with the `aspectjBinary` plugin. By default, it is applied but
in some cases, for example Android projects, it is not desired. WARN: in this case, you **must** provide the `ajcSourceSets`
and the `outputDir` property values by yourself as there is no default configuration for Android projects (any help is appreciated).
- `ajcSourceSets` is a set of Gradle source sets to be weaved. By default, it includes the `main` source set only.
Both `compileClasspath` and `runtimeClasspath` collections are extracted from each source set.
- `outputDir` is a folder where the weaved classes are copied. By default, it is `build/classes/java/main`.
- `source` has '1.7' as default value and is passed as it is to the [ajc](http://www.eclipse.org/aspectj/doc/next/devguide/ajc-ref.html) compiler parameter
- `target` has '1.7' as default value and is passed as it is to the [ajc](http://www.eclipse.org/aspectj/doc/next/devguide/ajc-ref.html) compiler parameter
- `writeToLog` has `false` as a default value and defines if ajc's compile messages should
Expand Down
1 change: 0 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ buildscript {
}

repositories {
mavenLocal()
mavenCentral()
jcenter()
}
Expand Down
15 changes: 6 additions & 9 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ It expects `1.0-SNAPSHOT` version of the plugin in the local Maven repository or

Run

./gradlew clean run
./gradlew clean :examples:app:run

to see the result. Probably you will see something similar to this:

Expand All @@ -21,13 +21,13 @@ to see the result. Probably you will see something similar to this:
Hello Kotlin

Jcabi aspect:
[main] WARN com.github.sedovalx.sandbox.gradle.aspectj.example.App - java.lang.RuntimeException: I'm very quiet
at com.github.sedovalx.sandbox.gradle.aspectj.example.App.jcabiExample_aroundBody2(App.java:24)
at com.github.sedovalx.sandbox.gradle.aspectj.example.App$AjcClosure3.run(App.java:1)
[main] WARN com.github.sedovalx.sandbox.gradle.aspectj.examples.aspects.App - java.lang.RuntimeException: I'm very quiet
at com.github.sedovalx.sandbox.gradle.aspectj.examples.aspects.App.jcabiExample_aroundBody2(App.java:24)
at com.github.sedovalx.sandbox.gradle.aspectj.examples.aspects.App$AjcClosure3.run(App.java:1)
at org.aspectj.runtime.reflect.JoinPointImpl.proceed(JoinPointImpl.java:149)
at com.jcabi.aspects.aj.QuietExceptionsLogger.wrap(QuietExceptionsLogger.java:83)
at com.github.sedovalx.sandbox.gradle.aspectj.example.App.jcabiExample(App.java:24)
at com.github.sedovalx.sandbox.gradle.aspectj.example.App.main(App.java:37)
at com.github.sedovalx.sandbox.gradle.aspectj.examples.aspects.App.jcabiExample(App.java:24)
at com.github.sedovalx.sandbox.gradle.aspectj.examples.aspects.App.main(App.java:37)

> You have to use AspectJ annotations to describe aspects.
Expand All @@ -43,8 +43,5 @@ You don't need all this tricky things in your application. Just add this to the
}

apply plugin: 'com.github.sedovalx.gradle-aspectj-binary'
weaveClasses.dependsOn compileJava
classes.dependsOn weaveClasses
to the project you want to weave during the build.
29 changes: 29 additions & 0 deletions examples/app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
buildscript {
repositories {
mavenLocal()
jcenter()
}

dependencies {
classpath "com.github.sedovalx.gradle:gradle-aspectj-binary:$pluginVersion"
}
}

dependencies {
compile project(":examples:aspects")
}

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

mainClassName = 'com.github.sedovalx.sandbox.gradle.aspectj.examples.aspects.App'

aspectjBinary {
applyJavaPlugin = true
weaveClasses {
source = "1.7"
target = "1.7"
ajcSourceSets = [project.sourceSets.main]
writeToLog = true
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.github.sedovalx.sandbox.gradle.aspectj.example;
package com.github.sedovalx.sandbox.gradle.aspectj.examples.aspects;

import com.github.sedovalx.sandbox.gradle.aspectj.example.aspects.JavaAnnotation;
import com.github.sedovalx.sandbox.gradle.aspectj.example.aspects.KotlinAnnotation;
import com.jcabi.aspects.Quietly;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package com.github.sedovalx.sandbox.gradle.aspectj.example.aspects
package com.github.sedovalx.sandbox.gradle.aspectj.examples.aspects

@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.PROPERTY_SETTER)
annotation class KotlinAnnotation(val value: String)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.sedovalx.sandbox.gradle.aspectj.example.aspects
package com.github.sedovalx.sandbox.gradle.aspectj.examples.aspects

import org.aspectj.lang.ProceedingJoinPoint
import org.aspectj.lang.annotation.Around
Expand Down
19 changes: 19 additions & 0 deletions examples/aspects/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
buildscript {
repositories {
mavenLocal()
jcenter()
}

dependencies {
classpath "com.github.sedovalx.gradle:gradle-aspectj-binary:$pluginVersion"
}
}

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

dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
compile "org.aspectj:aspectjrt:$aspectjVersion"
compile 'com.jcabi:jcabi-aspects:0.22.6'
compile 'org.slf4j:slf4j-simple:1.7.21'
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.sedovalx.sandbox.gradle.aspectj.example.aspects;
package com.github.sedovalx.sandbox.gradle.aspectj.examples.aspects;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.sedovalx.sandbox.gradle.aspectj.example.aspects;
package com.github.sedovalx.sandbox.gradle.aspectj.examples.aspects;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
Expand Down
37 changes: 1 addition & 36 deletions examples/build.gradle
Original file line number Diff line number Diff line change
@@ -1,37 +1,2 @@
buildscript {
repositories {
mavenLocal()
mavenCentral()
jcenter()
}

dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "com.github.sedovalx.gradle:gradle-aspectj-binary:$pluginVersion"
}
}

group 'com.github.sedovalx.sandbox.gradle.aspectj.example'
version '1.0-SNAPSHOT'

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

aspectjBinary {
applyJavaPlugin = true
weaveClasses {
source = "1.8"
target = "1.8"
ajcSourceSets = [project.sourceSets.main]
writeToLog = true
}
}

mainClassName = "com.github.sedovalx.sandbox.gradle.aspectj.example.App"

dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
compile "org.aspectj:aspectjrt:$aspectjVersion"
compile 'com.jcabi:jcabi-aspects:0.22.6'
compile 'org.slf4j:slf4j-simple:1.7.21'
}
version '1.0-SNAPSHOT'
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1 +1 @@
include 'plugin', 'examples'
include 'plugin', 'examples', 'examples:aspects', 'examples:app'

0 comments on commit 495ce94

Please sign in to comment.