AspectJ + Gradle configuration

后端 未结 6 469
执念已碎
执念已碎 2021-02-04 11:31

I\'d like to use AspectJ in Gradle project (it\'s not an Android project - just a simple Java app).

Here is how my build.gradle looks like:

apply plugin         


        
6条回答
  •  走了就别回头了
    2021-02-04 12:35

    I have been struggled with this for a while, so this the config I use and works well.

    In your configuration do this.

    configurations {
        ajc
        aspects
        aspectCompile
        compile{
            extendsFrom aspects
        }
    }
    

    In your dependencies use the following configuration. Spring dependencies are not needed if you are not using spring fwk.

    dependencies {
    
        //Dependencies required for aspect compilation
        ajc "org.aspectj:aspectjtools:$aspectjVersion"
        aspects "org.springframework:spring-aspects:$springVersion"
        aspectCompile  "org.springframework:spring-tx:$springVersion"
        aspectCompile  "org.springframework:spring-orm:$springVersion"
        aspectCompile  "org.hibernate.javax.persistence:hibernate-jpa-2.1-api:$hibernateJpaVersion"
    
    }
    
    compileJava {
        sourceCompatibility="1.7"
        targetCompatibility="1.7"
        //The following two lines are useful if you have queryDSL if not ignore
        dependsOn generateQueryDSL
        source generateQueryDSL.destinationDir
    
        dependsOn configurations.ajc.getTaskDependencyFromProjectDependency(true, "compileJava")
    
        doLast{
            ant.taskdef( resource:"org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties", classpath: configurations.ajc.asPath)
            ant.iajc(source:"1.7", target:"1.7", destDir:sourceSets.main.output.classesDir.absolutePath, maxmem:"512m", fork:"true",
                    aspectPath:configurations.aspects.asPath,
                    sourceRootCopyFilter:"**/.svn/*,**/*.java",classpath:configurations.compile.asPath){
                sourceroots{
                    sourceSets.main.java.srcDirs.each{
                        pathelement(location:it.absolutePath)
                    }
                }
            }
        }
    }
    

    I dont use the plugin I use the ant and aspectj compiler to do that, probably there will be an easy way

提交回复
热议问题