I am working with a third-party application, whose code is available, and I am trying to use AspectJ to get some additional behavior on a particular module (lets say A module) of such application. The application has more modules (lets suppose B and C modules), but I only need to apply extra functionality on the A module.
I have created an AspectJ module. I am using Maven as a build tool. I have the following project structure:
project
+-- aspect-module
| |
| +- pom.xml
|
+-- A module
| |
| +- pom.xml
|
+-- B module
| |
| +- pom.xml
|
+-- C module
| |
| +- pom.xml
|
+ pom.xml
The point is that the aspect-module defines an aspect which depends on the A module code (imports several of its tracked classes). For this reason, it does require a compile time dependency on the A module, thus, the aspect-module/pom.xml includes the A module dependency.
The pom.xml files I have are the followings.
Aspect module pom
...
<parent>
<groupId>project</groupId>
<artifactId>parent</artifactId>
<version>0.4.2-SNAPSHOT</version>
</parent>
<artifactId>aspectModule</artifactId>
<properties>
...
</properties>
<dependencies>
<dependency>
<groupId>project</groupId>
<artifactId>moduleA</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>1.9.2</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.2</version>
<scope>runtime</scope>
</dependency>
...
</dependencies>
A module pom.xml
...
<parent>
<groupId>project</groupId>
<artifactId>parent</artifactId>
<version>0.4.2-SNAPSHOT</version>
</parent>
<artifactId>moduleA</artifactId>
<packaging>jar</packaging>
<properties>
...
</properties>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>AspectModule</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>Bmodule</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>1.9.2</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.2</version>
<scope>runtime</scope>
</dependency>
...
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>assemble-all</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<configuration>
<mainClass>Main</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<configuration>
<aspectLibraries>
<aspectLibrary>
<groupId>${project.groupId}</groupId>
<artifactId>AspectModule</artifactId>
</aspectLibrary>
</aspectLibraries>
</configuration>
</plugin>
</plugins>
</build>
Main pom.xml
<modelVersion>4.0.0</modelVersion>
<groupId>project</groupId>
<artifactId>main</artifactId>
<packaging>pom</packaging>
<version>0.4.2-SNAPSHOT</version>
<modules>
<module>AModule</module>
<module>BModule</module>
<module>CModule</module>
<module>AspectModule</module>
</modules>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>project</groupId>
<artifactId>AspectModule</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>project</groupId>
<artifactId>AModule</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<properties>
...
</properties>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<!-- IMPORTANT -->
<useIncrementalCompilation>false</useIncrementalCompilation>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.14.0</version>
<configuration>
<!--<showWeaveInfo>true</showWeaveInfo> -->
<source>${java.version}</source>
<target>${java.version}</target>
<Xlint>ignore</Xlint>
<complianceLevel>${java.version}</complianceLevel>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
<executions>
<execution>
<!-- IMPORTANT -->
<phase>process-sources</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>a-make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.5.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
I need to include the aspect module dependency in the module A pom.xml since, otherwise, I get the error: The artifact project:aspectModule referenced in aspectj plugin as an aspect library, is not found the project dependencies
I did mvn clean install in the main project but as the result maven refuses to compile projects saying that they contain cyclic references. Can anyone help me?
thank you!
J Fabian Meier is right, you cannot have cyclic references in a Maven project. (What was first, the chicken or the egg?)
Let me elaborate a bit on his brief two suggestions and add a third one FYI:
Your choices are as follows:
aspectModuleseems to be clearly targeted atmoduleA, even using A's classes, simply put the aspects into A.aspectModuleto be re-usable by other modules or projects. Then you could, as J. Fabian said, split off acommonmodule ofmoduleA. BothaspectModuleandmoduleAcould then depend oncommon, andmoduleAcould depend onaspectModulewithout any circularities.aspectModuledependency frommoduleAand keep it unwoven. Then you could addwovenModuleA, depending onaspectModuleandmoduleAand do the weaving there. All other modules would, if they need aspect-enhanced A-type functionality, depend onwovenModuleA, not on the unwoven version. But that option only makes sense if you do not have the source code formoduleA, need to do binary weaving and load-time weaving is not an option.Technically, all 3 options are viable. Which one makes most sense, depends on your scenario. The way you described it, the first and simplest one looks like a good choice.