AspectJ + Junit + Maven - pointcut recognized in tests but NoSuchMethodError: aspectOf() exception thrown

孤街醉人 提交于 2019-11-30 08:53:06
stewenson

So, the trick was to compile that library with my aspects not with javac but with ajc (aka aspectj-maven-plugin)

That's it. I just had to add this into the maven module with aspects (they are in src/main/java)

Aspects are annotation ridden so you have to have 1.6 source/target/compliance level

ASPECTJ MODULE

<!-- Build -->
<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.4</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
                <complianceLevel>1.6</complianceLevel>
                <verbose>true</verbose>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
            </executions>
            <dependencies>
            </dependencies>
        </plugin>
    </plugins>
</build>

Than you have to add this module as a test dependency into your target module you want to use aspects with

TARGET MODULE

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.4</version>
            <configuration>
                <aspectLibraries>
                    <aspectLibrary>
                        <groupId>that-artifact</groupId>
                        <artifactId>mentioned-above</artifactId>
                    </aspectLibrary>
                </aspectLibraries>
                <source>1.6</source>
                <target>1.6</target>
                <complianceLevel>1.6</complianceLevel>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>test-compile</goal>
                     </goals>
                     <configuration>
                         <showWeaveInfo>true</showWeaveInfo>
                     </configuration>
                 </execution>
             </executions>
         </plugin>
     </plugins>
 </build>

You have to use 1.6 level everywhere

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