Search code examples
gradlebuild.gradlegradle-plugingradle-kotlin-dsl

gradle Error: "You must set the property 'aspectjVersion' before applying the aspectj plugin" when using kotlin DSL


I want to configure aspectJ plugin in my gradle project which is using kotlin DSL.

Below is my build.gradle.kts file.

    val aspectjVersion = "1.9.3"

    plugins {
        java
        id("aspectj.gradle") version "0.1.6"
    }

    group = "java-agents-demo"
    version = "1.0-SNAPSHOT"

    repositories {
        mavenCentral()
        mavenLocal()
        jcenter()
    }

    dependencies {
        implementation("org.aspectj","aspectjrt", aspectjVersion)
        implementation("org.aspectj","aspectjweaver", aspectjVersion)
        testCompile("junit", "junit", "4.12")
    }

    configure<JavaPluginConvention> {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }

When I run compileJava project I get error "You must set the property 'aspectjVersion' before applying the aspectj plugin".

I set aspectjVersion in my build file but I don't know what is the error in it.

Can anyone help me setup the aspectJ plugin for my project in correct way?


Solution

  • So val aspectjVersion = "1.9.3" defines a local variable, but the plugin is looking for a project property, note that this also means the plugin can't be applied straight away as the plugins block is evaluated before the rest of the build.gradle.kts (see limitations of plugin DSL), try this instead:

    val aspectjVersion by extra("1.9.3")
    
    plugins {
        java
        id("aspectj.gradle") version "0.1.6" apply false
    }
    
    apply(plugin = "aspectj.gradle")
    

    See Gradle docs on extra properties for details.