Android 搭建Maven 仓库
前言
由于公司项目众多,又都依赖相同的lib,导致lib有变动所有的被依赖者都需要重新导入,很繁琐。而用Maven管理最好不过了
创建Maven项目
第一种方式:
新建一个android library,这个module就是要提供给别人的。在build.gradle添加如下:
apply plugin: 'maven'
uploadArchives {
repositories.mavenDeployer {
repository(url: LOCAL_REPO_URL)
pom.groupId = GROUP
pom.artifactId = POM_ARTIFACT_ID
pom.version = VERSION
}
}
gradle.properties文件配置属性:
#MAVEN需要的配置
GROUP=com.test.android
POM_ARTIFACT_ID=testUI
VERSION=1.0.0
//这里 指定生成的Maven路径
LOCAL_REPO_URL=file:///Users/caojiaming/StudioProjects/maven/
第二种方式:当前modle 可以采用 apply from: './nexus-push.gradle' 方式
android {
dependencies { //当前modle采用 apply
implementation fileTree(dir: 'libs', include: ['*.jar'])
apply from: './nexus-push.gradle'
}
依附的脚本:
apply plugin: 'maven'
def isReleaseBuild() {
return VERSION_NAME.contains("SNAPSHOT") == false
}
def getRepositoryUsername() {
return hasProperty('NEXUS_USERNAME') ? NEXUS_USERNAME : "admin"
}
def getRepositoryPassword() {
return hasProperty('NEXUS_PASSWORD') ? NEXUS_PASSWORD : "admin123"
}
afterEvaluate { project ->
uploadArchives {
repositories {
mavenDeployer {
pom.groupId = "com.appcan.freestyle"
pom.artifactId = "ACComponentWelcome"
pom.version = "2.0.0"
repository(url: "http://192.168.1.115:8862/repository/maven-releases/") {
authentication(userName: getRepositoryUsername(), password: getRepositoryPassword())
}
snapshotRepository(url: "http://192.168.1.115:8862/repository/maven-releases/") {
authentication(userName: getRepositoryUsername(), password: getRepositoryPassword())
}
}
}
}
task androidJavadocs(type: Javadoc) {
source = android.sourceSets.main.java.srcDirs
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
options.encoding = "UTF-8"
}
task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) {
classifier = 'javadoc'
from androidJavadocs.destinationDir
}
task androidSourcesJar(type: Jar) {
classifier = 'sources'
from android.sourceSets.main.java.sourceFiles
}
artifacts {
archives androidSourcesJar
}
}
点一下 同步 (Sync Project with Gradle Files)
在Android Studio 右侧的Gradle Projects 如图


至此本地已经发布成功,可以看到默认生成的是aar。接下来看一下如何引用
在需要此依赖的项目里build.gradle里添加
repositories {
maven {
url 'file:///Users/gyb/custom/maven/'
}
}
dependencies {
compile 'com.test.android:testUI:1.0.0'
}
有的gradle版本需要在版本后追加@aar
dependencies {
compile 'com.test.android:testUI:1.0.0@aar'
}
compile的生成规则在前面配置的gradle.properties的GROUP、POM_ARTIFACT_ID、VERSION共同决定
怎么搭建自己的私服
以上都是在本地仓库玩的。但实际这并没什么用,至少你要发布到服务器上才会有切合实际的用处。这里先说一下发布到私有服务器上的步骤。
Nexus的搭建
什么是Nexus:Nexus Repository Manager是一个需要Java运行时环境的Java应用程序。运行Nexus Repository Manager时,您正在运行带有基于Web的用户界面的服务器应用程序。该应用程序本身与Eclipse Jetty servlet容器和Apache Karaf OSGi容器一起运行。更详细的介绍请参阅官方文档
下载地址:https://www.sonatype.com/download-oss-sonatype
启动Nexus服务
打开终端命令行依次输入
cd /Users/gyb/custom/nexus-3.3.2-02-mac/nexus-3.3.2-02/bin (懒得配置环境变量)
./nexus start (启动的命令)
你会看到Starting nexus提示,证明服务已经启动
浏览器输入:http://localhost:8081

点击Sigin in
账号:admin 密码admin123