I have created a Aspectj Project in Eclipse ide but i need to build it using maven.
I have maven-aspectj plugin but don't know how to use it.
I have created a Aspectj Project in Eclipse ide but i need to build it using maven.
I have maven-aspectj plugin but don't know how to use it.
Below are the steps that I followed to get this working. This gave me compile-time weaving. If you need other strategies, clearly you need another approach (such as Spring AOP for runtime AOP proxies).
Add a property to standardize the AspectJ version that you use:
<properties>
<aspectj.version>1.7.2</aspectj.version>
...
Add the runtime dependency:
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
Add the AspectJ Maven plugin:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
<configuration>
<source>1.7</source>
<target>1.7</target>
<forceAjcCompile>true</forceAjcCompile>
</configuration>
</plugin>
I'm not sure if forceAjcCompile
makes a lot of sense, really, but I've seen some cases where the aspects weren't consistently applied. I'm blaming this (for now) on Eclipse overwriting class files or something, hence the forceAjcCompile
.
Additional things I did:
src/main/aspects
as an extra source directory (build-helper-maven-plugin
plugin). Just because it looks good in EclipsepluginExecution
/pluginExecutionFilter
for the AspectJ plugin (lifecycle-mapping
plugin) and set it to execute
and runOnIncremental
, so that also aspects are (re-)applied when coding and testing in Eclipse (using m2e)