AspectJ + Gradle configuration

后端 未结 6 655
执念已碎
执念已碎 2021-02-04 11:31

I\'d like to use AspectJ in Gradle project (it\'s not an Android project - just a simple Java app).

Here is how my build.gradle looks like:

apply plugin         


        
6条回答
  •  自闭症患者
    2021-02-04 12:30

    A bit ugly, but short and does not require additional plugins or configurations. Works for me:

    Add aspectjweaver dependency ti Gradle build script. Then in the task you need it find the path to its jar and pass as javaagent in jvmArgs:

    dependencies {
        compile "org.aspectj:aspectjweaver:1.9.2"
    }
    
    test {
        group 'verification'
    
        doFirst {
            def weaver = configurations.compile.find { it.name.contains("aspectjweaver") }
            jvmArgs = jvmArgs << "-javaagent:$weaver"
        }
    }
    

    Add aop.xmlfile to the resources\META-INF\ folder with the specified Aspect class:

    
        
            
        
    
    

    Example of an aspect:

    package utils.listener
    
    import org.aspectj.lang.JoinPoint
    import org.aspectj.lang.annotation.*
    import org.aspectj.lang.reflect.MethodSignature
    
    @Aspect
    @SuppressWarnings("unused")
    public class StepListener {
    
        /* === Pointcut bodies. Should be empty === */
        @Pointcut("execution(* *(..))")
        public void anyMethod() {
        }
    
        @Pointcut("@annotation(org.testng.annotations.BeforeSuite)")
        public void withBeforeSuiteAnnotation() {
        }
    }
    

提交回复
热议问题