java AspectJ + Gradle 配置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32949541/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 21:00:58  来源:igfitidea点击:

AspectJ + Gradle configuration

javagradleaspectj

提问by Kao

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

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

Here is how my build.gradle looks like:

这是我的 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}"
}

The code compiles, however the aspect seems to not be weaved. What could be wrong?

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

采纳答案by Koitoer

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.

在您的依赖项中使用以下配置。如果您不使用 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)
                }
            }
        }
    }
}

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

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

回答by MazBeye

Just want to add the so called "official" plugin for AspectJ mentioned by Archie.

只是想为Archie提到的AspectJ添加所谓的“官方”插件。

Here's some gradle script example on how to do it:

这是有关如何执行此操作的一些 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")


}

However, it seems that it only supports Java 8 and above. As when you use java 7 to build it, it got error :

但是,它似乎只支持 Java 8 及更高版本。当您使用 java 7 构建它时,出现错误:

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

回答by Archie

Looks like there is a new "official" gradle plugin for AspectJ:

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

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

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

Unfortunately the documentation is minimal. I haven't tried it myself.

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

回答by BohdanN

A bit ugly, but short and does not require additional plugins or configurations. Works for me:

有点丑,但很短,不需要额外的插件或配置。对我有用:

Add aspectjweaverdependency ti Gradle build script. Then in the task you need it find the path to its jar and pass as javaagentin jvmArgs:

添加aspectjweaver依赖项 ti Gradle 构建脚本。然后在任务,你需要找到它的路径,它的罐子,并通过如javaagentjvmArgs

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"
    }
}

Add aop.xmlfile to the resources\META-INF\folder with the specified Aspect class:

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

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

Example of an aspect:

一个方面的例子:

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() {
    }
}

回答by Ohad Y

This pluginworked for me (gradle 5.6):

这个插件对我有用(gradle 5.6):

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

回答by Erwin Alberto

Adding this in my build.gradle file worked for me.

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

Doing a gradle assembleinjected the code defined in the aspect.

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

回答by Vitali Zarembouski

For gradle 5 u can use this 'official' plugin. there you can find a working example too.

对于 gradle 5,你可以使用这个“官方”插件。在那里你也可以找到一个工作示例。