How to build aspectj project using maven?

∥☆過路亽.° 提交于 2019-11-28 13:00:51

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)
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!