Maven发布公共类库
前言
本文只介绍如何向已有私仓发布类库的方法,如需学习如何搭建,传送门在此:Nexus搭建Maven私仓。
步骤
建立一个publish.gradle文件,内容如下:
apply plugin: ‘maven’ apply plugin: ‘signing’ // 判断是否是SNAPSHOT版 def isReleaseBuild() { return VERSION_NAME.contains(“SNAPSHOT”) == false } // 获取Repo的用户名 def getRepositoryUsername() { return hasProperty(‘NEXUS_USERNAME’) ? NEXUS_USERNAME : “” } // 获取Repo的密码 def getRepositoryPassword() { return hasProperty(‘NEXUS_PASSWORD’) ? NEXUS_PASSWORD : “” } // 数字签名 signing { required { isReleaseBuild() && gradle.taskGraph.hasTask(“uploadArchives”) } sign configurations.archives } afterEvaluate { project -> // 我们上传的核心 uploadArchives { repositories { mavenDeployer { // 这个repo的groupID pom.groupId = GROUP // 这个repo的artifactID pom.artifactId = POM_ARTIFACT_ID // 版本号,一定要注意版本号带了-SNAPSHOT就会存储到SNAPSHOT库中 pom.version = VERSION_NAME // 这是配置release库的auth信息 repository(url: RELEASE_REPOSITORY_URL) { authentication(userName: getRepositoryUsername(), password: getRepositoryPassword()) } // 这是配置snapshot库的auth信息 snapshotRepository(url: SNAPSHOT_REPOSITORY_URL) { authentication(userName: getRepositoryUsername(), password: getRepositoryPassword()) } } } } // 这特么一堆是用来上传源码和JavaDoc的,需要的就解开注释,那个encoding的配置一定要注意; // task androidJavadocs(type: Javadoc) { // source = android.sourceSets.main.java.srcDirs // classpath += project.files(android.getBootClasspath().join(File.pathSeparator)) // options { // encoding "UTF-8" // } // exclude '**/BuildConfig.java' // exclude '**/R.java' // // } // 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 // archives androidJavadocsJar // 这里配置的是上传我们编译出来的aar,如果想上传jar,这里需要修改成jar路径即可 "${project.buildDir}/outputs/aar/${project.name}-release.aar" } }
针对第一步中的NEXUS_USERNAME、NEXUS_PASSWOERD这些信息,我们将其存放到gradle.properties里面,当然,你熟悉了配置在gradle中也可以,这里我们在module下新建一个gradle.properties,填充如下内容:
#GROUP=对应maven的groupId值 #VERSION_NAME=对应maven的version值,如果名字中包含SNAPSHOT字符,项目将会发布到snapshots仓库,没有则发布到releases仓库 #POM_ARTIFACT_ID=对应maven的artifactId值 #SNAPSHOT_REPOSITORY_URL=snapshots仓库地址 #RELEASE_REPOSITORY_URL=releases仓库地址 #NEXUS_USERNAME=登录nexus oss的用户名 #NEXUS_PASSWORD=登录nexus oss的密码 # 下面这个是一个栗子 POM_ARTIFACT_ID=demo-helloworld GROUP=unic.demo.helloworld VERSION_NAME=1.1.0 SNAPSHOT_REPOSITORY_URL=http://****/nexus/content/repositories/snapshots/ RELEASE_REPOSITORY_URL=http://****/nexus/content/repositories/releases/ NEXUS_USERNAME=your username NEXUS_PASSWORD=your password
最后到我们需要发布的module的build.gradle中追加下面这句,另:from后面指向publish.gradle的文件地址即可,也就是说可以用相对路径或者绝对路径,
apply from:'publish.gradle'
即可在gradle面板在该module下找到一个uploadArchives方法,双击它,即可完成发布过程。或者你可以直接在这个module下面命令行执行:
gradle uploadArchives
也可以完成发布过程。
如何使用
在需要引用该库的项目中的build.gradle中配置好
... repositories { maven { url '发布的仓库的URL引用地址' } }
然后就跟普通的引用一个用法:
compile 'unic.demo.helloworld:demo-helloworld:1.1.0'
附:Maven仓库命令上传(TODO)
有同事提到需要直接上传文件,如果管理员没有屏蔽nexus的网页上传方式的话,可以直接在网页上操作,此处不再赘述。
下面介绍使用命令上传方式进行上传。