简体   繁体   中英

Including transient dependencies with Gradle Module Metadata

I've run into an issue while using the maven-publish plugin for this Android library I'm developing. I've looked at similar posts on SO and other forums as well, and my issue is still persisting.

scenario:

I have Library A which includes the core of the library, Library B which is a wrapper around Library A . So B depends on A . B also has several other dependencies on its own.

issue:

I need to be able to include only Library B and have all the required (api/compile) transient dependencies be downloaded along with it.

what i've tried :

(1) I read up here on how Gradle populates the generated pom.xml file with the dependencies required by looking at the dependencies you've declared in the dependencies {} block. api means the dependency should be transient and implementation hides it (prevents leakage). So I went ahead and applied the changes needed to Library B 's gradle.build and marked all the dependencies I wanted with api. This did not work and the generated pom.xml still did not contain the transient deps.

(2) I've followed the steps here and thought it's now generating the pom.xml with all the dependencies, after publishing it to mavenLocal and attempting to consume it in a fresh Demo app the project is building but it crashes as soon as it attempts to run (due to the missing dependencies).

code:

pom.withXml {
    // for dependencies and exclusions
    def dependenciesNode = asNode().appendNode('dependencies')

    //Iterate over the compile dependencies (we don't want the test ones), adding a <dependency> node for each
    configurations.api.allDependencies.each { dp ->
        def dependencyNode = dependenciesNode.appendNode('dependency')
        dependencyNode.appendNode('groupId', dp.group)
        dependencyNode.appendNode('artifactId', dp.name)
        dependencyNode.appendNode('version', dp.version)
        // for exclusions
        if (dp.excludeRules.size() > 0) {
            def exclusions = dependencyNode.appendNode('exclusions')
            dp.excludeRules.each { ExcludeRule ex ->
                def exclusion = exclusions.appendNode('exclusion')
                exclusion.appendNode('groupId', ex.group)
                exclusion.appendNode('artifactId', ex.module)
            }
        }
    }
} 

The only way I've gotten it to work so far was by also adding all the other deps on top of Library B (needless to say this is not ideal...)

QUESTION: How can I achieve what I need while only adding this one library?

After getting some much needed sleep I saw the world (Google) with a fresh pair of eyes and stumbled upon the following notation:

implementation ('Library B') {
    transitive = true // this tells your gradle to fetch the transient dependencies related to 'Library B'
}

Note: If your dependency needs the @aar at the end of it (specifies only the.aar artifact should be downloaded), then by default you will not get any transient dependencies of that library, in which case you would need the solution I stated above. Otherwise, you can omit the transitive = true and as long as the dependencies are written to the publication pom.xml file prior to publishing it you should get all the transients.

After applying this everything is back to working as expected, thought I did read that Gradle defaults this transitive property to true... so I'm still confused as to why I needed to specify it.

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