如何把开源项目发布到Jcenter

转载自:https://www.jianshu.com/p/f66972f0607a
首先咱们应该注册一个JFrog Bintray的帐号
Jfrog Bintray官网

这里咱们能够注意到那个绿色的按钮,START YOUR FREE TRIAL。注册吗,多简单的一件事啊。点了直接进去注册。注册完以后,直接开整,到最后发布的时候。你发现你会一直报错,我明明按照网上一步一步来的,怎么会报错。真正注册用于开发者的,是下面哪一行小字(我真的不明白为啥要那么不明显),For Open Source Distribution Sign Up Here,你们千万不要搞错了。java

接下来输入按照注册的要求进行填写就行了,ps:一些邮箱不支持,例如163和qq。这里我是用gmai注册的,没有的话,你们不妨去注册一个,如今注册gmail须要使用手机了。选择国家地区代码的时候并找不到中国,前面国家随便选一个,而后直接在你手机前面加上86就好了。android

注册成功,就能够看到咱们的首页了。点击Edit Profile,若是你发现,你和我同样是七个那么注册成功。git

Edit Profile界面
github

这里咱们须要注意的信息,一个是你注册的帐号,另外一个是API Key,为接下来发布作准备。web

准备一个须要发布的Android Studio项目
建立一个Android Studio项目,这里咱们就是用默认的Module - app。如今项目的Build.gradle文件中添加须要须要使用到的依赖。apache

buildscript {
repositories {
  jcenter()
}
dependencies {
  classpath'com.android.tools.build:gradle:2.3.0'
  classpath'com.github.dcendents:android-maven-gradle-plugin:1.5'
  classpath'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.5'
  // NOTE: Do not place your application dependencies here; they belong
  // in the individual module build.gradle files
  }
}
allprojects {
  repositories {
    jcenter()
  }
}
task clean(type: Delete) {
  deleterootProject.buildDir
}

有两点点很是重要,第一点就是gradle build tools的版本设置成1.1.2以上,由于之前的版本有严重的bug,咱们将使用的是最新的版本2.3.0。第二点是根据你Gradle的版本,使用不一样版本的android-maven-gradle-plugin。这里个人Gradle版本是3.3,因此使用的是com.github.dcendents:android-maven-gradle-plugin:1.5,具体版本直接对应的关系请到以下网站去查询,android-maven-gradle-plugin。
api

版本直接对应关系服务器

接下来咱们将修改local.properties。在里面定义api key的用户名以及被建立key的密码,用于bintray的认证。之因此要把这些东西放在这个文件是由于这些信息时比较敏感的,不该该处处分享,包括版本控制里面。幸运的是在建立项目的时候local.properties文件就已经被添加到.gitignore了。所以这些敏感数据不会被误传到git服务器。app

bintray.user=YOUR_BINTRAY_USERNAME
bintray.apikey=YOUR_BINTRAY_API_KEY

bintray.user对应的就是你注册bintray时的帐号,bintray.apikey对应的就https://bintray.com/profile/edit页面中的API KEY,点击进入还须要输入你注册时的密码。输入完成直接保存下。
接下来,打开咱们module下的build.gradle文件,以下:maven

apply plugin:'com.android.library'     //建议使用library,毕竟你是给别人用的
apply plugin:'com.github.dcendents.android-maven'
apply plugin:'com.jfrog.bintray'

version="1.0.2"           // 对应的版本

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        // 请注意这里没有Application Id。别人在他的module下的build.gradle文件依赖中,添加你的你开源项目。你的
        //defaultConfig中有Application Id,别人也会有啊,那么有两个Application Id必然会报错
        minSdkVersion15
        targetSdkVersion25
        versionCode
        versionName"1.0"
        testInstrumentationRunner"android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir:'libs',include: ['*.jar'])
    compile'com.android.support:appcompat-v7:25.3.1'
    testCompile'junit:junit:4.12'
}

