AspectJ + Gradle 配置

AspectJ + Gradle configuration

我想在 Gradle 项目中使用 AspectJ(它不是 Android 项目 - 只是一个简单的 Java 应用程序)。

这是我的 build.gradle 的样子:

apply plugin: 'java'

buildscript {
    repositories {
        maven {
            url "https://maven.eveoh.nl/content/repositories/releases"
        }
    }
    dependencies {
        classpath "nl.eveoh:gradle-aspectj:1.6"
    }
}

repositories {
    mavenCentral()
}

project.ext {
    aspectjVersion = "1.8.2"
}

apply plugin: 'aspectj' 

dependencies {
    //aspectj dependencies
    aspectpath "org.aspectj:aspectjtools:${aspectjVersion}"
    compile "org.aspectj:aspectjrt:${aspectjVersion}"
}

代码编译通过,但是画面好像没有编织。有什么问题吗?

我已经为此苦苦挣扎了一段时间,所以这是我使用的配置并且运行良好。

在您的配置中执行此操作。

configurations {
    ajc
    aspects
    aspectCompile
    compile{
        extendsFrom aspects
    }
}

在您的依赖项中使用以下配置。 Spring 如果您不使用 spring fwk,则不需要依赖项。

dependencies {

    //Dependencies required for aspect compilation
    ajc "org.aspectj:aspectjtools:$aspectjVersion"
    aspects "org.springframework:spring-aspects:$springVersion"
    aspectCompile  "org.springframework:spring-tx:$springVersion"
    aspectCompile  "org.springframework:spring-orm:$springVersion"
    aspectCompile  "org.hibernate.javax.persistence:hibernate-jpa-2.1-api:$hibernateJpaVersion"

}

compileJava {
    sourceCompatibility="1.7"
    targetCompatibility="1.7"
    //The following two lines are useful if you have queryDSL if not ignore
    dependsOn generateQueryDSL
    source generateQueryDSL.destinationDir

    dependsOn configurations.ajc.getTaskDependencyFromProjectDependency(true, "compileJava")

    doLast{
        ant.taskdef( resource:"org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties", classpath: configurations.ajc.asPath)
        ant.iajc(source:"1.7", target:"1.7", destDir:sourceSets.main.output.classesDir.absolutePath, maxmem:"512m", fork:"true",
                aspectPath:configurations.aspects.asPath,
                sourceRootCopyFilter:"**/.svn/*,**/*.java",classpath:configurations.compile.asPath){
            sourceroots{
                sourceSets.main.java.srcDirs.each{
                    pathelement(location:it.absolutePath)
                }
            }
        }
    }
}

我不使用插件,我使用 ant 和 aspectj 编译器来做到这一点,可能会有一个简单的方法

看起来有一个用于 AspectJ 的新 "official" gradle 插件:

https://plugins.gradle.org/plugin/aspectj.gradle

遗憾的是文档很少。我自己没试过。

只想为 Archie 提到的 AspectJ 添加所谓的 "official" 插件。

这里有一些 gradle 脚本示例,说明如何操作:

apply plugin: 'java'



sourceCompatibility = '1.8'
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'

if (!hasProperty('mainClass')) {
    ext.mainClass = 'com.aspectz.Main'
}
buildscript {
  repositories {
    maven {
      url "https://plugins.gradle.org/m2/"
    }
  }
  dependencies {
    classpath "gradle.plugin.aspectj:gradle-aspectj:0.1.6"
    //classpath "gradle.plugin.aspectj:plugin:0.1.1"
    //classpath "gradle.plugin.aspectj:gradle-aspectj:0.1.1"
  }
}


ext {
    aspectjVersion = '1.8.5'
}

apply plugin: "aspectj.gradle"


repositories {
    mavenCentral()
}

dependencies {

    testCompile group: 'junit', name: 'junit', version: '4.10'
    compile("log4j:log4j:1.2.16")
    compile("org.slf4j:slf4j-api:1.7.21")
    compile("org.slf4j:slf4j-log4j12:1.7.21")
    compile("org.aspectj:aspectjrt:1.8.5")
    compile("org.aspectj:aspectjweaver:1.8.5")


}

不过好像只支持Java8及以上。当您使用 java 7 构建它时,出现错误:

java.lang.UnsupportedClassVersionError: aspectj/AspectJGradlePlugin : Unsupported major.minor version 52.0

将此添加到我的 build.gradle 文件中对我有用。

project.ext {
  aspectjVersion = '1.8.12'
}

apply plugin: "aspectj.gradle"

buildscript {
  repositories {
    maven {
      url "https://plugins.gradle.org/m2/"
    }
  }
  dependencies {
    classpath "gradle.plugin.aspectj:gradle-aspectj:0.1.6"
  }
}

执行 gradle assemble 注入方面中定义的代码。

有点难看,但很短,不需要额外的插件或配置。 适合我:

添加 aspectjweaver 依赖 ti Gradle 构建脚本。然后在您需要的任务中找到它的 jar 的路径并在 jvmArgs:

中作为 javaagent 传递
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"
    }
}

aop.xml 文件添加到具有指定方面 class 的 resources\META-INF\ 文件夹:

<aspectj>
    <aspects>
        <aspect name="utils.listener.StepListener"/>
    </aspects>
</aspectj>

相位示例:

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() {
    }
}

这个 plugin 对我有用 (gradle 5.6):

plugins {
    id 'java'
    id "io.freefair.aspectj.post-compile-weaving" version "4.1.4"
}