使用Gradle将编织后的方面编织到项目中

问题描述 投票:1回答:1

背景

使用以下项目执行项目的编译后编织:

  • AspectJ 1.9.4
  • io.freefair.aspectj.post-compile-weaving 4.1.1
  • Java 11.0.3
  • 等级5.6.2(Groovy 2.5.4,Kotlin 1.3.41)

此项目不使用Maven或Spring。

布局

项目包括:

  • [app.aspects-包含单个用LogAspect注释的@Aspect类。
  • app.aspects.weaver-没有源文件,只有依赖关系来声明方面和要编织的项目。
  • [app.common-定义由@Log中描述的切入点引用的LogAspect批注。
  • [app.program.main-用LogAspect中所述的结合点编织的文件。

等级

这里定义了与方面相关的构建文件。想法是编织与应用程序无关,因此应用程序的公共类和主程序都不需要了解编织。相反,主程序只需要从通用包中引用@Log,AJC将负责编织。

app.aspects

apply plugin: "io.freefair.aspectj.post-compile-weaving"

dependencies {
    // For the @Log annotation
    compileOnly project(':app.common')

    // The LogAspect's joinpoint references the Main Program
    compileOnly project(':app.program.main')

    // Logging dependency is also compiled, but not shown here
}

app.aspects.weaver

apply plugin: "io.freefair.aspectj.post-compile-weaving"

dependencies {
    compileOnly "org.aspectj:aspectjrt:1.9.4"

    // This should set the -aspectpath ?
    aspect project(":app.aspects")

    // This should set the -inpath ?
    inpath(project(":app.program.main")) {
        // Only weave within the project
        transitive = false
    }
}

班级

日志

Log注释很简单:

package com.app.common.aspects;

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD, ElementType.TYPE, ElementType.CONSTRUCTOR })
public @interface Log {
    boolean secure() default false;
}

主程序

主程序类似于:

package com.app.program.main;

import com.app.common.aspects.Log;

@Log
public class Program {

  /** This is the method to weave. */
  protected void run() throws InterruptedException, TimeoutException {
  }
}

记录方面

日志记录方面类似(请参阅去年related question中的代码):

@Aspect
public class LogAspect {

    // In the future this will target points annotated with @Log
    @Pointcut("execution(* com.app.program.main.Program.run(..))")
    public void loggedClass() {
    }

    @Around("loggedClass()")
    public Object log(final ProceedingJoinPoint joinPoint) throws Throwable {
      return log(joinPoint, false);
    }

    private Object log(final ProceedingJoinPoint joinPoint, boolean secure) throws Throwable {
      // See last year's code for the full listing
      log.info("\u21B7| {}{}#{}({})", indent, className, memberName, params);
    }
}

问题

似乎正在织布,但找不到建议:

... / app.aspects / build / classes / java / main!com / app / aspects / LogAspect.class com.app.aspects.LogAspect中定义的[警告]建议尚未应用[Xlint:adviceDidNotMatch]] >

问题

需要进行哪些更改,以便使用Gradle将LogAspect编织到Programrun()方法中?

选项文件

ajc.options文件显示:

-inpath
.../app.aspects/build/classes/java/main
-classpath
.../.gradle/caches/modules-2/files-2.1/org.aspectj/...
-d
.../app.aspects/build/classes/java/main
-target
11
-source
11

令人不安的是,未显示-aspectpath,并且-inpath列出了app.aspects而不是app.program.main

背景使用以下项目执行项目的编译后编织:AspectJ 1.9.4 io.freefair.aspectj。编译后编织4.1.1 Java 11.0.3 Gradle 5.6.2(Groovy 2.5.4,Kotlin 1.3.41)项目确实...

java gradle aop aspectj
1个回答
0
投票

apps.aspectsapps.aspects.weaver合并到同一项目中:

© www.soinside.com 2019 - 2024. All rights reserved.