如何使用GitHub 并搭建Maven库

搭建Maven库,是因为这样做确实方便,可以把常用的功能封装成一个库打包好放在GitHub中,既方便使用,也方便和别人共享。至于安全性的问题嘛,我认为不必在意,毕竟框架中时不涉及到业务逻辑的,而我们的框架不是最好的,别人想用也不是坏事嘛。

有一个GitHub账号

这一点是必须的,如果你从没用过GitHub,请参考这篇博客GitHub的注册与使用(详细图解)

创建一个组织

New Orgnization
如果你已经拥有一个Orgnization,请跳过这一步

Create a new repository

注意,这里新建repository是在刚才创建的组织中新建的,而不是在个人账号中创建的,新建的repository的所有权是归组织所有。

New Repository

New Repository

将项目clone到本地

举个栗子:

1
git clone https://github.com/hemaapp/hm_Framework.git hm_framework_organization_aar

当然你也可以使用git的图形化工具把项目迁移到本地的某个文件夹中,这里我是放在了这里:
D:\hm_framework_organization_aar
一定要记住这个文件夹,待会要用的。

在gradle中配置相关信息

配置项目gradle

1
apply from: 'maven-release-aar.gradle'

配置maven-release-aar.gradle

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// 1.maven-插件
apply plugin: 'maven'
// 2.maven-信息
ext {// ext is a gradle closure allowing the declaration of global properties
PUBLISH_GROUP_ID = 'com.hemaapp'
PUBLISH_ARTIFACT_ID = 'hm_framework'
PUBLISH_VERSION = '0.2'
}
// 3.maven-路径
uploadArchives {
repositories.mavenDeployer {
def deployPath = file(getProperty('aar.deployPath'))
repository(url: "file://${deployPath.absolutePath}")
pom.project {
groupId project.PUBLISH_GROUP_ID
artifactId project.PUBLISH_ARTIFACT_ID
version project.PUBLISH_VERSION
}
}
}
//aar包内包含注释
task androidSourcesJar(type: Jar) {
classifier = 'sources'
from android.sourceSets.main.java.sourceFiles
}
artifacts {
archives androidSourcesJar
}

配置gradle.properties,这里需要制定项目生成路径

1
aar.deployPath=D:\\hm_framework_organization_aar

生成aar

打开命令行并执行

1
gradlew uploadarchives

将aar提交到GitHub

可以打开命令行执行以下命令

1
2
3
git add .
git commit -m 'content'
git push

在项目中使用aar

配置项目gradle

1
2
3
4
5
6
allprojects {
repositories {
jcenter()
maven { url "https://github.com/hemaapp/hm_Framework/raw/master/" }
}
}

配置module的gradle

1
compile 'com.hemaapp:luna_framework:1.0' exclude group: 'com.android.support'

最后

就可以尽情使用了


文章目录
  1. 1. 有一个GitHub账号
  2. 2. 创建一个组织
  3. 3. Create a new repository
  4. 4. 将项目clone到本地
  5. 5. 在gradle中配置相关信息
  6. 6. 生成aar
  7. 7. 将aar提交到GitHub
  8. 8. 在项目中使用aar
  9. 9. 最后
|