maven发布android.library与aar和源代码jar

有人可以给我一个关于如何使用maven-publish gradle插件发布com.android.library项目与aar和源代码的提示? 我可以用旧的Maven插件来做到这一点 – 但我想使用新的Maven-publish插件

Solutions Collecting From Web of "maven发布android.library与aar和源代码jar"

以下是使用新的maven-publish插件的示例。

 apply plugin: 'maven-publish' task sourceJar(type: Jar) { from android.sourceSets.main.java.srcDirs classifier "source" } publishing { publications { bar(MavenPublication) { groupId 'com.foo' artifactId 'bar' version '0.1' artifact(sourceJar) artifact("$buildDir/outputs/aar/bar-release.aar") } } repositories { maven { url "$buildDir/repo" } } } 

发布与./gradlew clean build publish

对dskinners进行一些调整,并生成正确的依赖关系:

 apply plugin: 'maven-publish' task sourceJar(type: Jar) { from android.sourceSets.main.java.srcDirs classifier "source" } publishing { publications { bar(MavenPublication) { groupId 'com.foo' artifactId 'bar' version '0.1' artifact(sourceJar) artifact("$buildDir/outputs/aar/bar-release.aar") 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 { if(it.group != null && (it.name != null || "unspecified".equals(it.name)) && it.version != null) { def dependencyNode = dependenciesNode.appendNode('dependency') dependencyNode.appendNode('groupId', it.group) dependencyNode.appendNode('artifactId', it.name) dependencyNode.appendNode('version', it.version) } } } } } repositories { maven { url "$buildDir/repo" } } } 

你可以通过定义来更改versiongroupId

 version = '1.0.0' group = 'foo.bar' 

这是我改进的解决scheme,基于其他答案。

要点: https : //gist.github.com/Robyer/a6578e60127418b380ca133a1291f017

其他答案的变化:

  • 更改classifier – 它必须是"sources"而不是 "source"
  • 处理依赖关系

    • 支持@aartransitive: false 。 在这种情况下,我们在POM中设置排除来忽略这个依赖的所有传递依赖。
    • 还支持依赖关系的自定义排除规则,例如:

       compile('com.example:something:1.0', { exclude group: 'com.exclude.this', module: 'some-module' }) 
  • 不需要手动指定工件path。

 apply plugin: 'maven-publish' task androidJavadocs(type: Javadoc) { source = android.sourceSets.main.java.srcDirs classpath += project.files(android.getBootClasspath().join(File.pathSeparator)) } task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) { classifier = 'javadoc' from androidJavadocs.destinationDir } task androidSourcesJar(type: Jar) { classifier = 'sources' from android.sourceSets.main.java.srcDirs } publishing { publications { maven(MavenPublication) { groupId 'com.example' artifactId 'custom-artifact' version '1.0' // Or use same version as in android branch // version = android.defaultConfig.versionName artifact bundleRelease artifact androidJavadocsJar artifact androidSourcesJar pom.withXml { def dependenciesNode = asNode().appendNode('dependencies') // List all compile dependencies and write to POM configurations.compile.getAllDependencies().each { Dependency dep -> if (dep.group == null || dep.version == null || dep.name == null || dep.name == "unspecified") return // ignore invalid dependencies def dependencyNode = dependenciesNode.appendNode('dependency') dependencyNode.appendNode('groupId', dep.group) dependencyNode.appendNode('artifactId', dep.name) dependencyNode.appendNode('version', dep.version) if (!dep.transitive) { // If this dependency is transitive, we should force exclude all its dependencies them from the POM def exclusionNode = dependencyNode.appendNode('exclusions').appendNode('exclusion') exclusionNode.appendNode('groupId', '*') exclusionNode.appendNode('artifactId', '*') } else if (!dep.properties.excludeRules.empty) { // Otherwise add specified exclude rules 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 ?: '*') } } } } } } } 

如果您想避免样板代码,因为maven-publish插件不会将依赖关系写入到pom.xml中

试试这个插件: android-maven-publish

 publishing { publications { mavenAar(MavenPublication) { from components.android } } } 

你也可以使用android maven插件 。 它创build.aar,javadoc.jar,sources.jar和.pom,并在将file upload到Maven存储库后更新maven-metadata.xml。 我也把脚本放在GitHub上 。

 apply plugin: 'com.android.library' apply plugin: 'maven' //Your android configuration android { //... } //maven repository info group = 'com.example' version = '1.0.0' ext { //Specify your maven repository url here repositoryUrl = 'ftp://your.maven.repository.com/maven2' //Or you can use 'file:\\\\C:\\Temp' or 'maven-temp' for a local maven repository } //Upload android library to maven with javadoc and android sources configurations { deployerJars } //If you want to deploy to an ftp server dependencies { deployerJars "org.apache.maven.wagon:wagon-ftp:2.2" } // custom tasks for creating source/javadoc jars task javadoc(type: Javadoc) { source = android.sourceSets.main.java.srcDirs classpath += project.files(android.getBootClasspath().join(File.pathSeparator)) destinationDir = file("../javadoc/") failOnError false } task javadocJar(type: Jar, dependsOn: javadoc) { classifier = 'javadoc' from javadoc.destinationDir } //Creating sources with comments task androidSourcesJar(type: Jar) { classifier = 'sources' from android.sourceSets.main.java.srcDirs } //Put the androidSources and javadoc to the artifacts artifacts { archives androidSourcesJar archives javadocJar } uploadArchives { repositories { mavenDeployer { configuration = configurations.deployerJars repository(url: repositoryUrl) { //if your repository needs authentication authentication(userName: "username", password: "password") } } } } 

用它来呼叫

 ./gradlew uploadArchives