Closed ansonliao closed 5 years ago
We have some example projects here: https://github.com/freefair/gradle-plugins/tree/master/examples/aspectj
If the example projects don't work for you, just tell me a bit more about what you want to do with AspectJ in your project.
@larsgrefer Let me try, thanks for your reply
Hi @larsgrefer , I clone the whole project of the repository, and when I want to run the example AspectJ plugin project, the task myCustomWeaving
gradle clean myCustomWeaving
Starting a Gradle Daemon (subsequent builds will be faster)
> Configure project :gradle-plugins
Using git branch as version: master-SNAPSHOT
> Task :aspectj:myCustomWeaving FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Some problems were found with the configuration of task ':aspectj:myCustomWeaving'.
> No value has been specified for property 'classpath'.
> No value has been specified for property 'destinationDir'.
> No value has been specified for property 'sourceCompatibility'.
> No value has been specified for property 'targetCompatibility'.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 30s
7 actionable tasks: 1 executed, 6 up-to-date
please advise.
@larsgrefer
Actually, my requirement is simple,
For my Gradle testing project without any modules, so the code is placed in the directory src/main/java
, and testing code is placed src/test/java
,
the Gradle project only one build.gradle
file
and my main codes also simple:
src/main/java/com/example/AspectClass.java
package com.example;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class AspectClass {
@Pointcut("within(com.example.MyApp)")
public void pointCut() {
}
public void aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("Before point cut...");
joinPoint.proceed();
System.out.println("After point cut.");
}
}
src/main/java/com/example/MyApp.java
package com.example;
import org.testng.annotations.Test;
public class MyApp {
@Test
public static void sayHi() {
System.out.println("Hello World!");
}
public static void main( String[] args ) {
sayHi();
}
}
so for this case, how should I configure the freefair AspectJ Gradle Plugin
in build.gradle
to make the AspectJ works when run MyApp.java
main method or @Test
method?
When my Gradle testing project includes modules, let's say one module, and the module name is freefair-aspectj
, and then placed the codes in the module freefair-aspectj
,
now two build.gradle
files, one is the root directory of the project build.gradle
, and another one is freefair-aspectj/build.gradle
.
and the codes:
freefair-aspectj/src/main/java/com/example/AspectClass.java
package com.example;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class AspectClass {
@Pointcut("within(com.example.MyApp)")
public void pointCut() {
}
public void aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("Before point cut...");
joinPoint.proceed();
System.out.println("After point cut.");
}
}
freefair-aspectj/src/main/java/com/example/MyApp.java
package com.example;
import org.testng.annotations.Test;
public class MyApp {
@Test
public static void sayHi() {
System.out.println("Hello World!");
}
public static void main( String[] args ) {
sayHi();
}
}
so for this case, how should I configure the build.gradle
files that build.gradle
and freefair-aspectj/build.gradle
to make the AspectJ works when run MyApp.java
main method for @Test
method?
Thanks for your great work on my question, @larsgrefer and everyone.
You have two options:
io.freefair.aspectj
plugin. This plugins uses ajc to perform the *.java
-to-*.class
compilation. By default this plugin compiles the sources from src/main/aspectj
(and src/test/aspectj
). If you want to keep your code in src/main/java
(and src/test/java
) you can configure this accordingly:sourceSets.main.aspectj.srcDir "src/main/java"
sourceSets.main.java.srcDirs = files()
sourceSets.test.aspectj.srcDir "src/test/java"
sourceSets.test.java.srcDirs = files()
io.freefair.aspectj.post-compile-weaving
plugin. This plugin leaves the actual compilation to javac and performs the weaving as *.class
-to-*.class
transformation. In this case you don't have to change any paths.For both cases the configuration has to be done in the build.gradle
file of the module where the sources are. (So in freefair-aspectj/build.gradle
for your second case)
@larsgrefer
For my case 1, I updated my build.gradle
and got some error message of build.gradle
build, could you help to fix my build.gradle
for works? thanks
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "io.freefair.gradle:aspectj-plugin:3.8.4"
}
}
apply plugin: "io.freefair.aspectj"
group 'com.github.ansonliao'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
targetCompatibility = 1.8
sourceSets.main.aspectj.srcDir 'src/main/java'
sourceSets.test.aspectj.srcDir 'src/main/test'
sourceSets.main.java.srcDirs = files()
sourceSets.test.java.srcDirs = files()
[compileJava, compileTestJava]*.options.collect { options ->
options.encoding = 'UTF-8'
}
allprojects {
repositories {
jcenter()
}
}
dependencies {
testCompile group: 'org.testng', name: 'testng', version: '6.14.3'
}
aspectj {
version = '1.9.4'
}
Errors:
Could not find any public constructor for class io.freefair.gradle.plugins.aspectj.AspectJExtension_Decorated which accepts parameters [].
Open File
Which gradle version are you using?
@larsgrefer
My gradle version is 5.5.1
.
I simplified the build.gradle
with introducing freefair's aspectj
from the guideline:https://docs.gradle.org/current/userguide/plugins.html#sec:subprojects_plugins_dsl
and now my build.gradle
as below:
plugins {
id 'java'
id "io.freefair.aspectj" version "3.8.4"
}
group 'com.ansonliao'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
// https://mvnrepository.com/artifact/org.testng/testng
testCompile group: 'org.testng', name: 'testng', version: '6.14.3'
}
aspectj {
version = "1.9.4"
}
finally, I got the error:
org.gradle.api.model.ObjectFactory.fileCollection()Lorg/gradle/api/file/ConfigurableFileCollection;
Please advise.
The buildscript looks fine. Could you please post the full error? (run the command with --info --stacktrace
)
@larsgrefer
I created the new gradle project with freefair aspectj plugin
, the build.gradle
as below, and looks like no error for build the build.gradle
now, still not find the root cause.
plugins {
id 'java'
id "io.freefair.aspectj" version "3.8.4"
}
group 'com.ansonliao'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
targetCompatibility = 1.8
sourceSets.main.aspectj.srcDir "src/main/java"
sourceSets.main.java.srcDirs = files()
sourceSets.test.aspectj.srcDir "src/test/java"
sourceSets.test.java.srcDirs = files()
repositories {
mavenCentral()
}
dependencies {
// https://mvnrepository.com/artifact/org.testng/testng
compile group: 'org.testng', name: 'testng', version: '6.14.3'
// https://mvnrepository.com/artifact/org.aspectj/aspectjweaver
// compile group: 'org.aspectj', name: 'aspectjweaver', version: '1.9.4'
// https://mvnrepository.com/artifact/org.aspectj/aspectjrt
compile group: 'org.aspectj', name: 'aspectjrt', version: '1.9.4'
}
aspectj {
version = "1.9.4"
}
and now, the main method of src/main/java/com/example/MyApp.java
run can attached the aspecjt task of freefair
run, so the point cut of aspectj is works now.
but I meet another problem, when I created a TestNG class in src/test/java/com/example/MyTest.java
, and the code same as src/main/java/com/example/MyApp.java
, please check below:
package com.example
import org.testng.annotations.Test;
public class MyTest {
@Test
public void f1() {
MyApp myApp = new MyApp();
myApp.sayHi();
}
public static void main( String[] args ) {
MyApp myApp = new MyApp();
myApp.sayHi();
}
}
when I run the TesNG test method MyTest::f1()
, and got the error as below:
gradle clean test --info --stacktrace
Initialized native services in: /Users/anson.liao/.gradle/native
The client will now receive all logging from the daemon (pid: 29057). The daemon log file: /Users/anson.liao/.gradle/daemon/5.5.1/daemon-29057.out.log
Starting 5th build in daemon [uptime: 34 mins 51.603 secs, performance: 100%, non-heap usage: 22% of 268.4 MB]
Using 8 worker leases.
Invalidating in-memory cache of /Users/anson.liao/repo/anson/Gradle-AspectJ-Sample/.gradle/5.5.1/fileHashes/fileHashes.bin
Starting Build
Settings evaluated using settings file '/Users/anson.liao/repo/anson/Gradle-AspectJ-Sample/settings.gradle'.
Projects loaded. Root project using build file '/Users/anson.liao/repo/anson/Gradle-AspectJ-Sample/build.gradle'.
Included projects: [root project 'Gradle-AspectJ-Sample']
> Configure project :
Evaluating root project 'Gradle-AspectJ-Sample' using build file '/Users/anson.liao/repo/anson/Gradle-AspectJ-Sample/build.gradle'.
Invalidating in-memory cache of /Users/anson.liao/repo/anson/Gradle-AspectJ-Sample/.gradle/5.5.1/fileHashes/resourceHashesCache.bin
Invalidating in-memory cache of /Users/anson.liao/repo/anson/Gradle-AspectJ-Sample/.gradle/buildOutputCleanup/outputFiles.bin
All projects evaluated.
Selected primary task 'clean' from project :
Selected primary task 'test' from project :
Tasks to be executed: [task ':clean', task ':compileJava', task ':compileAspectj', task ':processResources', task ':classes', task ':compileTestJava', task ':compileTestAspectj', task ':processTestResources', task ':testClasses', task ':test']
:clean (Thread[Execution worker for ':',5,main]) started.
Invalidating in-memory cache of /Users/anson.liao/repo/anson/Gradle-AspectJ-Sample/.gradle/5.5.1/executionHistory/executionHistory.bin
> Task :clean
Caching disabled for task ':clean' because:
Build cache is disabled
Task ':clean' is not up-to-date because:
Task has not declared any outputs despite executing actions.
:clean (Thread[Execution worker for ':',5,main]) completed. Took 0.165 secs.
:compileJava (Thread[Execution worker for ':',5,main]) started.
> Task :compileJava NO-SOURCE
Skipping task ':compileJava' as it has no source files and no previous output files.
:compileJava (Thread[Execution worker for ':',5,main]) completed. Took 0.004 secs.
:compileAspectj (Thread[Execution worker for ':',5,main]) started.
> Task :compileAspectj
file or directory '/Users/anson.liao/repo/anson/Gradle-AspectJ-Sample/src/main/aspectj', not found
Caching disabled for task ':compileAspectj' because:
Build cache is disabled
Task ':compileAspectj' is not up-to-date because:
Output property 'destinationDir' file /Users/anson.liao/repo/anson/Gradle-AspectJ-Sample/build/classes/aspectj/main has been removed.
Output property 'destinationDir' file /Users/anson.liao/repo/anson/Gradle-AspectJ-Sample/build/classes/aspectj/main/example has been removed.
Output property 'destinationDir' file /Users/anson.liao/repo/anson/Gradle-AspectJ-Sample/build/classes/aspectj/main/example/AspectClass.class has been removed.
file or directory '/Users/anson.liao/repo/anson/Gradle-AspectJ-Sample/src/main/aspectj', not found
Starting process 'command '/Library/Java/JavaVirtualMachines/jdk11.0.2.jdk/Contents/Home/bin/java''. Working directory: /Users/anson.liao/repo/anson/Gradle-AspectJ-Sample Command: /Library/Java/JavaVirtualMachines/jdk11.0.2.jdk/Contents/Home/bin/java -Dfile.encoding=UTF-8 -Duser.country=HK -Duser.language=en -Duser.variant -cp /Users/anson.liao/.gradle/caches/modules-2/files-2.1/org.aspectj/aspectjtools/1.9.4/b907e51a8a6820926785b8933be3de4a021da90b/aspectjtools-1.9.4.jar org.aspectj.tools.ajc.Main -argfile /Users/anson.liao/repo/anson/Gradle-AspectJ-Sample/build/tmp/compileAspectj/ajc.options
Successfully started process 'command '/Library/Java/JavaVirtualMachines/jdk11.0.2.jdk/Contents/Home/bin/java''
:compileAspectj (Thread[Execution worker for ':',5,main]) completed. Took 2.703 secs.
:processResources (Thread[Execution worker for ':',5,main]) started.
> Task :processResources NO-SOURCE
Skipping task ':processResources' as it has no source files and no previous output files.
:processResources (Thread[Execution worker for ':',5,main]) completed. Took 0.0 secs.
:classes (Thread[Execution worker for ':',5,main]) started.
> Task :classes
Skipping task ':classes' as it has no actions.
:classes (Thread[Execution worker for ':',5,main]) completed. Took 0.0 secs.
:compileTestJava (Thread[Execution worker for ':',5,main]) started.
> Task :compileTestJava NO-SOURCE
Skipping task ':compileTestJava' as it has no source files and no previous output files.
:compileTestJava (Thread[Execution worker for ':',5,main]) completed. Took 0.0 secs.
:compileTestAspectj (Thread[Execution worker for ':',5,main]) started.
> Task :compileTestAspectj
file or directory '/Users/anson.liao/repo/anson/Gradle-AspectJ-Sample/src/test/aspectj', not found
Caching disabled for task ':compileTestAspectj' because:
Build cache is disabled
Task ':compileTestAspectj' is not up-to-date because:
Output property 'destinationDir' file /Users/anson.liao/repo/anson/Gradle-AspectJ-Sample/build/classes/aspectj/test has been removed.
Output property 'destinationDir' file /Users/anson.liao/repo/anson/Gradle-AspectJ-Sample/build/classes/aspectj/test/example has been removed.
Output property 'destinationDir' file /Users/anson.liao/repo/anson/Gradle-AspectJ-Sample/build/classes/aspectj/test/example/MyTest.class has been removed.
file or directory '/Users/anson.liao/repo/anson/Gradle-AspectJ-Sample/src/test/aspectj', not found
Starting process 'command '/Library/Java/JavaVirtualMachines/jdk11.0.2.jdk/Contents/Home/bin/java''. Working directory: /Users/anson.liao/repo/anson/Gradle-AspectJ-Sample Command: /Library/Java/JavaVirtualMachines/jdk11.0.2.jdk/Contents/Home/bin/java -Dfile.encoding=UTF-8 -Duser.country=HK -Duser.language=en -Duser.variant -cp /Users/anson.liao/.gradle/caches/modules-2/files-2.1/org.aspectj/aspectjtools/1.9.4/b907e51a8a6820926785b8933be3de4a021da90b/aspectjtools-1.9.4.jar org.aspectj.tools.ajc.Main -argfile /Users/anson.liao/repo/anson/Gradle-AspectJ-Sample/build/tmp/compileTestAspectj/ajc.options
Successfully started process 'command '/Library/Java/JavaVirtualMachines/jdk11.0.2.jdk/Contents/Home/bin/java''
:compileTestAspectj (Thread[Execution worker for ':',5,main]) completed. Took 1.567 secs.
:processTestResources (Thread[Execution worker for ':',5,main]) started.
> Task :processTestResources NO-SOURCE
Skipping task ':processTestResources' as it has no source files and no previous output files.
:processTestResources (Thread[Execution worker for ':',5,main]) completed. Took 0.002 secs.
:testClasses (Thread[Execution worker for ':',5,main]) started.
> Task :testClasses
Skipping task ':testClasses' as it has no actions.
:testClasses (Thread[Execution worker for ':',5,main]) completed. Took 0.0 secs.
:test (Thread[Execution worker for ':',5,main]) started.
> Task :test
Caching disabled for task ':test' because:
Build cache is disabled
Task ':test' is not up-to-date because:
Task has failed previously.
Finished generating test XML results (0.0 secs) into: /Users/anson.liao/repo/anson/Gradle-AspectJ-Sample/build/test-results/test
Generating HTML test report...
Finished generating test html results (0.002 secs) into: /Users/anson.liao/repo/anson/Gradle-AspectJ-Sample/build/reports/tests/test
:test (Thread[Execution worker for ':',5,main]) completed. Took 0.03 secs.
BUILD SUCCESSFUL in 5s
4 actionable tasks: 4 executed
looks like the setting of freefair aspectj plugin
's doesn't work:
sourceSets.main.aspectj.srcDir "src/main/java"
sourceSets.main.java.srcDirs = files()
sourceSets.test.aspectj.srcDir "src/test/java"
sourceSets.test.java.srcDirs = files()
I no idea what is the correct place to add these setting that change the aspectj main dir and test dir, could you help advise this issue?
when I run the TesNG test method
MyTest::f1()
, and got the error as below:
What error? the build log you posted is successful.
You can have a look at /Users/anson.liao/repo/anson/Gradle-AspectJ-Sample/build/tmp/compileTestAspectj/ajc.options
to see what exactly was passed to ajc
@larsgrefer
Checked the ajc.options
, and the output is:
-classpath
/Users/anson.liao/repo/anson/Gradle-AspectJ-Sample/build/classes/aspectj/main:/Users/anson.liao/.gradle/caches/modules-2/files-2.1/org.testng/testng/6.14.3/d24515dc253e77e54b73df97e1fb2eb7faf34fdd/testng-6.14.3.jar:/Users/anson.liao/.gradle/caches/modules-2/files-2.1/org.aspectj/aspectjrt/1.9.4/7efb30f3259d13472ee0f92bef4319fda700f522/aspectjrt-1.9.4.jar:/Users/anson.liao/.gradle/caches/modules-2/files-2.1/com.beust/jcommander/1.72/6375e521c1e11d6563d4f25a07ce124ccf8cd171/jcommander-1.72.jar:/Users/anson.liao/.gradle/caches/modules-2/files-2.1/org.apache-extras.beanshell/bsh/2.0b6/fb418f9b33a0b951e9a2978b4b6ee93b2707e72f/bsh-2.0b6.jar
-d
/Users/anson.liao/repo/anson/Gradle-AspectJ-Sample/build/classes/aspectj/test
-target
1.8
-source
1.8
/Users/anson.liao/repo/anson/Gradle-AspectJ-Sample/src/test/java/example/MyTest.java
looks like no error here
I noticed the output of the command gradle clean test --info --stacktrace
, some snippets of the message as below:
> Task :compileAspectj
file or directory '/Users/anson.liao/repo/anson/Gradle-AspectJ-Sample/src/main/aspectj', not found
> Task :compileTestJava NO-SOURCE
Skipping task ':compileTestJava' as it has no source files and no previous output files.
:compileTestJava (Thread[Execution worker for ':' Thread 2,5,main]) completed. Took 0.001 secs.
:compileTestAspectj (Thread[Execution worker for ':' Thread 2,5,main]) started.
> Task :compileTestAspectj
file or directory '/Users/anson.liao/repo/anson/Gradle-AspectJ-Sample/src/test/aspectj', not found
Caching disabled for task ':compileTestAspectj' because:
Build cache is disabled
from the outputs above, looks like the aspectj plugin is looking the directory src/main/aspectj
and src/test/aspectj
,
but I added the setting per your advise before:
sourceSets.main.aspectj.srcDir "src/main/java"
sourceSets.main.java.srcDirs = files()
sourceSets.test.aspectj.srcDir "src/test/java"
sourceSets.test.java.srcDirs = files()
@larsgrefer Hi Lars, I no idea what the problem that the AspectJ plugin doesn't work now after my intellij reinstalled, I uploaded my simply repository to Github, could you kindly help me to fix the problem?
The repository is: https://github.com/ansonliao/Gradle-AspectJ-Sample
My Intellij Mac version is 2019.2
My Gradle version is 5.5.1
My Java version is 1.8.0.201
The AspectJ class is:
src/main/java/example/AspectClass
@Aspect
public class AspectClass {
@Pointcut("execution(* example.MyApp.sayHi())")
public void pointCut() {
}
// @After("pointCut()")
// public void afterAdvice(JoinPoint joinPoint) throws Throwable {
// System.out.println("Before point cut...");
// System.out.println("After point cut.");
// }
@Around("pointCut()")
public void aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("before...");
joinPoint.proceed();
System.out.println("after...");
}
}
1.
Looks like when running the main method of the class, but the @Around
of AspectJ never run
Run the TestNG method, but the @Around
of AspectJ never run
Thanks @larsgrefer
@larsgrefer I fixed the problem without the modules, and updated my repository (https://github.com/ansonliao/Gradle-AspectJ-Sample), thanks @larsgrefer
'io.freefair.aspectj' plugin not work
Hey had the same problem and adding your sourceSet definitions solved it. Why isn't this explained in the documentation? https://docs.freefair.io/gradle-plugins/current/reference/
Instead they talk about using aspect and inpath in the dependencies which I could not get to work at all. Is this deprecated? If so, how to change the aspectpath?
I am sorry I know that my issue should be not an issue and should not be here, but I did many searches and read articles, and can't solve the problem.
Until now, I can't configure the correct AspectJ for my normal Gradle project (not android project, not spring mvc project, and don't want to introduce the big library such spring mvc aop module as it is the simple project for QA).
Could someone kindly provide the works
build.gradle
for reference?Thanks so much.