defsiteUrl='https://github.com/x1ao3eng/GithubPro' // github上项目的地址
defgitUrl='https://github.com/x1ao3eng/GithubPro.git' //github上项目的git地址
group='com.x1aomeng.githubpro'
//通常用你项目的Application Id,这里通常用到哪里呢。
//举一个使用RecyclerView须要添加的依赖,compile'com.android.support:recyclerview-v7:25.3.1'
//com.android.support对应的就是上面的group,recyclerview-v7对应的就是module名字,25.3.1对应的是上面的version
install{
    repositories.mavenInstaller{
        pom {
            project{
                packaging'aar'
                // Add your description here
                name'Android Jcenter Test'
                url siteUrl
                // Set your license
                licenses {
                    license {
                        name'The Apache Software License, Version 2.0'
                        url'http://www.apache.org/licenses/LICENSE-2.0.txt'
                    }
                }
                developers{
                    developer {
                        id'huangxiaomeng'//填写的一些基本信息
                        name'x1ao3eng'
                        email'x1ao3eng@gmail.com'
                    }
                }
                scm {
                    connection gitUrl
                    developerConnection gitUrl
                    url siteUrl
                }
            }
        }
    }
}
task sourcesJar(type: Jar) {
    from android.sourceSets.main.java.srcDirs
    classifier='sources'
}
task javadoc(type: Javadoc) {
    source= android.sourceSets.main.java.srcDirs
    classpath+=project.files(android.getBootClasspath().join(File.pathSeparator))
}
task javadocJar(type: Jar,dependsOn: javadoc) {
    classifier='javadoc'
    from javadoc.destinationDir
}
artifacts {
    archives javadocJar
    archives sourcesJar
}
Properties properties =newProperties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
bintray {
    // 这里就使用到了,你以前在local.properties中添加的信息
    user = properties.getProperty("bintray.user")
    key = properties.getProperty("bintray.apikey")
    configurations = ['archives']
    pkg {
        repo ="maven"
        name ="GithubPro_for_jcenter_test"//发布到JCenter上的项目名字
        websiteUrl = siteUrl
        vcsUrl = gitUrl
        licenses = ["Apache-2.0"]
        publish =true
    }
}

完成这些以后先Rebuild或者Sync下你的Project,没什么问题的话。就能够把你项目生成到本地的仓库中了,Android Studio中默认即在Android\sdk\extras\android\m2repository这里,能够打开你Android Studio下方的Terminal 或则在Project下打开命令行,输入一下(可能还须要下载一遍Gradle):

gradlew install

把项目上传到Bintray
把项目上传到Bintray,须要plugin的支持,在project目录下build.gradle文件的dependencies中添加(上面已添加过):

classpath'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.5'

接下来执行以下命令行,完成上传:

gradlew bintrayUpload

这里咱们通常会在进度97%或者98%卡住一下子,ps:须要FQ,出去抽根烟或者倒杯咖啡。上传成功后,咱们打开bintray上仓库的界面就能够看到咱们的项目CithubPro_for_jcenter。

仓库界面

到这一步已经接近尾声了,可是咱们的项目尚未发布到Jcenter,只是在你本身的仓库中。别人若是想要使用你的开源项目,须要在project目录下添加以下代码:

allprojects {
    repositories {
        jcenter()
        maven { url'https://dl.bintray.com/huang3eng/maven/'}
    }
}

而后在相应module目录下的build.gradle中添加:

compile 'com.x1aomeng.githubpro:app:1.0.2'

虽然这样已经可使用,可是仍是有诸多不便的,若是有使用十个不一样人写的开源项目,那么不是要先添加十个别人maven仓库的地址。Android Studio如今默认已经关联到Jcenter这个远程仓库,因此咱们最好仍是发布到Jcenter上。打开你的项目,点击Add to Jcenter,什么也不须要填,直接Send就行了。接下来须要经过管理员的审核都行了(我这边审核用了四个多小时),通常你的项目没有什么中文或者全英语注释均可以经过(终于知道为啥那些国内开源项目也不多会有中文注释)。

审核完成后,你会收到一份邮件让你确认,审核成功以后,你会发现项目中Linked to会多了一个Jcenter,这说明你的项目已经成功发不到Jcenter上了。我只能告诉你,你成功了与世界接轨的第一步。

总结
每一个人的开发环境是不同的,使用的版本也多是不同的。因为种种的不一样,可能照着个人步骤,你会出现报错。网上将开源项目发布到Jcenter的文章也很多,使用的方式也可能不一样。我这边文章主要把我本身踩过的一些坑记录下来,但愿对大家哪怕只有一点点的帮助。第一次写简书,感受又把整个过程梳理了一遍。若是有不对的地方或者更好的方法,请告诉我。

做者:奥斯特洛夫斯基c 连接:https://www.jianshu.com/p/f66972f0607a 來源:简书 简书著做权归做者全部,任何形式的转载都请联系做者得到受权并注明出处。

相关文章
相关标签/搜索