Gradle和AspectJ - 避免在编译期间编织自己的包

时间:2016-03-06 12:05:12

标签: gradle aspectj gradle-plugin

我正在创建一个与弹簧无关的aspectj项目。所有方面都是我写的。到目前为止,我一直在使用“ant”构建来编译我的代理,现在我正试图进入gradle。

我的问题 - 我不希望对我的类进行检测,但是ajc也编织了我的代码,所以如果我在“someFunction()中添加一个切入点“函数和我的代码 com.myproject.MyClass 有一个名为”someFunction“的函数,它将被检测。

如何在“com.myProject”中将gradle aspectj插件配置为NOT instrument类? (在运行时它是aop.xml文件,但问题出在编译时)。

我的代码:

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"


}

由于

2 个答案:

答案 0 :(得分:1)

如果您的课程都在特殊包裹或其任何子包装内,例如my.company.blah,只需将其添加到您的切入点:

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

答案 1 :(得分:0)

问题解决了:

  1. 首先编译代码
  2. 然后使用特定的类路径和源添加aspectj编译。
  3. 这意味着我没有实现 compileJava ,而是添加了一个名为 compileAjc 的新任务,该任务依赖于compileJava的输出:

    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)
    

    ...