我已经把这个的MCVE代码放在GitHub-https://github.com/ravitechy/multi-module-project/tree/main上了

我的Java项目(基于Gradle构建)中有两个模块--app,它依赖于commons.

commons包含如下定义的特征类BasicCommonAspect

package org.example.commons.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

import javax.annotation.PostConstruct;

@Aspect
@EnableAspectJAutoProxy
public class BasicCommonAspect {
           
    @Before("@annotation(bca) && execution(* *(..))")
    public void executeBeforeForCommonAnnotation(JoinPoint joinPoint, BasicCommonAnnotation bca) {
        System.out.println("BasicCommonAnnotation detected");
    }
}

commons还包含如下所示的注释BasicCommonAnnotation

package org.example.commons.aspect;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface BasicCommonAnnotation {
}

app包含AppBackground,并具有用BasicCommonAnnotation注释的方法run

package org.example;

import org.example.commons.aspect.BasicCommonAnnotation;

public class AppBackground {

    @BasicCommonAnnotation
    public void run() {
        System.out.println("Running in background");
    }

}

app模块中有一个名为RunApp的驱动程序类,它调用AppBackground中的run方法

package org.example;

public class RunApp {

    public static void main(String[] args) {
        final AppBackground background = new AppBackground();
        background.run();
    }
}

我使用编译后编织(使用Gradle插件io.freefair.aspectj.post-compile-weaving),因为我需要使用Project Lombok.

当我运行RunApp类时,我的目标是看到方法BasicCommonAspect.executeBeforeForCommonAnnotation()按照定义的建议执行.

从我的初步研究中,我知道我需要在apps模块中包括外部模块作为方面库.我在这里找到了完全相同问题的Stack Overflow答案-AspectJ - Aspect from external JAR,但它是针对Maven构建的.我正在寻找与我的Gradle项目完全相似的配置.

我try 使用inpath,就像这里建议的https://github.com/freefair/gradle-plugins/issues/450,但我现在开始遇到不同的构建错误,如下所示.

