Nexus 是 Sonatype公司的一个产品,它是Maven的私服。
1、配置Nexus(v3.16.2)
下载对应版本的nexus:下载Nexus
解压文件,使用cmd,进入…\nexus-3.16.2-01-win64\nexus-3.16.2-01\bin目录,执行命令:
.\nexus.exe /run
注意:
1、确保8081端口未被占用;
2、使用管理员身份运行cmd;
3、使用/start命令可能会报错:Could not start service. Error code: 1060;使用run命令启动;
服务开启后,访问 http://localhost:8081/ ,点击右上角Sign in,默认用户名 : admin ;密码 : admin123 。
2、配置Gradle
1、使用AS新建项目,并新建nexusLib模块。在nexusLib中随便写一个测试类。
2、在nexusLib的根目录添加 upload_nexus.gradle 文件,并输入以下内容。
注意:参数配置(REPOSITORY_URL、用户名、密码)要求和服务端一致。
- 写法一(通过判断版本号,动态指定远程路径。):
参考:Android-Nexus 搭建自己的 Maven 仓库 & Gradle 上传依赖包
apply plugin: 'maven'
//def定义的变量才会在project中
def GROUP = 'com.test.nexuslib'
def ARTIFACT_ID = 'nexusLibrary'
def VERSION_NAME = "1.0.3"
//def VERSION_NAME = '1.0.1-SNAPSHOT'
//上传的目标仓库地址
def SNAPSHOT_REPOSITORY_URL = 'http://localhost:8081/repository/maven-snapshots/'
def RELEASE_REPOSITORY_URL = 'http://localhost:8081/repository/maven-releases/'
def REPOSITORY_URL = VERSION_NAME.toUpperCase().endsWith("-SNAPSHOT") ? SNAPSHOT_REPOSITORY_URL : RELEASE_REPOSITORY_URL
//Nexus 的私服的用户名称和密码
def NEXUS_USERNAME = 'admin'
def NEXUS_PASSWORD = 'admin123'
afterEvaluate { project ->
uploadArchives {
repositories {
mavenDeployer {
pom.groupId = GROUP
pom.artifactId = ARTIFACT_ID
pom.version = VERSION_NAME
repository(url: REPOSITORY_URL) {
authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD)
}
}
}
}
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.sourceFiles
}
//解决 JavaDoc 中文注释生成失败的问题
tasks.withType(Javadoc) {
options.addStringOption('Xdoclint:none', '-quiet')
options.addStringOption('encoding', 'UTF-8')
options.addStringOption('charSet', 'UTF-8')
}
artifacts {
archives androidSourcesJar
archives androidJavadocsJar
}
}
- 写法二:
动态指定本地仓库和私服,亦可参考LegoArch写法(BD Netdisk)。
项目结构如下:
3、在nexusLib模块下的build.gradle中添加如下代码(注意引用位置),并同步gradle。
android{
...
}
//必须在android{}后面
apply from: 'upload_nexus.gradle'
4、在AS右侧的gradle窗口中找到nexusLib模块对应的上传命令(uploadArchives):
执行命令,等待上传结果。
上传成功后,刷新 http://localhost:8081/ ,可以看到上传后的文件,和upload_nexus.gradle中的配置信息是对应的。
3、使用Library
1、新建 测试项目,在项目的build.gradle中添加maven仓库地址:
allprojects {
repositories {
maven {
url "http://localhost:8081/repository/maven-releases/"
}
...
}
}
2、在app模块的build.gradle中添加:
依赖格式对应发布时的属性:implementation ‘groupId:artifactId:verion’
dependencies {
implementation 'com.test.nexuslib:nexusLibrary:1.0.0'
}
3、在测试项目代码中直接引用aar中的资源:
4、有module依赖关系时如何发布(写法一):
当项目中有module依赖关系(例如:A 依赖 B;A 和 B需要各自独立发布到远程仓库时),项目整体结构:
4.1、每个module目录下新建 gradle.properties 配置文件,内容如下:
# 不可以加单引号或双引号
ARTIFACT_ID=app
VERSION_NAME=1.1.1
4.2、根目录下 新建通用配置文件 release-aar.gradle,内容如下:
apply plugin: 'maven'
def GROUP_ID = 'com.test.vehiclecheck'
//如果没有module依赖,可以直接定义版本和名称
//def ARTIFACT_ID = 'test'
//def VERSION_NAME = "1.0.3"
//def VERSION_NAME = '1.0.1-SNAPSHOT'
//上传的目标仓库地址
def SNAPSHOT_REPOSITORY_URL = 'http://localhost:8081/repository/maven-snapshots/'
def RELEASE_REPOSITORY_URL = 'http://localhost:8081/repository/maven-releases/'
def REPOSITORY_URL = VERSION_NAME.toUpperCase().endsWith("-SNAPSHOT") ? SNAPSHOT_REPOSITORY_URL : RELEASE_REPOSITORY_URL
//Nexus 的私服的用户名称和密码
def NEXUS_USERNAME = 'admin'
def NEXUS_PASSWORD = 'admin123'
afterEvaluate { project ->
uploadArchives {
// configuration = configurations.archives
repositories {
mavenDeployer {
repository(url: REPOSITORY_URL) {
authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD)
pom.groupId = GROUP_ID
//版本号和ARTIFACT_ID取值于gradle.properties
pom.artifactId = ARTIFACT_ID
pom.version = VERSION_NAME
}
pom.whenConfigured { MavenPom pom ->
def dependencies = pom.dependencies.findAll { item ->
("unspecified" == item.version)
println("--all 1--->>" + item.getArtifactId() + ":" + item.getVersion())
}
println("--all 2--->>" + dependencies)
pom.dependencies.removeAll(dependencies)//每个module独立发布,移除项目内的module依赖。例如:implementation project(path: ':ipw_0430')
}
}
}
}
// 写法(参考LegoArch):
if (project.hasProperty("android")) {
println("--all--->>android")
task androidJavadoc(type: Javadoc) {
options {
encoding "UTF-8"
charSet 'UTF-8'
links "http://docs.oracle.com/javase/8/docs/api/"
linksOffline "http://d.android.com/reference", "${android.sdkDirectory}/docs/reference"
}
destinationDir = file("./javadoc/")
source = android.sourceSets.main.java.srcDirs
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
exclude('**/BuildConfig.java',
'**/R.java',
'**/*.aidl')
failOnError = false
}
task androidJavadocJar(type: Jar, dependsOn: androidJavadoc) {
classifier = 'javadoc'
from androidJavadoc.destinationDir
}
task androidSourceJar(type: Jar) {
classifier = 'sources'
from android.sourceSets.main.java.srcDirs
}
artifacts {
archives androidSourceJar
archives androidJavadocJar
}
}
}
4.3、每个module的build.gradle 引用配置文件release-aar.gradle :
android{...}
apply from: '../release-aar.gradle'
4.4、分别执行uploadArchives 命令即可发布。
5、有module依赖关系时如何发布(写法二):
推荐这种写法
5.1、项目的build.gradle中加入以下配置:
subprojects {
apply plugin: 'maven'
}
ext {
GROUP_ID = 'com.test.vehiclecheck'
//上传的目标仓库地址
SNAPSHOT_REPOSITORY_URL = 'http://localhost:8081/repository/maven-snapshots/'
RELEASE_REPOSITORY_URL = 'http://localhost:8081/repository/maven-releases/'
//Nexus 的私服的用户名称和密码
NEXUS_USERNAME = 'admin'
NEXUS_PASSWORD = 'admin123'
appVersionName = '1.1.4'
appModuleName = 'app'
ipwVersionName = '1.1.0'
ipwModuleName = 'ipw_0430'
}
5.2、根目录下 新建通用配置文件 release-aar.gradle,内容如下:
//apply plugin: 'maven' //此处不需要了
def mExt = rootProject.ext
afterEvaluate { project ->
uploadArchives {
// configuration = configurations.archives
repositories {
mavenDeployer {
def REPOSITORY_URL = mExt.appVersionName.toUpperCase().endsWith("-SNAPSHOT")? mExt.SNAPSHOT_REPOSITORY_URL : mExt.RELEASE_REPOSITORY_URL
println 'REPOSITORY_URL : ' + REPOSITORY_URL
repository(url: REPOSITORY_URL) {
authentication(userName: mExt.NEXUS_USERNAME, password: mExt.NEXUS_PASSWORD)
pom.groupId = mExt.GROUP_ID
// pom.artifactId = ARTIFACT_ID
// pom.version = VERSION_NAME
}
}
}
}
if (project.hasProperty("android")) {
println("--all--->>android")
task androidJavadoc(type: Javadoc) {
options {
encoding "UTF-8"
charSet 'UTF-8'
links "http://docs.oracle.com/javase/8/docs/api/"
linksOffline "http://d.android.com/reference", "${android.sdkDirectory}/docs/reference"
}
destinationDir = file("./javadoc/")
source = android.sourceSets.main.java.srcDirs
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
exclude('**/BuildConfig.java',
'**/R.java',
'**/*.aidl')
failOnError = false
}
task androidJavadocJar(type: Jar, dependsOn: androidJavadoc) {
classifier = 'javadoc'
from androidJavadoc.destinationDir
}
task androidSourceJar(type: Jar) {
classifier = 'sources'
from android.sourceSets.main.java.srcDirs
// from android.sourceSets.main.java.sourceFiles//不同写法
}
artifacts {
archives androidSourceJar
archives androidJavadocJar
}
}
if (project.hasProperty("dependencies")) {
println("--all--->>dependencies")
}
}
5.3、在每个子module的build.gradle 中添加以下配置:
注意:不同的module,使用不同的版本和moduleName。
android{...}
apply from: '../release-aar.gradle'
uploadArchives {
repositories {
mavenDeployer {
pom.version = "${ipwVersionName}"
pom.artifactId = "${ipwModuleName}"
pom.name = "${ipwModuleName}"
pom.packaging = 'aar'
pom.whenConfigured { MavenPom pom ->
def dependencies = pom.dependencies.findAll { item ->
("unspecified" == item.version)
}
pom.dependencies.removeAll(dependencies)
}
}
}
}
5.4、分别执行uploadArchives 命令即可发布。
6、参考链接:
Gradle实战:发布aar包到maven仓库 (类似LegoArch)