【问题标题】:AspectJ + Gradle configurationAspectJ + Gradle 配置
【发布时间】:2015-10-05 13:11:06
【问题描述】:

我想在 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}"
}

代码可以编译,但是方面似乎没有被编织。有什么问题?

【问题讨论】:

  • 对于 gradle 5,你可以使用这个'official' plugin。在那里你也可以找到一个工作示例。

标签: java gradle aspectj


【解决方案1】:

我已经为此苦苦挣扎了一段时间,所以这是我使用的配置并且效果很好。

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

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编译器来做,可能会有一个简单的方法

【讨论】:

    【解决方案2】:

    只是想为 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")
    
    
    }
    

    不过好像只支持Java 8及以上。当你使用 java 7 构建它时,它得到了错误:

    java.lang.UnsupportedClassVersionError: aspectj/AspectJGradlePlugin : Unsupported major.minor version 52.0
    

    【讨论】:

    • if (!hasProperty('mainClass')) { ext.mainClass = 'com.aspectz.Main' } 这是强制性的吗?
    【解决方案3】:

    看起来 AspectJ 有一个新的“官方”gradle 插件:

    https://plugins.gradle.org/plugin/aspectj.gradle

    不幸的是,文档很少。我自己没试过。

    【讨论】:

    • 插件不再维护,也不再工作(“Could not get unknown property 'classesDir' [...]”):见github.com/eveoh/gradle-aspectj/commit/…
    • 这些天我尝试 aovid gradle。只是我的意见。
    • 是的,我也会。不幸的是,我必须在工作中使用它:(
    【解决方案4】:

    这个plugin 为我工作(gradle 5.6):

    plugins {
        id 'java'
        id "io.freefair.aspectj.post-compile-weaving" version "4.1.4"
    }
    

    【讨论】:

      【解决方案5】:

      有点难看,但很短,不需要额外的插件或配置。 为我工作:

      在 Gradle 构建脚本中添加 aspectjweaver 依赖项。然后在你需要它的任务中找到它的 jar 的路径,并在 jvmArgs 中作为 javaagent 传递:

      dependencies {
          compile "org.aspectj:aspectjweaver:1.9.2"
      }
      
      test {
          group 'verification'
      
          doFirst {
              def weaver = configurations.compile.find { it.name.contains("aspectjweaver") }
              jvmArgs = jvmArgs << "-javaagent:$weaver"
          }
      }
      

      aop.xmlfile 添加到具有指定Aspect 类的resources\META-INF\ 文件夹中:

      <aspectj>
          <aspects>
              <aspect name="utils.listener.StepListener"/>
          </aspects>
      </aspectj>
      

      方面的示例:

      package utils.listener
      
      import org.aspectj.lang.JoinPoint
      import org.aspectj.lang.annotation.*
      import org.aspectj.lang.reflect.MethodSignature
      
      @Aspect
      @SuppressWarnings("unused")
      public class StepListener {
      
          /* === Pointcut bodies. Should be empty === */
          @Pointcut("execution(* *(..))")
          public void anyMethod() {
          }
      
          @Pointcut("@annotation(org.testng.annotations.BeforeSuite)")
          public void withBeforeSuiteAnnotation() {
          }
      }
      

      【讨论】:

        【解决方案6】:

        在我的 build.gradle 文件中添加它对我有用。

        project.ext {
          aspectjVersion = '1.8.12'
        }
        
        apply plugin: "aspectj.gradle"
        
        buildscript {
          repositories {
            maven {
              url "https://plugins.gradle.org/m2/"
            }
          }
          dependencies {
            classpath "gradle.plugin.aspectj:gradle-aspectj:0.1.6"
          }
        }
        

        执行gradle assemble 注入了方面中定义的代码。

        【讨论】:

          猜你喜欢
          • 2014-06-04
          • 1970-01-01
          • 2012-04-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-10-23
          • 1970-01-01
          • 2015-09-30
          相关资源
          最近更新 更多