Gradle和AspectJ—避免在编译时编织自己的包

Gradle和AspectJ—避免在编译时编织自己的包,gradle,aspectj,gradle-plugin,Gradle,Aspectj,Gradle Plugin,我正在创建一个aspectj项目,它与spring无关。所有的方面都是我写的。到目前为止,我一直在使用“ant”build来编译我的代理,现在我正试图迁移到gradle 我的问题-我不想检测我的类,但ajc也会编制我的代码,因此如果我在“someFunction()”函数和我的代码com.myproject.MyClass中添加一个切入点,它将被检测 如何将GradleAspectJ插件配置为不在“com.myProject”中插入类?(在运行时是aop.xml文件,但问题是在编译时) 我的代码

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

我的问题-我不想检测我的类,但ajc也会编制我的代码,因此如果我在“someFunction()”函数和我的代码com.myproject.MyClass中添加一个切入点,它将被检测

如何将GradleAspectJ插件配置为不在“com.myProject”中插入类?(在运行时是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"


}

谢谢

如果您的类都在一个特殊的包或其任何子包中,例如,
my.company.blah
,只需将此添加到切入点:

&&!在(我的公司等等…*)内

问题可通过以下方式解决:

  • 首先编译代码
  • 然后添加带有特定类路径和源的aspectj编译 这意味着我没有实现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)
    

    …谢谢。我不想修改代码,只是为了能够在编译时排除方面,所以这更像是一个gradle配置问题。