Gradle not including dependencies in published pom.xml

前端 未结 7 1898
囚心锁ツ
囚心锁ツ 2020-12-02 06:26

I have a Gradle project I\'m using the maven-publisher plugin to install my android library to maven local and a maven repo.

That works, but the g

相关标签:
7条回答
  • 2020-12-02 06:55

    Kotlin DSL version of the accepted answer:

        create<MavenPublication>("maven") {
            groupId = "com.example"
            artifactId = "sdk"
            version = Versions.sdkVersionName
            artifact("$buildDir/outputs/aar/Example-release.aar")
            pom.withXml {
                val dependenciesNode = asNode().appendNode("dependencies")
                val configurationNames = arrayOf("implementation", "api")
                configurationNames.forEach { configurationName ->
                    configurations[configurationName].allDependencies.forEach {
                        if (it.group != null) {
                            val dependencyNode = dependenciesNode.appendNode("dependency")
                            dependencyNode.appendNode("groupId", it.group)
                            dependencyNode.appendNode("artifactId", it.name)
                            dependencyNode.appendNode("version", it.version)
                        }
                    }
                }
            }
        }
    
    0 讨论(0)
  • 2020-12-02 06:57

    I'm upgraded C.Ross solution. This example will generate pom.xml with dependecies from compile configuration and also with special build type dependecies, for example if you use different dependencies for release or debug version (debugCompile and releaseCompile). And also it adding exlusions

    publishing {
        publications {
            // Create different publications for every build types (debug and release)
            android.buildTypes.all { variant ->
                // Dynamically creating publications name
                "${variant.name}Aar"(MavenPublication) {
    
                    def manifest = new XmlSlurper().parse(project.android.sourceSets.main.manifest.srcFile);
                    def libVersion = manifest['@android:versionName'].text()
                    def artifactName = project.getName()
    
                    // Artifact properties
                    groupId GROUP_ID
                    version = libVersion
                    artifactId variant.name == 'debug' ? artifactName + '-dev' : artifactName
    
                    // Tell maven to prepare the generated "*.aar" file for publishing
                    artifact("$buildDir/outputs/aar/${project.getName()}-${variant.name}.aar")
    
                    pom.withXml {
                        //Creating additional node for dependencies
                        def dependenciesNode = asNode().appendNode('dependencies')
    
                        //Defining configuration names from which dependencies will be taken (debugCompile or releaseCompile and compile)
                        def configurationNames = ["${variant.name}Compile", 'compile']
    
                        configurationNames.each { configurationName ->
                            configurations[configurationName].allDependencies.each {
                                if (it.group != null && it.name != null) {
                                    def dependencyNode = dependenciesNode.appendNode('dependency')
                                    dependencyNode.appendNode('groupId', it.group)
                                    dependencyNode.appendNode('artifactId', it.name)
                                    dependencyNode.appendNode('version', it.version)
    
                                    //If there are any exclusions in dependency
                                    if (it.excludeRules.size() > 0) {
                                        def exclusionsNode = dependencyNode.appendNode('exclusions')
                                        it.excludeRules.each { rule ->
                                            def exclusionNode = exclusionsNode.appendNode('exclusion')
                                            exclusionNode.appendNode('groupId', rule.group)
                                            exclusionNode.appendNode('artifactId', rule.module)
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-02 07:01

    I was able to work around this by having the script add the dependencies to the pom directly using pom.withXml.

    //The publication doesn't know about our dependencies, so we have to manually add them to the pom
    pom.withXml {
        def dependenciesNode = asNode().appendNode('dependencies')
    
        //Iterate over the compile dependencies (we don't want the test ones), adding a <dependency> node for each
        configurations.compile.allDependencies.each {
            def dependencyNode = dependenciesNode.appendNode('dependency')
            dependencyNode.appendNode('groupId', it.group)
            dependencyNode.appendNode('artifactId', it.name)
            dependencyNode.appendNode('version', it.version)
        }
    }
    

    This works for my project, it may have unforeseen consequences in others.

    0 讨论(0)
  • 2020-12-02 07:04

    now that compile is deprecated we have to use implementation.

    pom.withXml {
    def dependenciesNode = asNode().appendNode('dependencies')
        configurations.implementation.allDependencies.each {
        def dependencyNode = dependenciesNode.appendNode('dependency')
        dependencyNode.appendNode('groupId', it.group)
        dependencyNode.appendNode('artifactId', it.name)
        dependencyNode.appendNode('version', it.version)
    }
    
    0 讨论(0)
  • 2020-12-02 07:12

    With gradle 3 implemention was introduced. Replace compile with implementation. Use this instead.

    pom.withXml {
        def dependenciesNode = asNode().appendNode('dependencies')
        configurations.implementation.allDependencies.each {
            def dependencyNode = dependenciesNode.appendNode('dependency')
            dependencyNode.appendNode('groupId', it.group)
            dependencyNode.appendNode('artifactId', it.name)
            dependencyNode.appendNode('version', it.version)
        }
    }
    
    0 讨论(0)
  • 2020-12-02 07:12

    I guess it has something to do with the from components.java directive, as seen in the guide. I had a similar setup and it made the difference to add the line into the publication block:

    publications {
        mavenJar(MavenPublication) {
            artifactId 'rest-security'
            artifact jar
            from components.java
        }
    }
    
    0 讨论(0)
提交回复
热议问题