[SOLVED] How to import aspectJ into project? – Stack Overflow

Issue

This Content is from Stack Overflow. Question asked by Boris Stupin

What’s up, guys

I have a need to use aspectJ. How to use the functionality of this lib / framework (I don’t know how to express myself correctly).

This is my build.gradle file

plugins {
    id "com.diffplug.eclipse.apt" version "3.22.0"
    id "com.github.johnrengelman.shadow" version "7.0.0"
    id "application"
}


version "0.2"
group "ru.fusionsoft"

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

configurations {
    // for dependencies that are needed for development only
    developmentOnly
//    aspectjVersion: '1.9.9.1'
}

dependencies {
    implementation group: 'org.aspectj', name: 'aspectjrt', version: '1.9.9.1'
    implementation group: 'org.aspectj', name: 'aspectjweaver', version: '1.9.9.1'
    implementation group: 'org.aspectj', name: 'aspectjtools', version: '1.9.9.1'
    implementation group: 'org.codehaus.mojo', name: 'aspectj-maven-plugin', version: '1.14.0'

    implementation "io.micronaut:micronaut-session:2.5.0"
    implementation "io.micronaut.redis:micronaut-redis-lettuce:2.5.0"
    implementation group: 'org.postgresql', name: 'postgresql', version: '42.2.8'
    implementation group: 'io.jsonwebtoken', name: 'jjwt', version: '0.9.1'
    implementation group: 'org.apache.pdfbox', name: 'pdfbox', version: '3.0.0-RC1'
    implementation group: 'cglib', name: 'cglib', version: '3.3.0'
    implementation 'org.reflections:reflections:0.10.2'
    annotationProcessor(platform("io.micronaut:micronaut-bom:2.5.0"))
    annotationProcessor("io.micronaut:micronaut-inject-java:2.5.0")
    annotationProcessor("io.micronaut:micronaut-validation:2.5.0")
    implementation(platform("io.micronaut:micronaut-bom:2.5.0"))
    implementation("io.micronaut:micronaut-inject:2.5.0")
    implementation("io.micronaut:micronaut-validation:2.5.0")
    implementation("io.micronaut:micronaut-runtime:2.5.0")
    implementation("javax.annotation:javax.annotation-api:1.3.2")
    implementation("io.micronaut:micronaut-http-server-netty:2.5.0")
    implementation("io.micronaut:micronaut-http-client:2.5.0")
    runtimeOnly("ch.qos.logback:logback-classic")
    testAnnotationProcessor(enforcedPlatform("io.micronaut:micronaut-bom:2.5.0"))
    testAnnotationProcessor("io.micronaut:micronaut-inject-java:2.5.0")
    testImplementation(enforcedPlatform("io.micronaut:micronaut-bom:2.5.0"))
    testImplementation("org.junit.jupiter:junit-jupiter-api:2.5.0")
    testImplementation("io.micronaut.test:micronaut-test-junit5:3.0.0")
    testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
    testImplementation "org.testcontainers:postgresql:1.14.3"
    annotationProcessor("io.micronaut.configuration:micronaut-openapi:1.5.3")
    implementation("io.swagger.core.v3:swagger-annotations")
    implementation("io.micronaut:micronaut-session:2.5.0")
    annotationProcessor ("io.micronaut.security:micronaut-security-annotations:2.5.0")
    implementation ("io.micronaut.security:micronaut-security-jwt:2.5.0")
    implementation("io.micronaut.sql:micronaut-jdbc-hikari:2.5.0")
    implementation("io.micronaut.sql:micronaut-hibernate-jpa:2.5.0")
    implementation("io.micronaut.data:micronaut-data-hibernate-jpa:2.5.0")
    annotationProcessor("io.micronaut.data:micronaut-data-processor:2.5.0")
    implementation group: 'com.jcraft', name: 'jsch', version: '0.1.44-1'

    // https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java
    implementation group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '3.141.59'

    // https://mvnrepository.com/artifact/com.github.2captcha/2captcha-java
    implementation group: 'com.github.2captcha', name: '2captcha-java', version: '1.0.1'

    // https://mvnrepository.com/artifact/com.linuxense/javadbf
    implementation group: 'com.linuxense', name: 'javadbf', version: '0.4.0'

    // https://mvnrepository.com/artifact/org.apache.commons/commons-csv
    implementation group: 'org.apache.commons', name: 'commons-csv', version: '1.8'

    // https://mvnrepository.com/artifact/org.apache.poi/poi
    implementation group: 'org.apache.poi', name: 'poi', version: '5.0.0'

    // https://mvnrepository.com/artifact/commons-beanutils/commons-beanutils
    implementation group: 'commons-beanutils', name: 'commons-beanutils', version: '1.9.4'

    // https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core
    implementation group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.13.1'

    // https://mvnrepository.com/artifact/com.fasterxml.jackson.dataformat/jackson-dataformat-xml
    implementation group: 'com.fasterxml.jackson.dataformat', name: 'jackson-dataformat-xml', version: '2.13.1'

    // Metrics
    implementation "io.micronaut.micrometer:micronaut-micrometer-core"
    implementation "io.micronaut.micrometer:micronaut-micrometer-registry-statsd"
    implementation "io.micronaut:micronaut-management"
    implementation 'io.micronaut.micrometer:micronaut-micrometer-registry-prometheus'
}



test.classpath += configurations.developmentOnly

mainClassName = "ru.fusionsoft.hcs.Application"

// use JUnit 5 platform
test {
    useJUnitPlatform()
}

java {
    sourceCompatibility = JavaVersion.toVersion('14')
    targetCompatibility = JavaVersion.toVersion('14')
}

tasks.withType(JavaCompile) {
    options.encoding = "UTF-8"
    options.compilerArgs.addAll([
        '-parameters',
        // enables incremental compilation
        '-Amicronaut.processing.incremental=true',
        '-Amicronaut.processing.annotations=ru.fusionsoft.*',
        "-Amicronaut.processing.group=$project.group",
        "-Amicronaut.processing.module=$project.name",
    ])
}

shadowJar {
    mergeServiceFiles()
}

tasks.withType(JavaCompile) {
    options.fork = true
    options.forkOptions.jvmArgs << '-Dmicronaut.openapi.views.spec=rapidoc.enabled=true,swagger-ui.enabled=true,swagger-ui.theme=default,swagger-ui.tagsSorter='alpha',swagger-ui.operationsSorter='alpha''
}

tasks.withType(JavaExec) {
    classpath += configurations.developmentOnly
    jvmArgs('-XX:TieredStopAtLevel=1', '-Dcom.sun.management.jmxremote')
    if (gradle.startParameter.continuous) {
        systemProperties(
            'micronaut.io.watch.restart':'true',
            'micronaut.io.watch.enabled':'true',
            "micronaut.io.watch.paths":"src/main"
        )
    }
}

I didn’t write it initially, and I never used the primitive again. Apparently, just entering the implementation is not enough to use AOP. If you need additional information, I’ll update.

P.S. Sorry for my poor English



Solution

It does not make much sense to add a Maven plugin to a Gradle build and hope that somehow magically it will work there. As the name implies, aspectj-maven-plugin is meant to be used in Maven projects. You need to use a Gradle plugin. Maybe you want to take a look at the Freefair plugins for AspectJ and decide which scenario (and therefore, which plugin) fits your use case.


This Question was asked in StackOverflow by Boris Stupin and Answered by kriegaex It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?