AspectJ + Gradle配置

Kao*_*Kao 10 java aspectj gradle

我想在Gradle项目中使用AspectJ(它不是一个Android项目 - 只是一个简单的Java应用程序).

以下是我的build.gradle的样子:

apply plugin: 'java'

buildscript {
    repositories {
        maven {
            url "https://maven.eveoh.nl/content/repositories/releases"
        }
    }
    dependencies {
        classpath "nl.eveoh:gradle-aspectj:1.6"
    }
}

repositories {
    mavenCentral()
}

project.ext {
    aspectjVersion = "1.8.2"
}

apply plugin: 'aspectj' 

dependencies {
    //aspectj dependencies
    aspectpath "org.aspectj:aspectjtools:${aspectjVersion}"
    compile "org.aspectj:aspectjrt:${aspectjVersion}"
}
Run Code Online (Sandbox Code Playgroud)

代码编译,但方面似乎没有编织.可能有什么不对?

Koi*_*oer 10

我一直在努力解决这个问题,所以这个配置我用得很好.

在您的配置中执行此操作.

configurations {
    ajc
    aspects
    aspectCompile
    compile{
        extendsFrom aspects
    }
}
Run Code Online (Sandbox Code Playgroud)

在依赖项中,使用以下配置.如果您不使用spring fwk,则不需要Spring依赖项.

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)
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我不使用插件我使用ant和aspectj编译器来做到这一点,可能会有一个简单的方法


Maz*_*eye 7

只想为Archie提到的AspectJ添加所谓的"官方"插件.

这是关于如何做的一些gradle脚本示例:

apply plugin: 'java'



sourceCompatibility = '1.8'
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'

if (!hasProperty('mainClass')) {
    ext.mainClass = 'com.aspectz.Main'
}
buildscript {
  repositories {
    maven {
      url "https://plugins.gradle.org/m2/"
    }
  }
  dependencies {
    classpath "gradle.plugin.aspectj:gradle-aspectj:0.1.6"
    //classpath "gradle.plugin.aspectj:plugin:0.1.1"
    //classpath "gradle.plugin.aspectj:gradle-aspectj:0.1.1"
  }
}


ext {
    aspectjVersion = '1.8.5'
}

apply plugin: "aspectj.gradle"


repositories {
    mavenCentral()
}

dependencies {

    testCompile group: 'junit', name: 'junit', version: '4.10'
    compile("log4j:log4j:1.2.16")
    compile("org.slf4j:slf4j-api:1.7.21")
    compile("org.slf4j:slf4j-log4j12:1.7.21")
    compile("org.aspectj:aspectjrt:1.8.5")
    compile("org.aspectj:aspectjweaver:1.8.5")


}
Run Code Online (Sandbox Code Playgroud)

但是,它似乎只支持Java 8及更高版本.当你使用java 7来构建它时,它会出错:

java.lang.UnsupportedClassVersionError: aspectj/AspectJGradlePlugin : Unsupported major.minor version 52.0
Run Code Online (Sandbox Code Playgroud)

  • `if (!hasProperty('mainClass')) { ext.mainClass = 'com.aspectz.Main' }` 这是强制性的吗? (2认同)