文章来自:http://blog.csdn.net/intbird 转载请说明出处
目录
源码github: https://github.com/intbird/maven-publish
主要内容为:
封装以下两个插件为自有gradle插件并发布到自有仓库
apply plugin: ‘maven’
uploadArchives
apply plugin: ‘maven-publish’
publishing
1.仓库搭建
1.安装nexus
1. linux私有仓库的搭建nexus
1. nexus2 手动安装
wget https://download.sonatype.com/nexus/nexus-professional-bundle-latest.tar.gz
tar -xzvf nexus-latest-bundle.tar.gz
#修改文件所有者
#export RUN_AS_USER=root
sudo chown -R $user nexus/
sh nexus-2.14.17-01/bin/nexus start
2. nexus3 一键安装
yum install -y java
nexus3_file=nexus-3.26.1-02-unix.tar.gz
# nexus
wget http://download.sonatype.com/nexus/3/$nexus3_file
mkdir nexus
sudo tar -xzvf $nexus3_file -C nexus
rm -rf $nexus3_file
#修改文件所有者
#export RUN_AS_USER=root
sudo chown -R $user nexus/
sudo nexus/nexus*/bin/nexus start
curl localhost:8081
2. mac私有仓库的搭建nexus
比较简单,直接下载包后解压即可
如果是个人机器搭建话,因为没有公有ip,所以
1, 将电脑ip固定为局域网地址,方便域内访问,见下图
2,使用ngwork进行内网穿透,如果要固定地址,则收费
2.私有仓库允许匿名用户登录
访问仓库:
1.地址: http://intbird.net:8081/nexus/
2.用户名: admin
3.密码: admin123
4.密码配置: nexus/sonatype-work/nexus/conf/security.xmllinux配置: 一个md5后的字符串,反解一下得到密码: admin123
mac配置: 第一次登陆页面会提示admin密码在哪个目录下面,复制登陆即可
3.配置统一访问仓库地址
public仓库添加releases和snapshots
这样在项目里引入一个 public地址即可
如 引入一个public即可:
4.允许匿名用户登陆
如果不设置,则再androidstudio中无法下载该pom
5.新建一个repo_user用户
将使用此用户,而非admin, 在后续android studio中发布进行认证
如:
6.如何发布到jcenter
网络资源过多,这里也无需多言
文章来自:http://blog.csdn.net/intbird 转载请说明出处
7.异常情况
1.java需求内存无法满足启动 : JVM内存调整
vi /nexus/nexus-3.26.1-02/bin/nexus.vmoptions
SYSTEM org.eclipse.jetty.util.log.StdErrLog - 2020-08-20 12:24:10.983:INFO:oejs.session:jetty-main-1: node0 Stopped scavenging
ERROR [jetty-main-1] *SYSTEM java.lang.Throwable - Exception in thread "jetty-main-1" java.lang.OutOfMemoryError: Java heap space
2. 防火墙阻止8081端口访问
firewall-cmd --add-port=8081/tcp --permanent
2. Gradle发布脚步
后续做成插件也是对这两个官方插件进行封装而已
1. 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")
}
}
}
}
2. 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.net'
}
}
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
}
}
}
}
}
3.封装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.net:8081/nexus/content/repositories/snapshots/"
String repoRelease = "http://intbird.net: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
4.将自定义的插件和需要的配置引入到主项目
并测试发布snapshot
1.add maven url in root project build.gradle
file
buildscript {
ext {
publish_version = '1.0.0'
}
repositories {
maven { url "http://intbird.net: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.net:8081/nexus/content/repositories/snapshots/"
repoRelease = "http://intbird.net:8081/nexus/content/repositories/releases/"
repoName = "repo_user"
repoPassword = "repo_password"
}
5.查看和运行发布task
6.查看是否发布成功(文件是否存在)
7. pom和sign
待补充
3.源码:
源码github: https://github.com/intbird/maven-publish
文章来自:http://blog.csdn.net/intbird 转载请说明出处