DaveH
DaveH

Reputation: 7335

Gradle Dependency Management

I am building a jar file that, at compile time, depends on 3 logback artefacts ( log-back-code, log-back-classic, logback-extensions )

These are all declared with compile scope.

My jar builds, but when I try to use it in a different project, I find I need to specify the 3 logback dependencies again or else the classes are not found. I was expecting these dependencies to have been fulfilled by my declaring an dependency on my jar.

I'm new to gradle and working with existing scripts that I have only the sketchiest of understanding of.

EDIT - Added some details of the dependency config

In the project which builds the jar file, the dependencies are declared as follows

compile group: 'ch.qos.logback',name: 'logback-classic', version: '1.2.1' compile group: 'ch.qos.logback',name: 'logback-core', version: '1.2.1' compile group: 'org.logback-extensions',name: 'logback-ext-spring', version: '0.1.4'

The jar file that I a produce is declared in the "other" projects dependencies as compile group: 'mygroup',name: 'mylogback', version: '1.0.0-SNAPSHOT'

This dependency is pulled from my local maven repository, while logback dependencies come from a nexus repo.

Further Edit The dependency tree of the other project has this, which would appear to support my view that the project is not bringing its dependencies with it ....

> |    +--- commons-io:commons-io:2.5 |    \---
> commons-collections:commons-collections:3.2.2
> +--- mygroup:mylogback:1.0.0-SNAPSHOT
> +--- ch.qos.logback:logback-classic:1.2.1 |    +--- ch.qos.logback:logback-core:1.2.1 |    \--- org.slf4j:slf4j-api:1.7.22
> +--- ch.qos.logback:logback-core:1.2.1
> +--- org.logback-extensions:logback-ext-spring:0.1.4 |    \--- ch.qos.logback:logback-classic:1.1.1 -> 1.2.1 (*)

Resolution

To answer my own question, the problem was due to

Upvotes: 0

Views: 273

Answers (2)

Jazzwave06
Jazzwave06

Reputation: 1851

Apply the dependency to all subprojects.

subprojects {
    dependencies {
        // ...
    }
}

There's also a whole api to be able to select specific project and apply configurations to them.

configure([project(":a"), project(":b"), project(":c")]) {
    dependencies {
        // ...
    }
}

configure(subprojects.findAll { it.name.startsWith("prefix") }) {
    dependencies {
        // ...
    }
}

Also, if you want transitive dependencies to work, you need to add inter-project dependencies via gradle.

dependencies {
    compile project(":toolbox")
}

Upvotes: 0

Jocce Nilsson
Jocce Nilsson

Reputation: 1748

I get the feeling that your problem is in the deployment of your artefacts since it compiles OK.

Make sure that your deployment/upload definition is in the same format as the target artefact repository (local maven in your case, right?). You can check this by looking in the destination folder for pom files (or ivy files depending on your definition) and check that it contains the details in the correct format.

Does your definition look like this? :

apply plugin: 'maven'

uploadArchives {
    repositories {
        mavenDeployer {
            repository(url: "file://localhost/tmp/myRepo/")
        }
    }
}

See more details in The Gradle documentation on maven plugin

More on the publishing can be read in Gradle documentation on publishing and more on dependency resolution and patterns in general can be found at Gradle documentation on dependency management

Upvotes: 1

Related Questions