简体   繁体   中英

Gradle dependency resolution strategy with maven deployer

I am working on an android project. We are using the DependencyResoultionStrategy to swap some dependency versions. The code looks like this:

resolutionStrategy.eachDependency { DependencyResolveDetails details ->
    final version = getVersionForDependency(project, details.requested.group, details.requested.name)
    if (version != null) {
        details.useVersion(version)
    }
}

So for example, the project requests the dependency group:name:1.1.2 but it is swapped so the dependency group:name:1.2.0 is used. This works perfectly and the project is built with the right dependency (the second one).

We also have a publish task, which deploys the project to a local maven repository. We use the maven plugin for this, the code looks like this:

apply plugin: 'maven'

task publish(dependsOn: uploadArchives)

uploadArchives {
    configurations {
        deployerFTP
    }
    repositories {
        mavenDeployer {
            configuration = configurations.deployerFTP
            repository(URL) {
                authentication(USERNAME, PASSWORD)
            }
        }
    }
    dependencies {
        deployerFTP "org.apache.maven.wagon:wagon-ftp:2.4"
    }
}

The problem is, if I publish the library, in the resulting .pom file, the dependency group:name:1.1.2 is entered, not the one which is actually used. How can I change this behavior, so the pom contains the right dependency?

I have found an answer, simply add this code block:

mavenDeployer {
    // ...
    pom.whenConfigured { pom ->
        pom.dependencies = pom.dependencies.collect { dep ->
            def version = getVersionForDependency(project, dep.groupId, dep.artifactId)
            if (version != null) {
                dep.version = version
            }
            return dep
        }
    }
}

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