文章來自:http://blog.csdn.net/intbird 轉載請說明出處
源碼github: https://github.com/intbird/maven-publish
主要內容爲:
封裝以下兩個插件爲自有gradle插件併發布到自有倉庫
apply plugin: ‘maven’
uploadArchives
apply plugin: ‘maven-publish’
publishing
1.倉庫搭建
1.linux私有倉庫的搭建nexus
1.下載:
wget https://download.sonatype.com/nexus/nexus-professional-bundle-latest.tar.gz
2.解壓:
tar -xzvf nexus-latest-bundle.tar.gz
3.運行
export RUN_AS_USE=root
sh nexus-2.14.17-01/bin/nexus start
2.mac私有倉庫的搭建nexus
比較簡單,直接下載包後解壓即可
如果是個人機器搭建話,因爲沒有公有ip,所以
1, 將電腦ip固定爲局域網地址,方便域內訪問,見下圖
2,使用ngwork進行內網穿透,如果要固定地址,則收費
2.私有倉庫允許匿名用戶登錄
1.訪問倉庫:
1.地址: http://intbird.world:8081/nexus/
2.用戶名: admin
3.密碼: admin123
4.密碼配置: nexus/sonatype-work/nexus/conf/security.xml
linux配置: 一個md5後的字符串,反解一下得到密碼: admin123
mac配置: 第一次登陸頁面會提示admin密碼在哪個目錄下面,複製登陸即可
2.public倉庫添加releases和snapshots
這樣在項目裏引入一個 public地址即可
如 引入一個public即可:
3.允許匿名用戶登陸
如果不設置,則再androidstudio中無法下載該pom
4.新建一個repo_user用戶
將使用此用戶,而非admin, 在後續android studio中發佈進行認證
如:
3.如何發佈到jcenter
網絡資源過多,這裏也無需多言
文章來自:http://blog.csdn.net/intbird 轉載請說明出處
2.插件發佈
後續做成插件也是對這兩個官方插件進行封裝而已
0. maven upload 插件用法
apply plugin: 'maven'
uploadArchives {
repositories {
mavenDeployer {
def hostUrl = "172.16.0.172:8081"
def url = version.endsWith('SNAPSHOT') ?
"http://$hostUrl/repository/maven-snapshots/" :
"http://$hostUrl/repository/maven-releases/"
repository(url: url) {
authentication(userName: "seal", password: "dingtone123")
}
}
}
}
1. maven-publish 在主項目中的寫法和用法
apply plugin: 'maven-publish'
def GroupId = 'intbird.soft.gradle'
def ArtifactId = 'maven-publish'
def Version = '1.0.0-SNAPSHOT'
def userName = "repo_user"
def passWord = "repo_password"
afterEvaluate {
publishing {
publications {
release(MavenPublication) {
// Applies the component for the release build variant.
from components.release
// You can then customize attributes of the publication as shown below.
groupId = GroupId
artifactId = ArtifactId
version = Version
pom {
name = "SimpleLibPom"
description = "Simple Lib Project Pom."
url = 'http://intbird.world'
}
}
debug(MavenPublication) {
// Applies the component for the debug build variant.
from components.debug
groupId = GroupId
artifactId = ArtifactId
version = Version
}
}
repositories {
maven {
def releasesRepoUrl = "http://172.16.66.172:8081/repository/maven-releases/"
def snapshotsRepoUrl = "http://172.16.66.172:8081/repository/maven-snapshots/"
url = project.hasProperty('release') ? releasesRepoUrl : snapshotsRepoUrl
credentials {
username = userName
password = passWord
}
}
}
}
}
2.封裝publish-maven爲自定義的gradle插件
每次引入gradle文件看起來也專業,所以封裝一下, 方便後續lib項目引入
注意:新建的項目爲groovy項目,且添加的是.groovy結尾的文件,不要添加java文件
代碼封裝1: PublishConfig
發佈時的一些外部配置
class PublishConfig {
Boolean snapshot = true
String groupId = ""
String artifactId = ""
String version = ""
String pomName = ""
String pomDescription = ""
String pomUrl = ""
String repoSnapshot = "http://intbird.world:8081/nexus/content/repositories/snapshots/"
String repoRelease = "http://intbird.world:8081/nexus/content/repositories/releases/"
String repoName = "repo_user"
String repoPassword = "repo_password"
}
代碼封裝2: PublishMaven
發佈主代碼
class PublishMaven implements Plugin<Project> {
def publishConfig = "publishConfig"
@Override
void apply(Project project) {
def publishingConfig = project.extensions.create(publishConfig, PublishConfig)
project.plugins.apply MavenPublishPlugin
PublishingExtension publishing = project.extensions.getByType(PublishingExtension)
project.afterEvaluate {
for (SoftwareComponent components : project.components) {
publishing.publications({ publications ->
publications.create(components.name, MavenPublication.class, { MavenPublication publication ->
publication.groupId = publishingConfig.groupId
publication.artifactId = publishingConfig.artifactId
publication.version = publishingConfig.version
publication.from(components)
publication.pom {
mavenPom -> configPom(mavenPom, publishingConfig)
}
})
})
}
publishing.repositories { artifactRepositories ->
artifactRepositories.maven { mavenArtifactRepository ->
mavenArtifactRepository.url = publishingConfig.snapshot ? publishingConfig.repoSnapshot : publishingConfig.repoRelease
mavenArtifactRepository.credentials {
credentials ->
credentials.username = publishingConfig.repoName
credentials.password = publishingConfig.repoPassword
}
}
}
}
}
static void configPom(MavenPom mavenPom, PublishConfig config) {
mavenPom.name = config.pomName
mavenPom.description = config.pomDescription
mavenPom.url = config.pomUrl
}
}
代碼封裝3: 將插件發佈到私有maven
使用maven upload
3.將自定義的插件和需要的配置引入到主項目
並測試發佈snapshot
1.add maven url in root project build.gradle
file
buildscript {
ext {
publish_version = '1.0.0'
}
repositories {
maven { url "http://intbird.world:8081/nexus/content/repositories/public/" }
}
...
}
文章來自:http://blog.csdn.net/intbird 轉載請說明出處
2 add plugin in your lib project(you want to publish) build.gradle
file
apply plugin: 'publish-maven'
android {
...
}
publishConfig {
groupId = 'intbird.soft.lib'
artifactId = '$your-lib-name' // use '-' connect
version = "1.0.0-SNAPSHOT"
// if you want publish to your own repository, mdf the flow config
repoSnapshot = "http://intbird.world:8081/nexus/content/repositories/snapshots/"
repoRelease = "http://intbird.world:8081/nexus/content/repositories/releases/"
repoName = "repo_user"
repoPassword = "repo_password"
}
3.查看和運行發佈task
4.查看是否發佈成功(文件是否存在)
4,pom和sign
待補充
5.源碼:
源碼github: https://github.com/intbird/maven-publish
文章來自:http://blog.csdn.net/intbird 轉載請說明出處