I use the aspectj-maven-plugin in order to weave my aspect in the existing dependencies of my project (in my case, the org.apache.spark jars). Than I use the maven-assembly-plugin to generate a standalone jar containing all the dependencies.
The aspectj plugin seems to correctly weave the aspect in the external jar. However, when I call the jar with dependencies created by the maven assembly plugin, the aspects are not called for the external jar (but works perfectly for the pointcut in my own code).
I suspect that the maven assembly plugin does not use the weaved jar when creating the jar with dependencies. But I have no idea of where the aspectj plugin stores the weaved jar and how to use them instead of the original ones.
Here is my maven configuration :
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>be.example.aspectj</groupId>
<artifactId>minimal</artifactId>
<version>1.0-SNAPSHOT</version>
<name>minimal</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.7</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.spark/spark-core -->
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.12</artifactId>
<version>3.1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.spark/spark-sql -->
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-sql_2.12</artifactId>
<version>3.1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version>
</dependency>
</dependencies>
<build>
<!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- Maven assembly plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<!-- get all project dependencies -->
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<!-- MainClass in mainfest make a executable jar -->
<archive>
<manifest>
<mainClass>be.example.aspectj.App</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- Deactivating the default maven compiler plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>default-compile</id>
<phase>none</phase>
</execution>
</executions>
</plugin>
<!-- Aspectj configuration -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.14.0</version>
<configuration>
<complianceLevel>${maven.compiler.source}</complianceLevel>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.source}</target>
<showWeaveInfo>true</showWeaveInfo>
<verbose>true</verbose>
<Xlint>ignore</Xlint>
<encoding>UTF-8 </encoding>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>be.example.aspectj.App</mainClass>
</manifest>
</archive>
<!-- Weaved dependencies -->
<weaveDependencies>
<weaveDependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-sql_2.12</artifactId>
</weaveDependency>
<weaveDependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.12</artifactId>
</weaveDependency>
</weaveDependencies>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
And here is the output for the aspectj-maven-plugin. I seems to indicate that the weaving was correctly done but the warning seems to indicate that there is conflict between several versions of a same jar :
[INFO] --- aspectj-maven-plugin:1.14.0:compile (default) @ minimal ---
[INFO] Showing AJC message detail for messages of types: [error, warning, fail]
[INFO] Join point 'method-call(java.sql.Connection java.sql.DriverManager.getConnection(java.lang.String, java.lang.String, java.lang.String))' in Type 'org.sparkproject.jetty.security.JDBCLoginService' (JDBCLoginService.java:183) advised by before advice from 'DriverManagerAspect' (DriverManagerAspect.aj:8)
[INFO] Join point 'method-call(java.sql.Connection java.sql.DriverManager.getConnection(java.lang.String))' in Type 'org.sparkproject.jetty.server.session.DatabaseAdaptor' (DatabaseAdaptor.java:305) advised by before advice from 'DriverManagerAspect' (DriverManagerAspect.aj:8)
[INFO] Join point 'method-call(boolean be.example.aspectj.Account.withdraw(int))' in Type 'be.example.aspectj.App' (App.java:19) advised by before advice from 'AccountAspect' (AccountAspect.aj:9)
[INFO] Join point 'method-call(java.sql.Connection java.sql.DriverManager.getConnection(java.lang.String))' in Type 'be.example.aspectj.App' (App.java:36) advised by before advice from 'DriverManagerAspect' (DriverManagerAspect.aj:8)
[WARNING] duplicate resource: 'META-INF/NOTICE'
/home/jeromefink/.m2/repository/org/apache/spark/spark-core_2.12/3.1.2/spark-core_2.12-3.1.2.jar:0
[WARNING] duplicate resource: 'META-INF/LICENSE'
/home/jeromefink/.m2/repository/org/apache/spark/spark-core_2.12/3.1.2/spark-core_2.12-3.1.2.jar:0
[WARNING] duplicate resource: 'META-INF/DEPENDENCIES'
/home/jeromefink/.m2/repository/org/apache/spark/spark-core_2.12/3.1.2/spark-core_2.12-3.1.2.jar:0
[WARNING] duplicate resource: 'META-INF/services/org.apache.spark.deploy.history.EventFilterBuilder'
/home/jeromefink/.m2/repository/org/apache/spark/spark-core_2.12/3.1.2/spark-core_2.12-3.1.2.jar:0
[WARNING] duplicate resource: 'META-INF/maven/org.spark-project.spark/unused/pom.xml'
/home/jeromefink/.m2/repository/org/apache/spark/spark-core_2.12/3.1.2/spark-core_2.12-3.1.2.jar:0
[WARNING] duplicate resource: 'META-INF/maven/org.spark-project.spark/unused/pom.properties'
/home/jeromefink/.m2/repository/org/apache/spark/spark-core_2.12/3.1.2/spark-core_2.12-3.1.2.jar:0
My full minimal example is available on this Github repository. Do you have any idea of how to create a jar with dependencies containing the dependency jar weaved by aspectj ? Or do you know where the aspectj-maven-plugin stores the jar he weaved ?
Thank you for reading me.