Java – how to run AspectJ load weaving on gradle (especially libgdx build)
So I want to inject some testing and logging functions into my libgdx game
So I added the following to the gradle dependency on the main desktop
compile 'org.aspectj:aspectjweaver:1.8.2' compile "org.aspectj:aspectjrt:1.8.2"
At first it didn't find dependencies, but it solved this problem by turning off offline mode and closing and reopening my IntelliJ project (the gradle sync button doesn't work)
My understanding is that aspectjweaver must be loaded as a Java proxy So I found where gradle downloaded it and added the following to my VM runtime configuration options
-javaagent:/Users/daniel/.gradle/caches/modules-2/files-2.1/org.aspectj/aspectjweaver/1.8.2/4963c0bef4748d5ad039cc26c1ac32a082eb755e/aspectjweaver-1.8.2.jar
Surprisingly, this gives me the following warning message
objc[66447]: Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home/bin/java and /Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home/jre/lib/libinstrument.dylib. One of the two will be used. Which one is undefined.
No - javaagent line, the message does not exist
I tried to load the braid sample according to this example http://andrewclement.blogspot.co.uk/2009/02/load-time-weaving-basics.html
However, I don't need a separate reusable aspect, so I just created a basic AOP in the SRC directory XML file, which contains the following contents
<aspectj> <weaver options="-verbose"/> </aspectj>
Obviously, I haven't set anything yet. I just want to confirm whether the setting is normal There are enough differences between the tutorial and my target environment, and I think many may make mistakes
I don't care if the solution is compile time or class loading, as long as it works predictably in the libgdx / gradle environment Because I am not familiar with the building environment of libgdx / gradle, I chose to view the course loading solution requirement.
Finish ahead of schedule
Update: will try to pass this http://www.breskeby.com/2010/02/using-gradle-with-aspectj/
... but the tutorial doesn't mention the really familiar iajc, and it seems complicated to know how to use it in libgdx build scripts
Solution
I won't use AspectJ to provide a gradle implementation, but Maven Obviously, you must customize / adapt to meet your needs
JAVA
@Aspect @ajcPrivileged public class MyAspect { @pointcut("execution(* bar.foo(String,Boolean)) && args(string,bool)") void foo(String string,Boolean bool) {} @Around(value = "loadFile(filePathName,toGray)",argNames = "filePathName,toGray") public MyReturnType foo(final ProceedingJoinPoint joinPoint,final String string,final Boolean bool) throws MyEx { //impl } }
POM
See weaving third-party dependency annotations to define the third-party libraries to weave
<project ..> .. <properties> <aspectj-maven-plugin.version>1.7</aspectj-maven-plugin.version> <aspectjrt.version>1.8.2</aspectjrt.version> .. </properties> <dependencies> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>${aspectjrt.version}</version> </dependency> .. </dependencies> <build> .. <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>aspectj-maven-plugin</artifactId> <version>${aspectj-maven-plugin.version}</version> <configuration> <complianceLevel>${java.version}</complianceLevel> <source>${java.version}</source> <target>${java.version}</target> <!-- weave third party dependency --> <weaveDependencies> <weaveDependency> <groupId>yourThirdPartyDepGroupId</groupId> <artifactId>yourThirdPartyDepGroupIdArtifactId</artifactId> </weaveDependency> </weaveDependencies> </configuration> <!-- weave on compile --> <executions> <execution> <goals> <goal>compile</goal> <goal>test-compile</goal> </goals> </execution> </executions> </plugin> </plugins> <pluginManagement> <plugins> <!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.--> <plugin> <groupId>org.eclipse.m2e</groupId> <artifactId>lifecycle-mapping</artifactId> <version>${lifecycle-mapping.version}</version> <configuration> <lifecycleMappingMetadata> <pluginExecutions> <pluginExecution> <pluginExecutionFilter> <groupId>org.codehaus.mojo</groupId> <artifactId>aspectj-maven-plugin</artifactId> <versionRange>[1.7,)</versionRange> <goals> <goal>compile</goal> </goals> </pluginExecutionFilter> <action> <ignore></ignore> </action> </pluginExecution> </pluginExecutions> </lifecycleMappingMetadata> </configuration> </plugin> </plugins> </pluginManagement> </build> </project>