Not skipping AJC (inpath is set)
Starting process 'command 'D:\OpenJdk17\bin\java.exe''. Working directory: D:\intellij-idea workspace\multi-module-project\app Command: D:\OpenJdk17\bin\java.exe -Dfile.encoding=windows-1252 -Duser.country=IN -Duser.language=en -Duser.variant -cp C:\Users\raviteja.kothapalli\.gradle\caches\modules-2\files-2.1\org.aspectj\aspectjtools\1.9.8\caf8b7f9023d93af2ed2e6e8bf023a800e9d1323\aspectjtools-1.9.8.jar org.aspectj.tools.ajc.Main -argfile D:\intellij-idea workspace\multi-module-project\app\build\tmp\compileJava\ajc.options
Successfully started process 'command 'D:\OpenJdk17\bin\java.exe''
←[0K
←[0K
←[2A←[1m<←[0;32;1m===←[0;39;1m----------> 25% EXECUTING [18s]←[m←[35D←[1B←[1m> :app:compileJava←[m←[18D←[1B←[2A←[1m<←[0;32;1m===←[0;39;1m----------> 25% EXECUTING [19s]←[m←[35D←[2B←[2A←[1m<←[0;32;1m===←[0;39;1m----------> 25% EXECUTING [20s]←[m←[35D←[2B←[2A←[1m<←[0;32;1m===←[0;39;1m----------> 25% EXECUTING [21s]←[m←[35D←[2B←[2A←[1m<←[0;32;1m===←[0;39;1m----------> 25% EXECUTING [22s]←[m←[35D←[2B←[2A←[1m<←[0;32;1m===←[0;39;1m----------> 25% EXECUTING [23s]←[m←[35D←[2B←[2AC:\Users\raviteja.kothapalli\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-autoconfigure\2.7.18\9cf147c6ca274c75b32556acdcba5a1de081ebcd\spring-boot-autoconfigure-2.7.18.jar [error] can't determine implemented interfaces of missing type org.springframework.web.server.WebExceptionHandler
when weaving type org.springframework.boot.autoconfigure.web.reactive.error.AbstractErrorWebExceptionHandler
when weaving classes
when weaving
when batch building BuildConfig[null] #Files=0 AopXmls=#0
 [Xlint:cantFindType]
(no source information available)
        [Xlint:cantFindType]
←[0K
←[0K
←[2A←[1m<←[0;32;1m===←[0;39;1m----------> 25% EXECUTING [23s]←[m←[35D←[1B←[1m> :app:compileJava←[m←[18D←[1B←[2A←[1m<←[0;32;1m===←[0;39;1m----------> 25% EXECUTING [24s]←[m←[35D←[2B←[2AC:\Users\raviteja.kothapalli\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot\2.7.18\f6dbdd8da7c2bded63dff9b1f48d01a4923f20a0\spring-boot-2.7.18.jar [error] can't determine implemented interfaces of missing type javax.servlet.Filter
when weaving type org.springframework.boot.web.servlet.filter.OrderedHiddenHttpMethodFilter
when weaving classes
when weaving
when batch building BuildConfig[null] #Files=0 AopXmls=#0
 [Xlint:cantFindType]
(no source information available)
        [Xlint:cantFindType]
C:\Users\raviteja.kothapalli\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot\2.7.18\f6dbdd8da7c2bded63dff9b1f48d01a4923f20a0\spring-boot-2.7.18.jar [error] can't determine implemented interfaces of missing type io.undertow.server.HttpHandler
when weaving type org.springframework.boot.web.embedded.undertow.UndertowWebServer$CloseableHttpHandlerFactory$1
when weaving classes
when weaving
when batch building BuildConfig[null] #Files=0 AopXmls=#0
 [Xlint:cantFindType]
(no source information available)
        [Xlint:cantFindType]
C:\Users\raviteja.kothapalli\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot\2.7.18\f6dbdd8da7c2bded63dff9b1f48d01a4923f20a0\spring-boot-2.7.18.jar [error] can't determine implemented interfaces of missing type org.springframework.web.server.WebFilter
when weaving type org.springframework.boot.web.reactive.filter.OrderedHiddenHttpMethodFilter
when weaving classes
when weaving
when batch building BuildConfig[null] #Files=0 AopXmls=#0
 [Xlint:cantFindType]
(no source information available)
        [Xlint:cantFindType]

因此,我所理解的是,为了克服这个问题,我只需要包括来自inpath中特定包的方面类,但我似乎找不到这样做的配置.

请帮助我提供必要的配置,以包括来自外部JAR/模块的方面,仅来自特定的包.

apps模块中的build.gradle个文件

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

java.sourceCompatibility = JavaVersion.VERSION_17
java.targetCompatibility = JavaVersion.VERSION_17

repositories {
    mavenCentral()
}

dependencies {
//    api project(":commons")
    inpath project(":commons")
    api libs.spring.boot.starter
    annotationProcessor libs.lombok
    implementation libs.aspectjrt
}

build.gradle in commons模块

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

java.sourceCompatibility = JavaVersion.VERSION_17
java.targetCompatibility = JavaVersion.VERSION_17

repositories {
    mavenCentral()
}

dependencies {
    api libs.spring.boot.starter
    annotationProcessor libs.lombok
    implementation libs.aspectjrt
}

root英寸settings.gradle英寸

rootProject.name = 'multi-module-project'
include('app')
include('commons')

dependencyResolutionManagement {
    versionCatalogs {
        libs {
            version('spring-boot-ver', '2.7.18')
            library('lombok', 'org.projectlombok:lombok:1.18.30')
            library('spring-boot-starter', 'org.springframework.boot', 'spring-boot-starter').versionRef('spring-boot-ver')
            library('aspectjrt','org.aspectj:aspectjrt:1.9.8')
        }

        testLibs {
            library('junit-jupiter-api','org.junit.jupiter:junit-jupiter-api:5.8.1')
            library('junit-jupiter-engine', 'org.junit.jupiter:junit-jupiter-engine:5.8.1')
        }
    }
}

root英寸build.gradle英寸

rootProject.ext {
    projectGroup = 'org.example'
    projectVersion = '1.0.0-SNAPSHOT'
    aspectjVersion = '8.4'
}

附注:如果所有的类、注释和方面都在一个单独的模块中,那么一切都会按预期进行.

推荐答案

问题出在您的应用程序模块中的inpath project(":commons").如果您想要将方面编织到第三方模块中,则可以使用inpath.后者将在inpath号公路上行驶.但在本例中,您只需要告诉插件在哪里查找要编织到当前模块中的方面.然而,方面库属于aspectpath.因此,您需要将行更改为aspect project(":commons").这相当于从my answer到您已经找到的相应Maven问题的<aspectLibrary/>

我已经为你的项目创建了一个pull request,解决了这个问题,并改进了其他一些东西.只需逐一查看提交差异和注释.

现在,您可以构建和运行您的应用程序了.控制台日志(log)显示:

> Task :app:RunApp.main()
BasicCommonAnnotation detected
Running in background

也就是说,方面如预期那样被触发.

Java相关问答推荐

Java 21虚拟线程会解决转向react 式单线程框架的主要原因吗?

强制Mockito返回null而不是emtpy list

Javascript在边界中心调整ImageView大小

Java模式匹配记录

Character::Emoji不支持带数字的字符吗?

Junit with Mockito for java

Java .类参数不通过构造函数传递

Java中如何根据Font.canDisplay方法对字符串进行分段

如何正确创建序列图?

Spring和可编辑";where";@Query

测试何时使用Mockito强制转换对象会导致ClassCastException

如何在Spring Boot中创建可以将值传递给配置的&Enable&Quot;注释?

使用SWIG将C++自定义单元类型转换为基本Java类型

在打开搜索结果时,如何让Eclipse打开整个文件?

当使用不同的参数类型调用时,为什么围绕Objects.equals的类型安全包装不会失败?

JOOQ:批处理CRUD操作使用动态表定义,如何?

@此处不能应用可为null的批注

在外部类和内部类之间,当调用外部类内部或外部的主方法时,它们的静态初始化程序的运行顺序不同

语句打印在错误的行(Java Token 问题)

ChromeDriver 版本 117+ 强制另存为对话框 - 如何绕过? (selenium/Java)