Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I replace a gradle module dependency with a jar?

Tags:

java

gradle

I have a multi-project build and one or more sub-projects have a dependency on a broken module. I would like to replace the module dependency with a dependency on a local JAR file.

I've tried the following (which I didn't really expect to work), but beyond this am at a bit of a loss:

configurations.all {
    resolutionStrategy {
        failOnVersionConflict()

        eachDependency { DependencyResolveDetails details ->
            if (details.requested.group == "com.android.support" && details.requested.name == "support-v4") {
                details.useTarget("lib/android-support-v4-fixed.jar")
            }
        }
    }
}

How can I replace a module dependency in a subproject with a dependency on a file (i.e. JAR)?

like image 807
Benjamin Dobell Avatar asked Dec 09 '22 11:12

Benjamin Dobell


2 Answers

In the end I crawled through a lot of documentation and was able to achieve what I wanted quite simply.

configurations.all {
    exclude group: 'com.android.support', module: 'support-v4'
}

dependencies {
    compile files('libs/support-v4.jar')
}

The above will exclude a maven dependency for all modules (works fine with libraries etc.), in this case we've excluded the Android support library. Then I simply add my own support library (with several bug fixes) as a regular jar dependency.

like image 105
Benjamin Dobell Avatar answered Dec 11 '22 08:12

Benjamin Dobell


You can do this by adding local repository like this:

configure(allprojects) {
    repositories {                                                                                                         
      maven {
          url "../repo"  // this is relative to subproject, so assumes all subprojects are in the same folder
      }
      
      // ... other repositories

}

The fixed jar should be put to repo dir using mvn folder structure. For you case two files should be added to repo/com/android/support/support-v4/r6-fixed:

support-v4-r6-fixed.jar
support-v4-r6-fixed.pom

Note: files should be named like <artifactId>-<version>.<ext> and this should match values in pom.

The pom should be a valid pom meta with content like (create it using existing pom for broken version)

<?xml version="1.0" encoding="UTF-8"?>                                                                                                                                 
    <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.android.support</groupId>
      <artifactId>support-v4</artifactId>
      <version>r6-fixed</version>
    </project>
like image 21
Roman Konoval Avatar answered Dec 11 '22 10:12

Roman Konoval