如何将我的自定义注释处理器 jar 文件包含到我的其他 gradle 项目中?

How to include my custom annotation processor jar file to my other gradle project?

我正在编写一个简单的 java 应用程序,其中一些 类 如果我使用我的自定义 @Log 注释进行注释,则应打印消息“在 <[=27= 处找到@Log 注释” ]>”。我正在尝试使用自定义注释处理器进行试验。为此,我编写了第二个项目,其中将包含我的自定义注释及其处理器,如下所示:

@SupportedAnnotationTypes ("Log")
@AutoService({Processor.class})
public class LogProcessor extends AbstractProcessor {
    @Override
    public SourceVersion getSupportedSourceVersion() {
        return SourceVersion.latestSupported();
    }

    @Override
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
        for (TypeElement typeElement : annotations) {
            for (Element element : roundEnv.getElementsAnnotatedWith(typeElement)) {
                processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "found @Log annotation at " + element);
            }
        }
        return true;
    }
}

而我自定义的@Log注解代码如下:

@Target (ElementType.TYPE)
public @interface Log {}

我正在使用 @AutoService 注册注解处理器。如何将 运行 生成的 .jar 文件包含在 gradle 版本中?我尝试了以下方式:

repositories {
    mavenCentral()
    flatDir {
        dirs 'libs'
    }
}

dependencies {
    annotationProcessor name:  'AnnotationSupplier-1.0-SNAPSHOT'
}

dependencies {
    annotationProcessor files('/libs/AnnotationSupplier-1.0-SNAPSHOT.jar')
}

但这不起作用,我不确定还有什么方法可以添加注释处理器 jar。我的期望是,在 运行 添加了我的 AnnotationSupplier-1.0-SNAPSHOT 的项目后,我应该能够在控制台中看到“found @Log annotation at ”,但我没有。

您可以试试这个作为解决方法:

implementation files('/path/to/custom-processor.jar')

annotationProcessor files('/path/to/custom-processor.jar')

对我来说,通过在父项目下添加注释处理器模块(通过创建一个新模块)并在父项目中添加以下内容来解决问题 build.gradle:

dependencies {
    annotationProcessor project(':AnnotationProcessor')
    compile project(':AnnotationProcessor')
}

注1:AnnotationProcessor是我在父项目下创建的注释处理器模块的名称。

注意 2:我在 IntelliJ 中这样做 IDE。

注意3:确保为项目激活注释处理