Thibaut D.
Thibaut D.

Reputation: 2663

Gradle / Maven: How to use in-developement dependencies?

Context:

I'm working on a Gradle project which depends on external libraries, also created by me. Currently I was usign a simple dependecy on project(':lib').

But this is not enough anymore, I need to release and distribute libraries as standalone components, versionned and documented. I will install Apache Archiva and publish them to an internal maven repository, so I can depend explicitly on 'com.company:lib:1.0'.

Question:

During developement, I will work on libraries and on projects at the same time. How can I test my code without publishing the libraries ? My application which used to depend on project() will now depend on a specific version. But while developing, I would like to use the local code.

Do you know what is the best process to handle this ?

Upvotes: 0

Views: 225

Answers (2)

Thibaut D.
Thibaut D.

Reputation: 2663

The correct answer to this question is available in Gradle's documentation (section 23.8.3.1), and is the following:

configurations.all {
    resolutionStrategy.dependencySubstitution {
        substitute module("com.company:lib") with project(":lib")
        substitute module("com.company:lib2") with project(":lib2")
    }
}

Personally, I used the following code, which makes all -SNAPSHOT versions to be taken from local projects instead of remote repository (if a local project is available):

configurations.all {
    resolutionStrategy.dependencySubstitution {
        all { dependency ->
            if (! dependency.requested.version.endsWith('SNAPSHOT'))
                return
            if (subprojects.find { p ->p.name == dependency.requested.module })
                dependency.useTarget project(":" + dependency.requested.module)
        }
    }
}

Upvotes: 0

JBirdVegas
JBirdVegas

Reputation: 11413

One way would be to add the dependency conditionally. So for your local builds (IDE) you want to build the dependency via source. Then you can distinguish your release builds by having your releases pass a param to a build.

dependencies {
    if (project.hasProperty('release')) {
        compile 'com.company:lib:1.0'
    } else {
        compile project(':lib')
    }
}

Then in your release builds to use the lib from nexus:
$ gradle -Prelease=true clean build

If you want to build the project with the lib from inside the project:
$ gradle clean build

Upvotes: 2

Related Questions