-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.gradle.kts
166 lines (143 loc) · 4.63 KB
/
build.gradle.kts
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
import com.google.protobuf.gradle.*
import java.net.URI
import java.util.*
plugins {
id("java")
id("maven-publish")
id("com.google.protobuf") version "0.8.18"
}
group = "org.dlms"
version = "1.0.0"
repositories {
mavenCentral()
google()
}
dependencies {
// proto buffer
compileOnly("com.google.protobuf:protobuf-java:4.28.2")
// grpc
compileOnly("io.grpc:grpc-netty-shaded:1.68.0")
compileOnly("io.grpc:grpc-protobuf:1.68.0")
compileOnly("io.grpc:grpc-stub:1.68.0")
compileOnly("io.grpc:protoc-gen-grpc-java:1.68.0")
// annotation
compileOnly("javax.annotation:javax.annotation-api:1.3.2")
testImplementation(platform("org.junit:junit-bom:5.10.0"))
testImplementation("org.junit.jupiter:junit-jupiter")
}
tasks.register<Zip>("zipProto") {
doFirst {
println("Running zipping...")
}
from("src/main/proto")
archiveBaseName.set("dlms-proto")
version = project.findProperty("publishVersion").toString()
archiveClassifier.set("proto")
destinationDirectory.set(layout.buildDirectory.dir("dist"))
}
publishing {
publications {
create<MavenPublication>("maven") {
from(components["java"])
// Add the proto files as an artifact with a classifier
artifact(tasks["zipProto"])
pom {
name = "DLMS Protos"
description = "A proto library for dlms schemas."
url.set("https://github.com/bxute/dlms-schemas")
groupId = "org.dlms.protos"
val newVersion = project.findProperty("publishVersion").toString()
version = newVersion
licenses {
license {
name.set("MIT License")
url.set("https://opensource.org/licenses/MIT")
}
}
developers {
developer {
id.set("org.dlms")
name.set("Ankit Kumar")
email.set("[email protected]")
}
}
scm {
url.set("https://github.com/bxute/dlms-schemas")
}
}
}
}
repositories {
maven {
name = "GitHubPackages"
url = URI.create("https://maven.pkg.github.com/bxute/dlms-schemas") // For release
credentials {
username = (project.findProperty("gpr.user") ?: "").toString()
password = (project.findProperty("gpr.token") ?: "").toString()
}
}
}
}
fun incrementVersion(version: String): String {
val parts = version.split(".")
if (parts.size < 3) {
throw IllegalArgumentException("Version format should be MAJOR.MINOR.PATCH")
}
val patch = parts[2].toInt() + 1 // Increment patch version
return "${parts[0]}.${parts[1]}.$patch" // Reconstruct the version string
}
tasks.register("updatePublishVersion") {
doLast {
println("updating version...")
val gradlePropertiesFile = File(rootDir, "gradle.properties")
val properties = Properties()
if (gradlePropertiesFile.exists()) {
gradlePropertiesFile.inputStream().use { properties.load(it) }
}
val newVersion = incrementVersion(project.findProperty("publishVersion").toString())
// Modify properties as needed
properties["publishVersion"] = newVersion
println("new version: $newVersion")
gradlePropertiesFile.outputStream().use { properties.store(it, "Updated by updateGradleProperties task") }
}
}
// Update version before publish
tasks.publish {
dependsOn("updatePublishVersion")
dependsOn("testGeneratedCode")
}
val generatedProtoDirectory = layout.buildDirectory.dir("generated/source/proto/main/java")
protobuf {
protoc {
artifact = "com.google.protobuf:protoc:3.24.4"
}
generatedFilesBaseDir = generatedProtoDirectory.get().toString()
plugins {
id("grpc") {
artifact = "io.grpc:protoc-gen-grpc-java:1.68.0"
}
}
generateProtoTasks {
all().forEach { task ->
task.plugins {
id("grpc") {}
}
task.doFirst {
println("Generating proto files...")
}
}
}
}
// Custom task to compile generated Java code
tasks.register("testGeneratedCode") {
dependsOn("compileJava")
doLast {
println("Running testGeneratedCode...")
}
}
tasks.named("compileJava").configure {
dependsOn("generateProto")
doFirst {
println("Running compileJava...")
}
}