Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven vs. AspectJ - Example?

MY aspect works great from Eclipse with AspectJ plugin, however if I try to use it with Maven I get .... nothing.

I tried this http://mojo.codehaus.org/aspectj-maven-plugin/includeExclude.html

I add loggin in my aspect and I try to test it with junit test, but when I run

mvn clean
mvn test

I get...

[INFO] [aspectj:compile {execution: default}]

But i dont see logging in test

If I do compiling in Eclipse it works find, but Id like it to be IDE Independent(so I could use it with Hudson)

P.S. I use .aj file for Aspect

I tried to Google it, but I cant find any working example.

like image 732
martin Avatar asked Jul 18 '09 20:07

martin


People also ask

What is AspectJ Maven plugin used for?

This plugin weaves AspectJ aspects into your classes using the AspectJ compiler ("ajc"). Typically, aspects are used in one of two ways within your Maven reactors: As part of a Single Project, implying aspects and code are defined within the same Maven project.

What is AspectJ used for?

AspectJ Leads to Clean Code AOP tries to reduce code scattering and code tangling. Code is tangled when its crosscutting concern code is intermixed with code that implements other concerns, and code is scattered when crosscutting code is spread out over multiple modules.

Is AspectJ slow?

A study done showed that the AspectJ compiler ajc is about 34% slower than the Sun Microsystems Java 1.3 compiler and about 62% slower than the Java 1.4 compiler.


1 Answers

Without seeing your POM it's hard to say, one thing to check is that Maven expects your aspects to be under src/main/aspect rather than src/main/java by default.

You also need to ensure the aspectj runtime library is on your classpath (in Eclipse it is included by the AJDT classpath container.

For example (from the plugin documentation):

<project>
    ...
    <dependencies>
        ...
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.6.2</version>
        </dependency>
        ...
    </dependencies>
    ...
    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.2</version>
                <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>
           </plugin>
           ...
       </plugins>
   <build>
...
</project>

If neither of these work, can you post your pom contents? It might help to identify the problem.

like image 150
Rich Seller Avatar answered Oct 15 '22 20:10

Rich Seller