Saturday, March 20, 2010

Using AspectJ for AOP on Maven project

AspectJ is used for Aspect Oriented Programming (AOP) in Java.

To configure AspectJ inside a Maven project you use aspectj-maven-plugin.

Modify your Maven project's pom.xml as follows:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
...
    <properties>
        <aspectj.version>1.6.8</aspectj.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>${aspectj.version}</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.3</version>
                <configuration>
                    <source>1.5</source>
                    <target>1.5</target>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>       <!-- use this goal to weave all your main classes -->
                            <goal>test-compile</goal>  <!-- use this goal to weave all your test classes -->
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjtools</artifactId>
                        <version>${aspectj.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.5</source>
                    <target>1.5</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

When you want to upgrade AspectJ version, simply change aspectj.version from properties.

2 comments:

Anonymous said...

Keep sharing such ideas in the future as well. This was actually what I was looking for, and I am glad to came here! Thanks for sharing the such information with us.http://www.agilitty.co.uk |

http://www.lettrader.co.uk |

http://www.dtas-diamonds.co.uk |

http://www.bodwennursery.co.uk |

http://www.runhamsweddingcars.co.uk |

http://www.im16.co.uk |

http://www.ralphandrusso.co.uk |

http://www.alaniles.co.uk |

http://www.acenet2000.co.uk |

http://www.hastings-online.co.uk |

Getit said...

I love this kind of post. Download Zapya for PC, Zapya free download, Zapya download, Zapya apk to share files and folders

Post a Comment

 
Copyright 2009 Spring vs Java EE Web Dev. Powered by Blogger Blogger Templates create by Deluxe Templates. WP by Masterplan