-
Notifications
You must be signed in to change notification settings - Fork 0
/
maven-publish-jitpack.gradle
161 lines (143 loc) · 5.27 KB
/
maven-publish-jitpack.gradle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// 拉取不下则复制本地引用 apply from: rootProject.file('maven-publish-jitpack.gradle')
// 发布配置 https://developer.android.google.cn/studio/build/maven-publish-plugin?hl=en#groovy
apply plugin: 'maven-publish'
// 不使用缓存,使用仓库中最新的包
configurations.all {
// 动态版本 x.x.+
resolutionStrategy.cacheDynamicVersionsFor 0, 'seconds'
// 变化版本 x.x.x
resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
}
// ----------------------- 在gradle.properties下配置
// POM_GROUP_ID
// POM_NAME
// POM_ARTIFACT_ID
// POM_VERSION
// POM_PACKAGING
// POM_LOCAL_PATH
// 完整结构如下 [groupId]:[artifactId]:[version]
// 公司或者组织域名倒序
def _groupId = hasProperty('POM_GROUP_ID') ? POM_GROUP_ID : 'com.github.y1xian'
// 项目名 (github后需带项目名)
def _groupName = hasProperty("POM_NAME") ? POM_NAME : 'android'
// 模块名
def _artifactId = hasProperty("POM_ARTIFACT_ID") ? POM_ARTIFACT_ID : 'core'
// 版本号
def _version = hasProperty('POM_VERSION') ? POM_VERSION : '1.0.0-SNAPSHOT'
// 类型,aar、jar、aab
def _packaging = hasProperty('POM_PACKAGING') ? POM_PACKAGING : 'aar'
// 本地仓库地址
def _localPath = hasProperty('POM_LOCAL_PATH') ? POM_LOCAL_PATH : ''
println("===> groupId: $_groupId ,groupName $_groupName ,artifactId: $_artifactId , version: $_version , packaging: $_packaging ,localPath $_localPath")
// ----------------------- 扩展方法
// android插件
def hasAndroidPlugin = plugins.hasPlugin("com.android.library")
// java插件
def hasJavaPlugin = plugins.hasPlugin("java-library") || plugins.hasPlugin('java')
// 快照版本,则为本地maven
def isSnapshot = {
return _version.endsWith("SNAPSHOT")
}
def enableLocalMaven = (_localPath != '/' && _localPath != '' && _localPath != null)
println("===> 版本号 $_version $isSnapshot")
println("===> 是否开启本地maven $enableLocalMaven $_localPath")
// -----------------------
task sourceJar(type: Jar) {
if (hasAndroidPlugin) {
task javadoc(type: Javadoc) {
source = android.sourceSets.main.java.source
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
failOnError false
}
task javadocJar(type: Jar, dependsOn: javadoc) {
archiveClassifier = 'javadoc'
from javadoc.destinationDir
}
task sourcesJar(type: Jar) {
archiveClassifier = 'sources'
from android.sourceSets.main.java.source
}
} else if (hasJavaPlugin) {
task javadocJar(type: Jar, dependsOn: javadoc) {
archiveClassifier = 'javadoc'
from javadoc.destinationDir
}
task sourcesJar(type: Jar, dependsOn: classes) {
archiveClassifier = 'sources'
from sourceSets.main.allSource
}
}
}
if (JavaVersion.current().isJava8Compatible()) {
allprojects {
tasks.withType(Javadoc) {
options.addStringOption('Xdoclint:none', '-quiet')
}
}
}
// 控制台中文设置UTF-8
tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
}
// ----------------------- 发布maven配置
afterEvaluate {
publishing {
if (hasAndroidPlugin) {
println("当前模块 $project , 发布aar, $enableLocalMaven")
if (enableLocalMaven) {
// 本地测试配置
// 发布配置
publications {
// 起个啥名都行,发布路径 Gradle/Tasks/publishing/publish
debug(MavenPublication) {
from components.debug
version = _version
groupId = "$_groupId.$_groupName"
}
}
// 发布到本地仓
repositories {
maven {
url = uri(_localPath)
println("===> android 发布到本地仓 $_localPath")
}
}
} else {
// 发布到 jitpack.io
publications {
release(MavenPublication) {
from components.release
groupId = _groupId
}
}
}
} else if (hasJavaPlugin || _packaging == 'jar') {
println("当前模块 $project , 发布jar, $enableLocalMaven")
if (enableLocalMaven) {
// 本地测试配置
publications {
debug(MavenPublication) {
from components.java
version = _version
groupId = "$_groupId.$_groupName"
}
}
// 发布到本地仓
repositories {
maven {
url = uri(_localPath)
println("===> java 发布到本地仓 $_localPath")
}
}
} else {
// 发布到 jitpack.io
publications {
release(MavenPublication) {
from components.java
groupId = _groupId
}
}
}
}
}
}