配置 .jar 以公开其依赖项

Configure .jar to expose its dependencies

我有 3 个模块:annotationsannotation_processorapp

annotations/build.gradle

apply plugin: 'java-library'

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
}

annotation_processor/build.gradle

apply plugin: 'java-library'

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    api project(":annotations")

    api "com.squareup:javapoet:1.11.1"
}

repositories {
    mavenCentral()
}

sourceCompatibility = "1.8"
targetCompatibility = "1.8"

task deleteJar(type: Delete) {
    delete 'libs/annotations.jar'
}

task createJar(type: Copy) {
    from('build/intermediates/bundles/release/')
    into('libs/')
    include('classes.jar')
    rename('classes.jar', 'annotations.jar')
}

createJar.dependsOn(deleteJar, build)

和app/build.gradle

dependencies {
    ...
    implementation files('libs/annotation_processor.jar')
    annotationProcessor files('libs/annotation_processor.jar')
    ...
}

当我 运行 项目时,出现以下错误。

error: package com.annotations does not exist

如果我将 annotations 作为项目包含在项目中,我可以跳过此错误,但随后我得到了。

Caused by: java.lang.NoClassDefFoundError: com/annotations/ProviderApi
at com.annotation_processor.ProviderAnnotationProcessor.getSupportedAnnotationTypes(ProviderAnnotationProcessor.java:45)
at org.gradle.api.internal.tasks.compile.processing.DelegatingProcessor.getSupportedAnnotationTypes(DelegatingProcessor.java:47)
at org.gradle.api.internal.tasks.compile.processing.NonIncrementalProcessor.getSupportedAnnotationTypes(NonIncrementalProcessor.java:33)
at org.gradle.api.internal.tasks.compile.processing.DelegatingProcessor.getSupportedAnnotationTypes(DelegatingProcessor.java:47)
at org.gradle.api.internal.tasks.compile.processing.TimeTrackingProcessor.access1(TimeTrackingProcessor.java:37)
at org.gradle.api.internal.tasks.compile.processing.TimeTrackingProcessor.create(TimeTrackingProcessor.java:68)
at org.gradle.api.internal.tasks.compile.processing.TimeTrackingProcessor.create(TimeTrackingProcessor.java:65)
at org.gradle.api.internal.tasks.compile.processing.TimeTrackingProcessor.track(TimeTrackingProcessor.java:117)
at org.gradle.api.internal.tasks.compile.processing.TimeTrackingProcessor.getSupportedAnnotationTypes(TimeTrackingProcessor.java:65)

我无法从 annotation_processor 访问任何依赖项(Java Poet 和 annotation 模块),即使我已经替换了实施api。哪个应该公开依赖关系,但没有。

我需要 app 模块中 .jar 文件的依赖项。

我是 Java 库的新手,我可能犯了一个基本错误,但似乎无法弄清楚,我已经用了一天多了。

您必须创建一个多模块 gradle 项目。

我提供了一个多模块项目的一小段。

project(':module1') { 
   dependencies {
      compile project(':module-service-api')
      compile 'org.apache.commons:commons-lang3:3.3.2'
      compile 'log4j:log4j:1.2.17'
   }
} 

//module-app depends on module-service-impl
project(':module2') { 
   dependencies {
      compile project(':module1')
   }

关于多模块项目的更多详细信息,请参考以下项目中的文件build.gradle。

https://github.com/debjava/gradle-multi-module-project-1

如果你想创建一个 fat jar 或一个 jar,你必须包含 Gradle shadow 插件,以便你可以将 jar 文件与其他依赖项一起分发。

参考下面link.

https://plugins.gradle.org/plugin/com.github.johnrengelman.shadow