Maven项目使用项目对象模型(Project Object Model,POM)来配置。Maven 是一种构建工具,Maven 包是由所谓 POM(Project Object Model)所定义的文件

项目对象模型存储在名为 pom.xml 的文件中。

以下是一个简单的示例:

4.0.0

com.mycompany.app

my-app

1.0

junit

junit

3.8.1

test

对于一个合符规范的 Maven Package,pom 文件、aar(或者 jar) 文件是必须的。

设置 maven 仓库

在Android 项目中通常是是在根目录的 build.gradle 中设置 maven 仓库的地址

buildscript {
repositories {
maven {
url 'https://xxxxxxxx/nexus/content/repositories/ss_app_android'
}
maven {
url 'https://xxxxxxx/nexus/content/repositories/central'
}
// 这两个是 maven 的中央仓库
jcenter()
mavenCentral()
}
}
maven { url } 除了可以添加远程仓库,还能添加本地的 maven 仓库
maven {
url 'file:///xxxxAndroid/repo/'
}

发布 Maven 包

可以通过 gradle 插件 快速发布 maven 仓库

lolcalmaven 发布

新建lolcalmaven module

修改gradle.properties

PROJ_NAME=localrepo

PROJ_ARTIFACTID=localrepo

PROJ_POM_NAME=Local Repository

LOCAL_REPO_URL=file:///Users/your-user-name/Documents/Android/repo/

#以上是 Mac 的本地路径,如果是 Windows,则是类似:

#LOCAL_REPO_URL=file:///C:/Users/cadmanager/Documents/repo/
在lolcalmaven 的 build.gradle 中增加和配置 maven plugin
apply plugin: 'maven'
uploadArchives {
repositories.mavenDeployer {
repository(url: LOCAL_REPO_URL)
pom.groupId = PROJ_GROUP
pom.artifactId = PROJ_ARTIFACTID
pom.version = PROJ_VERSION }
}

控制台运行

./gradlew -p localrepo clean build uploadArchives --info

生成的文件

.

├── localrepo-1.0.0.aar

├── localrepo-1.0.0.aar.md5

├── localrepo-1.0.0.aar.sha1

├── localrepo-1.0.0.pom

├── localrepo-1.0.0.pom.md5

└── localrepo-1.0.0.pom.sha1

生成的 pom 文件

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

4.0.0

io.github.xuyushi

localrepo

1.0.0

aar

com.android.support

appcompat-v7

25.3.0

compile

在新的项目工程中使用,在根目录的 gradle 中添加本地仓库

allprojects {
repositories {
jcenter()
maven {
url 'file:///Users/xuyushi/Documents/Android/repo/'
}
}
}

在 app moudle的 build.gradle 中增加依赖

compile 'io.github.xuyushi:localrepo:1.0.0'

这样就可以在项目中使用 本地仓库中的库了

发布 jcenter 远程仓库

注册账号

Bintray是jcenter的托管商,因此你必须注册一个Bintray账号,注册完账号后记下你的用户名以及API Key。

登陆后在首页右上角点击用户名进入个人主页,然后点击用户名下面的Edit进入个人信息编辑页面,接下来点击页面左边列表的最后一项API Key

配置插件

在项目的根目录 build.gradle 中添加 dependencies

dependencies {
classpath 'com.android.tools.build:gradle:2.2.2'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.2'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}

配置 local.properties

配置在本地文件中 ,不计入版本管理

bintray.user=xxxx
bintray.apikey=your keys
修改需要上传的 module 的 gradle 文件
apply 插件
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'
// This is the library version used when deploying the artifact
version = "1.0.0"
def siteUrl = 'http://xuyushi.github.io' // 项目的主页
def gitUrl = 'http://xuyushi.github.io' // Git仓库的url
group = "io.github.xuyushi" // Maven Group ID for the artifact,一般填你唯一的包名
install {
repositories.mavenInstaller {
// This generates POM.xml with proper parameters
pom {
project {
packaging 'aar'
// Add your description here
name 'multi-format 1D/2D barcode image processing use zxing.'
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 'xuyushi' //填写的一些基本信息
name 'xuyushi'
email 'xmy166@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 = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
bintray {
user = properties.getProperty("bintray.user")
key = properties.getProperty("bintray.apikey")
configurations = ['archives']
pkg {
repo = "maven"
name = "maventest" //发布到JCenter上的项目名字
websiteUrl = siteUrl
vcsUrl = gitUrl
licenses = ["Apache-2.0"]
publish = true
}
}
javadoc { //jav doc采用utf-8编码否则会报“GBK的不可映射字符”错误
options{
encoding "UTF-8"
charSet 'UTF-8'
}
}

在 jcenter 中新建 maven 仓库

上传命令

./gradlew jcentermaven:bintrayUpload

将提交到Bintray的项目发布到JCenter

完成上述的步骤只是将项目提交到bintray,还无法使用该项目库,因为还没有发布到JCenter。

登入Bintray网站,进入个人中心,在右侧的Owned Repositories区域点击Maven的图标,进入你的Maven项目列表。

如果已经上传成功了,在这里就能看到你的项目,进入项目详情,在右下角的Linked To区域点击Add to JCenter,然后在Comments输入框里随便填写下信息,最后点Send提交请求即可。一般情况下当天就会审核,审核通过后会给你发邮件通知你,并且以后更新项目就不需要再审核了。

审核成功后就可以使用你发布到JCenter上的项目了。

使用你发布到JCenter上的项目

在Bintray的搜索输入框中输入你的项目:

###总结

既然要上传到Jcenter上,自然要去 https://bintray.com 中注册账号

根据自己的需求创建maven的Repository

把项目分离成Module

在项目根目录的build.gradle中添加bintray插件

在local.properties中添加bintray认证

修改Module中的build.gradle中的配置

在Android Studio终端使用./gradlew xxx上传

最后在JFrog Bintray中同步到Jcenter中