Gradle and AspectJ - avoid weaving my own packages during compile time

半城伤御伤魂 提交于 2019-12-11 11:29:55

问题


I'm creating an aspectj project, which is NOT spring related. All the aspects were written by me. So Far I've been working with "ant" build to compile my agent, and now I'm trying to move to gradle.

My problem - I don't want my classes to be instrumented, but the ajc weaves my code as well, so if I'm adding a point cut to "someFunction()" function and my code com.myproject.MyClass has a function called "someFunction" it will be instrumented.

How can I configure the gradle aspectj plugin to NOT instrument classes in "com.myProject"? (during runtime it's the aop.xml file, but the problem is in compile time).

My code:

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'eclipse-wtp'

ext.aspectjVersion = '1.8.8'

configurations {

    ajc
    aspects
    aspectCompile
    ajInpath

    compile {
        extendsFrom aspects
    }

}

publishing {
    publications {
        mavenJava(MavenPublication) { from components.java }
    }
}

repositories {
    jcenter()

}


compileJava {
    sourceCompatibility = "1.6"
    targetCompatibility = "1.6"

    doLast {
        ant.taskdef(resource: "org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties",
                classpath: configurations.ajc.asPath)
        ant.iajc(
                source: sourceCompatibility,
                target: targetCompatibility,
                destDir: sourceSets.main.output.classesDir.absolutePath,
                maxmem: "512m",
                fork: "true",
                inpath: configurations.ajInpath.asPath,
                aspectPath: configurations.aspects.asPath,
                Xlint: "ignore",
                sourceRootCopyFilter: "**/*.java, **/*.class, **/*.aj",
                classpath: "${configurations.compile.asPath};${configurations.aspectCompile.asPath}") {
                    sourceroots {
                        sourceSets.main.java.srcDirs.each {
                            pathelement(location: it.absolutePath)
                        }
                    }
                }
    }
}

dependencies {
    ajc "org.aspectj:aspectjtools:1.8.8"

    compile "org.aspectj:aspectjrt:1.8.8"


}

Thanks


回答1:


If your classes are all within a special package or any of its subpackages, e.g. my.company.blah, just add this to your pointcuts:

&& !within(my.company.blah..*)



回答2:


The problem is solved by:

  1. first compiling the code
  2. then adding the aspectj compilation with the specific classpath and sources.

This means that instead of implementing compileJava I added a new task called compileAjc that depends on compileJava's output:

task compileAjc(overwrite: true) {

    // Declare the output directory to enable uptodate checks
    outputs.dir sourceSets.main.output.classesDir

    sourceCompatibility = "1.6"
    targetCompatibility = "1.6"

    doLast {
        ant.taskdef(resource: "org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties",
                classpath: configurations.ajc.asPath)
        ant.iajc(
                source: sourceCompatibility,
                target: targetCompatibility,
                destDir: sourceSets.main.output.classesDir.absolutePath,
                maxmem: "512m",
                fork: "true",
                // Jars containing classes where aspects should be woven into should be declared in the ajInpath configuration
                inpath: configurations.ajInpath.asPath,
                // Jars containing aspects to be woven should be declared in the aspects configuration
                aspectPath: configurations.aspects.asPath,
                Xlint: "ignore",
                sourceRootCopyFilter: "**/*",
                classpath: "${configurations.compile.asPath};${sourceSets.main.output.classesDir};**",
                sourceroots: 'src/main/java/com/my/stuff/aspects'
        )
    }
}

...
processResources.dependsOn(compileAjc)

...



来源:https://stackoverflow.com/questions/35826638/gradle-and-aspectj-avoid-weaving-my-own-packages-during-compile-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!