发布项目/Library到Maven 仓库
在上一篇博客中《Maven从入门到提高(三)-Mac下使用Nexus搭建Maven私服》我们已经搭建好了Maven私服,今天我们就来把我们的项目/Library 发布到我们Maven 服务器中。
准备工作
首先要有一个Android 项目,我以我自己GitHub上的一个例子作说明。
GitHub项目地址:
https://github.com/xuxian361/MavenDemo-LocalRepo
项目结构图:
我们的目标是把nexuslib发布到Nexus搭建的Maven 仓库。
第一步
启动Nexus 服务
用浏览器访问:http://localhost:8081/nexus/,成功的话可以看到
第二步
填写我们这个项目(nexuslib)的build.gradle 脚本代码。
nexuslib的builde.gradle文件:
apply plugin: 'com.android.library'
android {
compileSdkVersion 24
buildToolsVersion "24.0.2"
defaultConfig {
minSdkVersion 15
targetSdkVersion 24
versionCode 1
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'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:24.2.1'
testCompile 'junit:junit:4.12'
}
apply from: './nexus-push.gradle'
其实我们只看最后一句就好。我们为了避免以后这个build.gradle 文件会越来越大,所以我们添加了
apply from: './nexus-push.gradle'
这句脚本来引用一个gradle 文件来专门处理maven 的发布。
所以我们在nexuslib 的根目录新建一个gradle 文件,名字是nexus-push.gradle。
在这个新建的文件添加如下内容:
apply plugin: 'maven'
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
}
artifacts {
archives androidSourcesJar
archives androidJavadocsJar
}
uploadArchives {
repositories {
mavenDeployer {
repository(url: "http://localhost:8081/nexus/content/repositories/releases/") {
authentication(userName: "admin", password: "admin123")
}
pom.groupId = 'com.dragonpass.sundy.nexuslib'
pom.artifactId = 'nexuslib'
pom.version = '0.0.2'
pom.project {
licenses {
license {
name 'The Apache Software License, Version 2.0'
url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
}
}
}
}
}
}
首先需要引用maven插件;然后定义了2个任务:androidSourcesJar和androidJavadocsJar,这两个任务分别用于对Java sources打包和Java doc进行打包;接着我们对uploadArchives.repositories闭包进行一些配置,包括仓库的url地址,上传所需的用户名和密码,以及pom属性。
第三步
执行脚本
我们可以使用命令行方式执行脚本
gradlew uploadArchives
也可以直接使用Android Studio 自带的Gradle 工具
如果不报错的话我们就可以在nexus 服务器中查看我们发布的项目了,
第四步
引用nexus 发布的项目
在我们的项目的根目录下的build.gradle 文件中添加,
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
maven {
url 'file:///Users/sundy/Documents/MavenRepo'
}
maven {
url "http://localhost:8081/nexus/content/repositories/releases/"
}
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
其中
maven {
url "http://localhost:8081/nexus/content/repositories/releases/"
}
就是我们的nuxeus服务器发布的项目的地址
接下来在需要引用的项目中的build.gradle中添加依赖即可,
compile 'com.dragonpass.sundy.nexuslib:nexuslib:0.0.2'
最后Sync 一下项目,我们就可以在项目中使用该maven 的项目/Library了。