0

I have a project where I want to use aspects but not use spring. So I'm using AspectJ. In my particular scenario, LTW won't work for me, so I have to use CTW, which leads me to the maven aspectj plugin. I am currently using Java 7. I am weaving an aspect written in one maven module into another module that has it as a dependency.

The plugin seems to weave correctly when I am weaving a non-annotated aspect, but when I switch it to an annotation based aspect, it seems like the plug in isn't writing in the hasaspect() and aspectof() methods because at runtime I get exceptions saying that the methods are not there.

One thing I tried was to have the annotation based aspect extend the Aspects class, in hopes that then it will be able to find the methods, but that also did not work.

my plug in is configured as such:

   <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.4</version>
            <configuration>
                <showWeaveInfo>false</showWeaveInfo>
                <Xlint>ignore</Xlint>
                <source>1.7</source>
                <target>1.7</target>
                <complianceLevel>1.7</complianceLevel>
                <encoding>UTF-8</encoding>
                <verbose>false</verbose>
                <weaveDirectories>
                    <weaveDirectory>${project.build.outputDirectory}</weaveDirectory>
                </weaveDirectories>
                <aspectLibraries>
                    <aspectLibrary>
                        <groupId>my.group</groupId>
                        <artifactId>dependencywithaspecttoweave</artifactId>
                    </aspectLibrary>
                </aspectLibraries>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>org.aspectj</groupId>
                    <artifactId>aspectjtools</artifactId>
                    <version>1.7.2</version>
                </dependency>
            </dependencies>

My annotated aspect looks like this:

public class TraceLog extends Aspects {
private ComponentLogger logger;

@Pointcut(value = "within(my.package..*) && "
        + "execution(* *(..))")
public void scope() {
}

/**
 * Before method.
 *
 * @param thisJoinPoint The joinpoint.
 * @throws IllegalAccessException When it tries to get the logger and can't.
 */
@Before(value = "scope()")
public void logBefore(final JoinPoint thisJoinPoint) throws IllegalAccessException {
    logMethod(thisJoinPoint, "Entering " + createLogMessage(thisJoinPoint));
}
...

What I've had to do is revert back to the plain old non-annotated aspect style. But this is a drag since I can't then throw exceptions from the method (or at least as of yet, I do not know how to). Throwing exceptions would be particularly useful for things like security, where if a user did not have access, an exception would be the appropriate way to alert the caller.

Thanks in advance.

Eric
  • 1,023
  • 1
  • 15
  • 27
  • I had a similar problem about a week ago, while using the same plugin. I don't know if your code is the same as mine, but I had to use the plugin on both the module with the aspect and the module that contained the advised classes. The plugin invokes ajc to add the required methods to the aspect. It also invokes ajc to wrap your advice around (before/after) the annotated class. The question I raised (unanswered so far) is here: http://stackoverflow.com/questions/17075862/java-lang-verifyerror-incompatible-argument-to-function-aspectj. There is a link to my example code in github in there. – Software Engineer Jun 15 '13 at 01:04
  • Actually, yes i forgot to mention that i ran the plugin on both the module that contains the aspect as well as the module that contains the advised classes. The plugin configuration was identical with the exception of the aspectlibraries node of course. – Eric Jun 15 '13 at 02:07
  • Incidentally, I think I have a line on the question you asked. Check my answer there. – Eric Jun 15 '13 at 02:19

1 Answers1

0

I have almost the exact same setup working (I moved away from Spring and the Google App Engine only supports compile time weaving), and deploying a multi-module EAR.

From your question it sounds like you didn't find the AspectJ Multi-Module documentation and use that in your setup. From looking at your POM more closely I see that in your executions section you don't have anything for the test-compile phase. Also, you should be getting as much information as possible: set verbose and showWeaveInfo to true. That should give you a better error to Google (and probably get pointed back to StackOverflow) or ask for help with.

My executions section is as follows

      <!-- Executions for compiler -->
      <executions>
        <execution>
          <id>aspectj-compile</id>
          <goals>
            <goal>compile</goal>
          </goals>
        </execution>
        <execution>
          <id>aspectj-test-compile</id>
          <goals>
            <goal>test-compile</goal>
          </goals>
        </execution>
      </executions>

Between getting more information, the test-compile and setting up you project as recommended you should be able to get going.

Michael Gower
  • 81
  • 1
  • 5