在 Kotlin 项目中使用 Freefair AspectJ 插件(使用 gradle)

问题描述 投票:0回答:1

我在 kotlin 项目中配置 AspectJ 时遇到问题。我正在使用 freefair 插件,但它似乎不起作用。我开始申请时没有任何问题,但没有考虑到这些方面。

我将方面放在 src/main/aspectj 源目录中的 MetricAspect.kt 文件中

我现在拥有的是一个 gradle.build.kts 文件,看起来像这样:

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile


plugins {
    //other plugins

    id("io.freefair.aspectj") version "6.6.3"

    val kotlinVersion = "1.7.21"
    kotlin("jvm") version kotlinVersion
    kotlin("plugin.spring") version kotlinVersion

    //other plugins
}


allprojects {
    apply(plugin = "org.jetbrains.kotlin.jvm")
    apply(plugin = "io.gitlab.arturbosch.detekt")
    apply(plugin = "com.diffplug.spotless")
    apply(plugin = "io.freefair.aspectj")
    apply(plugin = "kotlin-spring")
    version = "1.0.0-SNAPSHOT"

    java {
        sourceCompatibility = JavaVersion.VERSION_17
        targetCompatibility = JavaVersion.VERSION_17
    }

    

    repositories {
        maven {
            url = uri("https://private.repo/public-maven-virtual/")
            credentials {
                username = user
                password = password
            }
        }
    }

    dependencies {

        kotlinCompilerPluginClasspath("gradle.plugin.aspectj:gradle-aspectj:0.1.6")
        
        //other deps

        implementation("org.aspectj:aspectjrt:1.9.7")
        annotationProcessor("org.aspectj:aspectjtools:1.9.7")

        //other deps
    }

    tasks.withType<KotlinCompile> {
        kotlinOptions {
            freeCompilerArgs = listOf("-Xjsr305=strict")
            jvmTarget = "17"
        }
    }

    tasks.withType<Test> {
        useJUnitPlatform()
    }

    spotless {
        //....
    }

    detekt {
        buildUponDefaultConfig = true // preconfigure defaults
        allRules = false // activate all available (even unstable) rules.
        parallel = true
        config = files("$rootDir/detekt.yml")
    }
}

tasks {
    bootJar {
        enabled = false
    }
}

关于它的文档非常稀缺,每个人似乎都有自己的一种方法来配置这个插件,这很奇怪。有没有官方的方法可以做到这一点?

kotlin gradle aspectj
1个回答
0
投票

AspectJ 编译器仅理解基于注释的 Java 语法和本机 AspectJ 语法。如果你想使用 Kotlin 等其他 JVM 语言,则需要使用 post-compile weaving。 Freefair 有一个选项。如果您在运行该项目时遇到问题,请随时提供 MCVE 项目并提出后续问题。

© www.soinside.com 2019 - 2024. All rights reserved.