使用gradle maven-publish插件导出的Kotlin Android Library Module不添加依赖项

时间:2017-05-15 12:17:40

标签: android maven android-gradle kotlin maven-publish

我正在尝试使用gradle的maven-publish plugin导出在Kotlin中开发的Android库模块。 aar文件已成功生成并导出但是没有添加任何依赖 - 根本不是Kotlin文件。以下是基于this答案的插件配置:

apply plugin: 'maven-publish'

Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())

ext {
    releaseRepository = "s3://****/maven/releases"
    snapshotsRepository = "s3://****/maven/snapshots"
    artifactRepository = "$buildDir/outputs/aar/render-engine-release.aar"

    _version = properties.getProperty('lib.version')
    _artifact = properties.getProperty('lib.name')
    _group = properties.getProperty('lib.group')
}


task sourceJar(type: Jar) {
    from android.sourceSets.main.java.srcDirs
    classifier "source"
}

publishing {
    publications {
        maven(MavenPublication) {
            groupId _group
            artifactId _artifact
            version _version
            artifact (sourceJar)
            artifact artifactRepository

            pom.withXml {
                def dependenciesNode = asNode().appendNode('dependencies')

                configurations.compile.allDependencies.each {
                    if(_group != null && (_artifact != null || "unspecified" == _artifact) && _version != null) {
                        def dependencyNode = dependenciesNode.appendNode('dependency')

                        dependencyNode.appendNode('groupId', _group)
                        dependencyNode.appendNode('artifactId', _artifact)
                        dependencyNode.appendNode('version', _version)
                    }
                }
            }
        }
    }

    repositories {
        maven {
            if(_version.endsWith('-SNAPSHOT')) {
                url snapshotsRepository
            } else {
                url releaseRepository
            }

           credentials(AwsCredentials) {
                accessKey AWS_ACCESS_KEY
                secretKey AWS_SECRET_KEY
           }
        }
    }
}

1 个答案:

答案 0 :(得分:3)

好的,所以请继续阅读我已实施question的链接this的答案,以解决我的问题。我最后的publish.gradle就像:

apply plugin: 'maven-publish'

Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())

ext {
    releaseRepository = "s3://****/maven/releases"
    snapshotsRepository = "s3://****/maven/snapshots"
    artifactRepository = "$buildDir/outputs/aar/render-engine-release.aar"

    _version = properties.getProperty('lib.version')
    _artifact = properties.getProperty('lib.name')
    _group = properties.getProperty('lib.group')
}


task sourceJar(type: Jar) {
    from android.sourceSets.main.java.srcDirs
    classifier "source"
}

publishing {
    publications {
        maven(MavenPublication) {
            groupId _group
            artifactId _artifact
            version _version
            artifact(sourceJar)
            artifact artifactRepository

            pom.withXml {
                def dependenciesNode = asNode().appendNode('dependencies')

                configurations.compile.getAllDependencies().each { Dependency dep ->
                    if (dep.group == null || dep.version == null || dep.name == null || dep.name == "unspecified")
                        return

                    def dependencyNode = dependenciesNode.appendNode('dependency')
                    dependencyNode.appendNode('groupId', dep.group)
                    dependencyNode.appendNode('artifactId', dep.name)
                    dependencyNode.appendNode('version', dep.version)

                    if (!dep.transitive) {
                        def exclusionNode = dependencyNode.appendNode('exclusions').appendNode('exclusion')
                        exclusionNode.appendNode('groupId', '*')
                        exclusionNode.appendNode('artifactId', '*')
                    } else if (!dep.properties.excludeRules.empty) {
                        def exclusionsNode = dependencyNode.appendNode('exclusions')
                        dep.properties.excludeRules.each { ExcludeRule rule ->
                            def exclusionNode = exclusionsNode.appendNode('exclusion')
                            exclusionNode.appendNode('groupId', rule.group ?: '*')
                            exclusionNode.appendNode('artifactId', rule.module ?: '*')
                        }
                    }
                }
            }
        }
    }

    repositories {
        maven {
            if (_version.endsWith('-SNAPSHOT')) {
                url snapshotsRepository
            } else {
                url releaseRepository
            }

            credentials(AwsCredentials) {
                accessKey AWS_ACCESS_KEY
                secretKey AWS_SECRET_KEY
            }
        }
    }
}