Spring + AspectJ weaving for java 8 using aspectj-maven-plugin

前端 未结 1 1005
误落风尘
误落风尘 2020-12-02 23:44

I\'m migrating my project from java 7 to java 8 and the problem I have is related to aspectj weaving using aspectj-maven-plugin.

I could configure succe

1条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-03 00:15

    Solution before the official release prior to Sep 2015

    After many headaches and many hours struggling against this, fortunately I could solve this problem. Here is what I did:

    To use aspectj-maven-plugin with Java 8 I could configure version aspectj-maven-plugin 1.7 (Note that aspectj-maven-plugin 1.6 works for Java 7).

    So, the maven plugin configuration needs to be:

    
    
        org.codehaus.mojo
        aspectj-maven-plugin
        1.7-SNAPSHOT
        
            1.8
            1.8
            1.8
        
        
            
                
                    compile
                    test-compile
                
            
        
    
    

    By the way, the aspectJ jars needed are:

    
    
        org.aspectj
        aspectjrt
        1.8.1
    
    
        org.springframework
        spring-aop
        4.0.4.RELEASE
    
    
        org.aspectj
        aspectjweaver
        1.8.1
    
    

    The most important thing I've struggled was that to install the aspectj-maven-plugin 1.7 jar I had to do it manually since these jar/pom files aren't on maven repo yet.

    Update: So, the jar file can be downloaded from Haus Jira link (look at the Attachment section). If Haus is not available anymore you can download it from my github:

    https://github.com/fedepia/aspectj-maven-plugin-1.7

    After download it and copy it to my local repo I needed to create my own aspectj-maven-plugin-1.7-SNAPSHOT.pom file within the directory:

    .m2\repository\org\codehaus\mojo\aspectj-maven-plugin\1.7-SNAPSHOT\aspectj-maven-plugin-1.7-SNAPSHOT.pom
    

    I based on a copy from version 1.6 but had to modify the following content:

    1.7-SNAPSHOT
    
    
        1.8.1
        2.2.1
        2.9
    
    

    That's all here you go, hope to help.

    Update: (adding more details as Xtreme Biker asked in the comments)

    In my context configuration I have:

     
    
    
    

    For my java aspect I use:

    @Aspect
    public class NotificationAspect
    {
       ...
       @AfterThrowing(pointcut="@annotation(com.integration.core.meta.NotifyOnFailure)", throwing="ex")
       public void executeOnException(JoinPoint joinPoint, ExternalApiExecutionException ex) throws Throwable
        {
        ...
    



    Finally official plugin released since Sep 2015

    This is an update to the answer with the official plugin release. In order to use Java 8 with AspectJ, the official aspectj maven plugin can be found on this link:

    http://www.mojohaus.org/aspectj-maven-plugin/usage.html

    Here is the link to maven repository:

    http://mvnrepository.com/artifact/org.codehaus.mojo/aspectj-maven-plugin/1.8

    As the documentation stated the code to use it is:

    
      ...
      
        ...
        
          org.aspectj
          aspectjrt
          1.8.7
        
        ...
      
      ...
      
        
          
            org.codehaus.mojo
            aspectj-maven-plugin
            1.8
            
              
                
                  compile       
                  test-compile  
                
              
            
          
          ...
        
      
      ...
    
    

    0 讨论(0)
提交回复
热议问题