本文介绍了Gradle和AspectJ-避免在编译时编织我自己的包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个aspectj项目,该项目与spring无关.所有方面都是我写的.到目前为止,我一直在使用"ant"构建来编译我的代理,而现在我正试图发展.

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.

我的问题-我不希望检测我的类,但是ajc也会编织我的代码,因此如果我在"someFunction()中添加切入点"函数和我的代码 com.myproject.MyClass 有一个名为"someFunction" 的函数,将对其进行检测.

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.

如何将gradle Aspectj插件配置为"com.myProject"中的非工具类? (在运行时是aop.xml文件,但问题出在编译时).

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).

我的代码:

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"


}

谢谢

推荐答案

问题由以下解决:

  1. 首先编译代码
  2. 然后使用特定的类路径和源添加Aspectj编译.

这意味着我没有执行 compileJava ,而是添加了一个新的名为 compileAjc 的任务,该任务取决于compileJava的输出:

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)

...

这篇关于Gradle和AspectJ-避免在编译时编织我自己的包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 04:06