我想在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}"
}
代码编译,但方面似乎没有编织.可能有什么不对?
解决方法
我一直在努力解决这个问题,所以这个配置我用得很好.
在您的配置中执行此操作.
configurations {
ajc
aspects
aspectCompile
compile{
extendsFrom aspects
}
}
在依赖项中,使用以下配置.如果您不使用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)
}
}
}
}
}
我不使用插件我使用ant和aspectj编译器来做到这一点,可能会有一个简单的方法