1

I have java library that I created which implements AOP using AspectJ runtime and AspectJ maven plugin. The AspectJ point cut is getting triggered when I add the annotations to the functions which are defined in the same library itself.

I want to use this library in another project so that I don't have to implement AOP in all of the projects that I will be using this library with regardless of their nature: native java app, spring or spring-boot apps. And regardless of they were using maven or gradle.

I tried to add my library to another gradle project spring configured with xml file. I added the annotation to function which is declared in the host app but the AOP doesn't get triggered, however when I call function from the library itself that has the annotation the AOP gets triggered even if I call it from host application.

Is there way to be able to do this or should the implementation of AOP be done in the same host project as well ?

For my example now the host application is using spring-framework and is built with gradle.

NOTE: the library is compiled and exported as jar using maven shade plugin

library POM.xml

<dependency>
   <groupId>org.aspectj</groupId>
   <artifactId>aspectjrt</artifactId>
   <version>1.8.9</version>
</dependency>
<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.10</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <complianceLevel>1.8</complianceLevel>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Aspect class

@Aspect
public class TimerAspect {
    @Around("@annotation(Timer) && execution(* *(..))")
    public Object timerMethod(ProceedingJoinPoint joinPoint) throws Throwable{
      ...code
    }

}

on the other application which uses gradle and spring-framework I just added the library as a dependency

implementation('com.exmple.library:aspetLib:1.0.0')

As I mentioned previously, the annotation is working fine if the functions are from the same library which has the AOP, however when I add this annotation to a function from the host app which uses spring the AOP doesn't trigger.

Eugene
  • 5,269
  • 2
  • 14
  • 22
Ali
  • 11
  • 2
  • 1
    Welcome to SO. Your question is unclear. First you talk about Maven, then Gradle. Then you explain a lot of things which are hard to understand without any code. Please learn what an [MCVE](https://stackoverflow.com/help/mcve) is, then publish one on GitHub. Otherwise, I do not know how to help you debug your multi-module build. I am also unsure if you want to weave code you compile yourself or weave existing binaries. I can only guess you might miss an AspectJ compiler plugin like Freefair in your Gradle target project, similar to what AspectJ Maven does in the Maven world. – kriegaex May 14 '22 at 12:48
  • 1
    Hi thanks for your response. I will look into the MCVE for sure thanks for the tip. However, I think you mostly gave me the answer. From what I understand is that in order to run the AspectJ the code needs to be compiled by the host application. In case of maven we use the AspectJ maven plugin and for gradle we use Freefair in order to make it work. I was trying to see if it possible to just compile in the AspectJ library and then added as dependency in any other project and it will be good to go, but apparently it does not work like this. – Ali May 14 '22 at 13:43
  • 1
    The target code needs to be instrumented by the aspects, of course. Instead of using the AspectJ compiler, you can also use load-time weaving, then you do not need to modify the target build, only add a JVM parameter. – kriegaex May 14 '22 at 16:33
  • Sorry for the late replay, I managed to make it work using the load-time weaving and adding the jvm args in the host project. Thanks for your help. – Ali May 20 '22 at 10:53

0 Answers0