简体   繁体   中英

Configure .jar to expose its dependencies

I have 3 modules: annotations , annotation_processor and app .

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)

and app/build.gradle

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

When I run the project I get the following error.

error: package com.annotations does not exist

If I include annotations as a project the project I can skip this error but then I get.

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.access$101(TimeTrackingProcessor.java:37)
at org.gradle.api.internal.tasks.compile.processing.TimeTrackingProcessor$2.create(TimeTrackingProcessor.java:68)
at org.gradle.api.internal.tasks.compile.processing.TimeTrackingProcessor$2.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)

I can't access any of the dependencies (Java Poet and annotation module) from annotation_processor even though I have replaced implementation with api . Which should've expose the dependencies, but haven't.

I need the dependencies of the .jar file in the app module.

I'm new to Java Library and it may be I'm making a basic mistake, but can't seem to figure out, I've been at it for more than a day now.

You have to create a multi module gradle project.

I provide a small snippet of a multi module project.

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')
   }

For more details about multi module project, refer to the file build.gradle in the following project.

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

If you want to create a fat jar or one jar, you have to include Gradle shadow plugin so that you can distribute the jar file along with other dependencies.

Refer below the link.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM