1

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.

Abhishek Agarwal
  • 846
  • 4
  • 13
  • 34
  • This might help you http://stackoverflow.com/questions/2610633/maven-compile-aspectj-project-containing-java-1-6-source – Shreyos Adikari Apr 26 '13 at 09:52
  • Possible duplicate of [Maven + AspectJ - all steps to configure it](http://stackoverflow.com/questions/12423965/maven-aspectj-all-steps-to-configure-it) – Jonathan Hult Mar 29 '17 at 16:12

1 Answers1

2

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)
Sander Verhagen
  • 8,540
  • 4
  • 41
  • 63