Skip to content
This repository has been archived by the owner on Feb 11, 2022. It is now read-only.

Add support for syncing to maven central

Farbod Salamat-Zadeh edited this page Mar 26, 2016 · 2 revisions

As jCenter has superceeded Maven Central as the most popular repository for your dependencies, this plugin doesn't provide full support to sync between bintray and Maven Central out of the box (you can read the full discussion here). Adding missing fields in the generated pom-file is pretty easy though.

In addition to the steps described above, do the following:

  • Open your project's build.gradle.
  • Define some extended properties like this and replace the placeholders with your own values. They will be used in the hook that will a generate pom-file that won't be rejected by Maven Central. IS_UPLOADING will be true if you invoke ./gradlew bintrayUpload.
ext {
    ARTIFACT_ID = 'YOUR_ARTIFACT_ID'
    VERSION_NAME = 'YOUR_VERSION_NAME'
    VERSION_CODE = 1 //your version
    
    DESCRIPTION = 'YOUR_DESCRIPTION'
    
    SITE_URL = 'YOUR_SITE_URL'
    GIT_URL = 'YOUR_GIT_URL'
    GROUP_NAME = 'YOUR_GROUP_NAME'
    COMPILE_SDK = 23
    BUILD_TOOLS = '23.0.1'
    
    MODULE_NAME = 'YOUR_MODULE_NAME'
    
    LICENSE = 'YOUR_LICENSE'
    
    DEVELOPER_ID = 'YOUR_DEVELOPER_ID'
    DEVELOPER_NAME = 'YOUR_NAME'
    DEVELOPER_EMAIL = 'YOUR_EMAIL_ADDRESS'
    
    IS_UPLOADING = project.getGradle().startParameter.taskNames.any{it.contains('bintrayUpload')}
}

Feel free to define those properties somewhere else. They tend to be pretty general in nature, you will probably already have a place where you define your properties.

  • Add the hook that will first delete the pom-file and regenerate one that meets Maven Central's criteria. If you want to apply the hock to several modules in your project, make sure to specify them all in the if-clause.
subprojects {
    group = GROUP_NAME
    version = VERSION

    if (IS_UPLOADING && project.name in [MODULE_NAME]) {
        println project.name
        apply plugin: 'maven'

        gradle.taskGraph.whenReady { taskGraph ->
            taskGraph.getAllTasks().find {
                it.path == ":$project.name:generatePomFileForMavenPublication"
            }.doLast {
                file("build/publications/maven/pom-default.xml").delete()
                println 'Overriding pom-file to make sure we can sync to maven central!'
                pom {
                    //noinspection GroovyAssignabilityCheck
                    project {
                        name "$project.name"
                        artifactId ARTIFACT_ID
                        packaging project.name == 'compiler' ? 'jar' : 'aar'
                        description DESCRIPTION
                        url SITE_URL
                        version VERSION_NAME

                        scm {
                            url GIT_URL
                            connection GIT_URL
                            developerConnection GIT_URL
                        }

                        licenses {
                            license {
                                name LICENSE
                            }
                        }

                        developers {
                            developer {
                                id DEVELOPER_ID
                                name DEVELOPER_NAME
                                email DEVELOPER_EMAIL
                            }
                        }
                    }
                }.writeTo("build/publications/maven/pom-default.xml")
            }
        }
    }
}