简体   繁体   中英

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

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 (ie JAR)?

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.

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>

If you want to use the awesome solution from Roman Konoval in kotlin gradle build files, you need to specify the path as an URI, example:

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

创建一个 FlatDir 存储库并将您的 jar 放在那里。

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