Hugo Sartori
Hugo Sartori

Reputation: 570

Gradle api transitive dependencies not working

I have a library project that I use to hold some setup for my other projects that has a lot of utility classed and also utility libraries included. So, I changed all my "implementation" calls in the library's build.gradle to "api" calls so I don't need to reimport the dependencies again and again. After building the library and moving the jar from my library folder to the lib folder inside my main project, I can access all the classes in my library, but the transitive dependencies are not available in my main project.

I also tried using implementation and transitive = true, but no luck.

I'm using AdoptOpenJDK 16 and Gradle 7.0 and I already tried to rebuild everything after cleaning the cache.

library's build.gradle

buildscript {
    repositories {
        mavenLocal()
        mavenCentral()
    }
}

plugins {
    id 'java-library'
    id 'idea'
    id 'org.springframework.boot' version '2.5.0'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
}

group = 'com.mygroup'
archivesBaseName = 'utilities'
version = '1.0.2'

bootJar {
    enabled = false
}

jar {
    enabled = true
}

repositories {
    mavenLocal()
    mavenCentral()
}

dependencies {

    // Spring
    api('org.springframework.boot:spring-boot-starter-web')
    api('org.springframework.boot:spring-boot-starter-validation')
    api('org.springframework.boot:spring-boot-starter-logging')
    api('org.springframework.boot:spring-boot-starter-log4j2')
    api('org.springframework.boot:spring-boot-starter-test')
    api('io.springfox:springfox-boot-starter:3.0.0')

    // Utilities
    api('io.sentry:sentry-spring:4.3.0')
    api('joda-time:joda-time:2.10.10')
    api('commons-codec:commons-codec:1.15')
    api('org.apache.commons:commons-lang3:3.12.0')
    api('org.apache.commons:commons-csv:1.8')
    api('org.apache.commons:commons-math3:3.6.1')
    api('org.projectlombok:lombok:1.18.20')
    api('org.springframework.boot:spring-boot-configuration-processor')

    // Annotation Processor
    annotationProcessor('org.projectlombok:lombok:1.18.20')
    annotationProcessor('org.springframework.boot:spring-boot-configuration-processor')

    // Tests
    testAnnotationProcessor('org.projectlombok:lombok:1.18.20')
    testAnnotationProcessor('org.springframework.boot:spring-boot-starter-test')
    testImplementation('org.springframework.boot:spring-boot-starter-test')

}

test {
    useJUnitPlatform()
    systemProperties = System.properties as Map<String, ?>
}

application's build.gradle

buildscript {
    repositories {
        mavenLocal()
        mavenCentral()
        maven {
            url 'https://plugins.gradle.org/m2/'
        }
    }
}

plugins {
    id 'java'
    id 'idea'
    id 'org.springframework.boot' version '2.5.0'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
}

group = 'com.mygroup'
archivesBaseName = 'application'
version = '1.0.2'

bootJar {
    enabled = true
}

jar {
    enabled = false
}

repositories {
    mavenLocal()
    mavenCentral()
    flatDir dirs: 'lib'
}

dependencies {

    // Local
    implementation('com.mygroup:utilities:1.0.2')
    testImplementation('com.mygroup:utilities:1.0.2')
    annotationProcessor('com.mygroup:utilities:1.0.2')
    testAnnotationProcessor('com.mygroup:utilities:1.0.2')

    // Additional dependencies
    implementation('org.springframework.boot:spring-boot-starter-data-mongodb')
    implementation('com.graphql-java:graphql-spring-boot-starter:5.0.2')
    implementation('com.graphql-java:graphiql-spring-boot-starter:5.0.2')
    implementation('com.graphql-java:graphql-java-tools:5.2.4')
    
}

test {
    useJUnitPlatform()
    systemProperties = System.properties as Map<String, ?>
}

Upvotes: 4

Views: 7276

Answers (1)

Sergiusz Pogorzała
Sergiusz Pogorzała

Reputation: 317

Information about transitive dependencies isn't included in Your jar. When You publish libraries to a repository via Maven or Gradle, there are several files being published:

  • obviously .jar file with all compiled code
  • pom.xml file (it contains transitive dependencies definitions)
  • some files with checksums

When You just copy Your library jar to lib directory, Your application has no information about it's dependencies. There are several solutions:

  1. Publish Your library to Maven Repository (Sonatype Nexus or JFrog Artifactory are most popular products to set up self hosted repository, but You can also use mavenLocal()) instead of copying jar to lib - I think it's the best solution

  2. Build library as fatJar (jar file with compiled code and all it's dependencies - Creating a Fat Jar in Gradle)

  3. Copy all of Your library dependencies to lib folder

Upvotes: 7

Related Questions