简体   繁体   中英

How to build aspectj project using maven?

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).

  1. Add a property to standardize the AspectJ version that you use:

     <properties> <aspectj.version>1.7.2</aspectj.version> ... 
  2. Add the runtime dependency:

     <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>${aspectj.version}</version> </dependency> 
  3. 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:

  1. Add src/main/aspects as an extra source directory ( build-helper-maven-plugin plugin). Just because it looks good in Eclipse
  2. Add a pluginExecution / 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM