-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.gradle.kts
More file actions
171 lines (154 loc) · 5.87 KB
/
build.gradle.kts
File metadata and controls
171 lines (154 loc) · 5.87 KB
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
162
163
164
165
166
167
168
169
170
171
// KTS教程: https://github.com/gradle/kotlin-dsl-samples/blob/master/samples/maven-publish/build.gradle.kts
import com.nova.build.Configuration
import org.gradle.api.Project
import java.io.ByteArrayOutputStream
// ------------------------------------------------------------------------
// 为下面配置AllProjects做编译导入支持
// ------------------------------------------------------------------------
plugins {
//alias(libs.plugins.spotless)
id("maven-publish")
id("java")
}
java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
buildscript {
repositories {
mavenLocal()
maven(uri("https://repo1.maven.org/maven2/"))
maven(uri("https://maven.aliyun.com/repository/public"))
maven(uri("https://maven.aliyun.com/repository/google"))
maven(uri("https://jitpack.io"))
google()
mavenCentral()
jcenter()
maven(uri("https://oss.sonatype.org/content/repositories/snapshots"))
}
dependencies {
classpath(libs.agp)
classpath(libs.kotlin.gradlePlugin)
// classpath("com.github.Onion99:Transformers:1.2")
}
}
apply(from = "$rootDir/exclude_other_version.gradle")
fun Project.getLatestGitTag(): String {
return try {
val stdout = ByteArrayOutputStream()
exec {
commandLine("git", "describe", "--tags", "--abbrev=0")
standardOutput = stdout
}
stdout.toString().trim().removePrefix("v")
} catch (e: Exception) {
// 如果没有 tag,返回默认版本
Configuration.pluginVersion
}
}
fun Project.getGitVersion(): String {
return try {
val stdout = ByteArrayOutputStream()
exec {
// 获取最近的 tag 和提交信息
commandLine("git", "describe", "--tags", "--long")
standardOutput = stdout
}
stdout.toString().trim()
} catch (e: Exception) {
Configuration.pluginVersion
}
}
allprojects {
val noTransformerProject = arrayOf("app", "Transformers","transformer","plugin")
if (!noTransformerProject.contains(name)) {
println("current project = ${name}")
apply(plugin = "maven-publish")
apply(plugin = "java")
val project = this
// 配置 Javadoc 任务
tasks.withType<Javadoc> {
options {
this as StandardJavadocDocletOptions
addStringOption("Xdoclint:none", "-quiet")
addStringOption("encoding", "UTF-8")
addStringOption("charSet", "UTF-8")
addBooleanOption("html5", true)
// 忽略 @hide 标签等错误
(this as CoreJavadocOptions).addStringOption("tag", "hide:X")
(this as CoreJavadocOptions).addStringOption("tag", "param:X")
(this as CoreJavadocOptions).addStringOption("tag", "return:X")
}
// 忽略 Javadoc 错误
isFailOnError = false
}
// 添加 sources jar
val sourcesJar by tasks.registering(Jar::class) {
archiveClassifier.set("sources")
from(sourceSets.main.get().allSource)
}
// 添加 javadoc jar
val javadocJar by tasks.registering(Jar::class) {
archiveClassifier.set("javadoc")
from(tasks.named("javadoc"))
}
val configurePublication: Action<MavenPublication> = Action {
val publication = this
// 对应发布的包域
group = Configuration.pluginGroup
// 使用最近的 Git tag 作为版本号
version = project.getLatestGitTag()
artifactId = project.name
// 根据不同的 publication 类型选择合适的配置
if (plugins.hasPlugin("java-gradle-plugin")) {
// 对于 gradle plugin,不需要手动添加 java component
artifact(sourcesJar.get())
artifact(javadocJar.get())
} else {
// 对于普通的 java 库
from(components.getByName("java"))
artifact(sourcesJar.get())
artifact(javadocJar.get())
}
// 配置 POM
pom {
name.set(project.name)
description.set("Transformer plugin for Android")
url.set("https://github.com/onion99/transformer")
licenses {
license {
name.set("The Apache License, Version 2.0")
url.set("http://www.apache.org/licenses/LICENSE-2.0.txt")
}
}
developers {
developer {
id.set("onion99")
name.set("onion99")
email.set("891564341@qq.com")
}
}
scm {
connection.set("scm:git:git://github.com/onion99/transformer.git")
developerConnection.set("scm:git:ssh://github.com/onion99/transformer.git")
url.set("https://github.com/onion99/transformer")
}
}
}
afterEvaluate {
publishing {
repositories {
maven {
// change to point to your repo, e.g. http://my.org/repo
url = uri("$buildDir/repo")
}
}
publications {
if (plugins.hasPlugin("java-gradle-plugin")){
withType(MavenPublication::class).configureEach(configurePublication)
}else register("mavenJava", MavenPublication::class).configure(configurePublication)
}
}
}
}
}