上传 jar 包到 Nexus 私服最简单的莫过于在网页上传,只需要手动选择需要上传的文件即可。在这里介绍下利用gradle上传jar包。上传Maven仓库主要可以使用maven-publish和maven插件,例子如下。
使用maven-publish插件
1. 添加插件
apply plugin: 'maven-publish'
2. 打包jar
task buildJar(dependsOn: ['assembleRelease'], type: Jar) {
//后缀名
extension = "jar"
//最终的 Jar 包名
archiveName = "${project.getName()}-release.jar"
//需打包的资源所在的路径集
def srcClassDir = [project.buildDir.absolutePath + "/intermediates/classes/release"]
def srcKotlinClassDir = [project.buildDir.absolutePath + "/tmp/kotlin-classes/release"]
//初始化资源路径集
from srcClassDir
from srcKotlinClassDir
//去掉不要的类
exclude('io/reactivex/android/R.class')
//需要打包的类
include('com/smartahc/android/smartble/*.class')
include('com/smartahc/android/smartble/**/*.class')
}
3. 配置Maven仓库信息
publishing {
publications {
mavenJava(MavenPublication) {
groupId GROUP_ID
version = VERSION_NAME
artifactId ARTIFACT_ID
// Tell maven to prepare the generated "*.jar" file for publishing
def artifactPath = "$buildDir/libs/${project.getName()}-release.jar"
println(artifactPath)
artifact(artifactPath)
}
}
repositories {
maven {
// 指定要上传的maven私服仓库
url = MAVEN_LOCAL_PATH
//认证用户和密码
credentials {
username localRespos.userName
password localRespos.password
}
}
}
}
使用maven插件
1. 添加插件
apply plugin: 'maven'
2. 配置仓库信息
afterEvaluate { project ->
uploadArchives {
repositories.mavenDeployer {
pom.groupId = localRespos.GROUP_ID
pom.artifactId = localRespos.POM_ARTIFACT_ID
pom.version = localRespos.VERSION_NAME
repository(url: getRepositoryUrl()) {
authentication(userName: localRespos.NEXUS_USERNAME, password: localRespos.NEXUS_PASSWORD)
}
}
}
task buildJar(dependsOn: ['assembleRelease'], type: Jar) {
extension = "jar" //后缀名
//最终的 Jar 包名
archiveName = "${project.getName()}-release.jar"
//需打包的资源所在的路径集
def srcClassDir = [project.buildDir.absolutePath + "/intermediates/classes/release"]
def srcKotlinClassDir = [project.buildDir.absolutePath + "/tmp/kotlin-classes/release"]
//初始化资源路径集
from srcClassDir
from srcKotlinClassDir
//去掉不要的类
exclude('io/reactivex/android/R.class')
//需要打包的类
include('com/smartahc/android/smartble/*.class')
include('com/smartahc/android/smartble/**/*.class')
}
artifacts {
archives buildJar
}
}