1
+ /*
2
+ * SPDX-License-Identifier: Apache-2.0
3
+ *
4
+ * The OpenSearch Contributors require contributions made to
5
+ * this file be licensed under the Apache-2.0 license or a
6
+ * compatible open source license.
7
+ */
8
+
9
+ /*
10
+ * Modifications Copyright OpenSearch Contributors. See
11
+ * GitHub history for details.
12
+ */
13
+
14
+ import com.github.jk1.license.ProjectData
15
+ import com.github.jk1.license.render.ReportRenderer
16
+ import de.undercouch.gradle.tasks.download.Download
17
+ import java.io.FileWriter
18
+
19
+ buildscript {
20
+ repositories {
21
+ mavenLocal()
22
+ maven(url = " https://aws.oss.sonatype.org/content/repositories/snapshots" )
23
+ mavenCentral()
24
+ maven(url = " https://plugins.gradle.org/m2/" )
25
+ }
26
+ dependencies {
27
+ " classpath" (group = " org.opensearch.gradle" , name = " build-tools" , version = " 3.0.0-SNAPSHOT" )
28
+ }
29
+ }
30
+
31
+ plugins {
32
+ application
33
+ id(" com.github.jk1.dependency-license-report" ) version " 2.9"
34
+ id(" org.owasp.dependencycheck" ) version " 10.0.2"
35
+ id(" com.diffplug.spotless" ) version " 6.25.0"
36
+ id(" de.undercouch.download" ) version " 5.6.0"
37
+ }
38
+ apply (plugin = " opensearch.repositories" )
39
+ apply (plugin = " org.owasp.dependencycheck" )
40
+
41
+ val runtimeJavaVersion = (System .getProperty(" runtime.java" )?.toInt())?.let (JavaVersion ::toVersion) ? : JavaVersion .current()
42
+ logger.quiet(" =======================================" )
43
+ logger.quiet(" Runtime JDK Version : $runtimeJavaVersion " )
44
+ logger.quiet(" Gradle JDK Version : " + JavaVersion .current())
45
+ logger.quiet(" =======================================" )
46
+
47
+ java {
48
+ targetCompatibility = JavaVersion .VERSION_11
49
+ sourceCompatibility = JavaVersion .VERSION_11
50
+
51
+ withJavadocJar()
52
+ withSourcesJar()
53
+
54
+ toolchain {
55
+ languageVersion = JavaLanguageVersion .of(runtimeJavaVersion.majorVersion)
56
+ vendor = JvmVendorSpec .ADOPTIUM
57
+ }
58
+ }
59
+
60
+ application {
61
+ mainClass.set(" org.opensearch.client.codegen.CodeGenerator" )
62
+ applicationDefaultJvmArgs = listOf (
63
+ " --add-exports" , " jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED" ,
64
+ " --add-exports" , " jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED" ,
65
+ " --add-exports" , " jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED" ,
66
+ " --add-exports" , " jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED" ,
67
+ " --add-exports" , " jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED"
68
+ )
69
+ }
70
+
71
+ val localSpecification = " $projectDir /opensearch-openapi.yaml"
72
+
73
+ tasks.create<Download >(" downloadLatestSpec" ) {
74
+ src(" https://github.com/opensearch-project/opensearch-api-specification/releases/download/main-latest/opensearch-openapi.yaml" )
75
+ dest(localSpecification)
76
+ }
77
+
78
+ tasks.named<JavaExec >(" run" ) {
79
+ args = listOf (
80
+ " --input" , localSpecification,
81
+ " --eclipse-config" , " $rootDir /buildSrc/formatterConfig-generated.xml" ,
82
+ " --output" , " ${project(" :java-client" ).projectDir} /src/generated/java/"
83
+ )
84
+ }
85
+
86
+ tasks.withType<ProcessResources > {
87
+ expand(
88
+ " version" to version,
89
+ " git_revision" to (if (rootProject.extra.has(" gitHashFull" )) rootProject.extra[" gitHashFull" ] else " unknown" )
90
+ )
91
+ }
92
+
93
+ tasks.withType<Javadoc >().configureEach{
94
+ options {
95
+ encoding = " UTF-8"
96
+ }
97
+ }
98
+
99
+ tasks.withType<Jar > {
100
+ doFirst {
101
+ if (rootProject.extra.has(" gitHashFull" )) {
102
+ val jar = this as Jar
103
+ jar.manifest.attributes[" X-Git-Revision" ] = rootProject.extra[" gitHashFull" ]
104
+ jar.manifest.attributes[" X-Git-Commit-Time" ] = rootProject .extra[" gitCommitTime" ]
105
+ } else {
106
+ throw GradleException (" No git information available" )
107
+ }
108
+ }
109
+
110
+ manifest {
111
+ attributes[" Implementation-Title" ] = " OpenSearch Java client code generator"
112
+ attributes[" Implementation-Vendor" ] = " OpenSearch"
113
+ attributes[" Implementation-URL" ] = " https://github.com/opensearch-project/opensearch-java/"
114
+ attributes[" Build-Date" ] = rootProject.extra[" buildTime" ]
115
+ }
116
+
117
+ metaInf {
118
+ from(" ../LICENSE.txt" )
119
+ from(" ../NOTICE.txt" )
120
+ }
121
+ }
122
+
123
+ tasks.withType<Test > {
124
+ useJUnitPlatform()
125
+ testLogging {
126
+ events(" passed" , " skipped" , " failed" )
127
+ }
128
+ }
129
+
130
+ tasks.build {
131
+ dependsOn(" spotlessJavaCheck" )
132
+ }
133
+
134
+ dependencies {
135
+ // Apache 2.0
136
+ implementation(" io.swagger.parser.v3" , " swagger-parser" , " 2.1.22" )
137
+
138
+ // (New) BSD
139
+ implementation(" com.samskivert" , " jmustache" , " 1.16" )
140
+
141
+ // Apache 2.0
142
+ implementation(" commons-cli" , " commons-cli" , " 1.8.0" )
143
+ implementation(" commons-codec" , " commons-codec" , " 1.17.0" )
144
+ implementation(" commons-logging" , " commons-logging" , " 1.3.2" )
145
+ implementation(" org.apache.commons" , " commons-lang3" , " 3.14.0" )
146
+ implementation(" org.apache.commons" , " commons-text" , " 1.12.0" )
147
+ implementation(" org.apache.logging.log4j" , " log4j-api" , " [2.17.1,3.0)" )
148
+ implementation(" org.apache.logging.log4j" , " log4j-core" , " [2.17.1,3.0)" )
149
+ implementation(" org.apache.logging.log4j" , " log4j-slf4j2-impl" , " [2.17.1,3.0)" )
150
+
151
+ // Apache 2.0
152
+ implementation(" com.fasterxml.jackson.core" , " jackson-core" , " 2.17.1" )
153
+ implementation(" com.fasterxml.jackson.core" , " jackson-databind" , " 2.17.1" )
154
+
155
+ // Apache 2.0
156
+ implementation(" com.diffplug.spotless" , " spotless-lib" , " 2.45.0" )
157
+ implementation(" com.diffplug.spotless" , " spotless-lib-extra" , " 2.45.0" )
158
+
159
+ // Apache 2.0
160
+ // https://search.maven.org/artifact/com.google.code.findbugs/jsr305
161
+ implementation(" com.google.code.findbugs" , " jsr305" , " 3.0.2" )
162
+
163
+ // Apache 2.0
164
+ compileOnly(" org.jetbrains" , " annotations" , " 24.1.0" )
165
+
166
+ // Apache 2.0
167
+ implementation(" org.apache.maven.resolver" , " maven-resolver-api" , " 1.9.20" )
168
+ implementation(" org.apache.maven.resolver" , " maven-resolver-supplier" , " 1.9.20" )
169
+
170
+ // MIT
171
+ implementation(" org.semver4j" , " semver4j" , " 5.3.0" )
172
+
173
+ // EPL-2.0
174
+ testImplementation(platform(" org.junit:junit-bom:5.10.3" ))
175
+ testImplementation(" org.junit.jupiter" , " junit-jupiter" )
176
+ testRuntimeOnly(" org.junit.platform" , " junit-platform-launcher" )
177
+ }
178
+
179
+ licenseReport {
180
+ renderers = arrayOf(SpdxReporter (rootProject.layout.buildDirectory.file(" release/codegen-dependencies.csv" ).get().getAsFile()))
181
+ excludeGroups = arrayOf()
182
+ }
183
+
184
+ class SpdxReporter (val dest : File ) : ReportRenderer {
185
+ // License names to their SPDX identifier
186
+ val spdxIds = mapOf (
187
+ " Apache 2" to " Apache-2.0" ,
188
+ " Apache 2.0" to " Apache-2.0" ,
189
+ " Apache-2.0" to " Apache-2.0" ,
190
+ " \" Apache-2.0\" ;link=\" https://www.apache.org/licenses/LICENSE-2.0.txt\" " to " Apache-2.0" ,
191
+ " \" Apache 2.0\" ;link=\" http://www.apache.org/licenses/LICENSE-2.0.txt\" " to " Apache-2.0" ,
192
+ " \" Apache License 2.0\" ;link=\" http://www.apache.org/licenses/LICENSE-2.0.html\" " to " Apache-2.0" ,
193
+ " Apache License 2.0" to " Apache-2.0" ,
194
+ " Apache License, version 2.0" to " Apache-2.0" ,
195
+ " Apache License, Version 2.0" to " Apache-2.0" ,
196
+ " Apache Software License, version 2.0" to " Apache-2.0" ,
197
+ " The Apache License, Version 2.0" to " Apache-2.0" ,
198
+ " The Apache Software License, Version 2.0" to " Apache-2.0" ,
199
+ " BSD Zero Clause License" to " 0BSD" ,
200
+ " The (New) BSD License" to " BSD-3-Clause" ,
201
+ " EDL 1.0" to " BSD-3-Clause" ,
202
+ " Eclipse Distribution License - v 1.0" to " BSD-3-Clause" ,
203
+ " Eclipse Distribution License (New BSD License)" to " BSD-3-Clause" ,
204
+ " Eclipse Public License 2.0" to " EPL-2.0" ,
205
+ " Eclipse Public License v. 2.0" to " EPL-2.0" ,
206
+ " Eclipse Public License - v 2.0" to " EPL-2.0" ,
207
+ " GNU General Public License, version 2 with the GNU Classpath Exception" to " GPL-2.0 WITH Classpath-exception-2.0" ,
208
+ " COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0" to " CDDL-1.0" ,
209
+ " Lesser General Public License, version 3 or greater" to " LGPL-3.0+" ,
210
+ " MIT" to " MIT" ,
211
+ " MIT License" to " MIT" ,
212
+ " The MIT License" to " MIT" ,
213
+ " Mozilla Public License, Version 2.0" to " MPL-2.0" ,
214
+ " Public Domain" to " PUBLIC-DOMAIN"
215
+ )
216
+
217
+ private fun quote (str : String ) : String {
218
+ return if (str.contains(' ,' ) || str.contains(" \" " )) {
219
+ " \" " + str.replace(" \" " , " \"\" " ) + " \" "
220
+ } else {
221
+ str
222
+ }
223
+ }
224
+
225
+ override fun render (data : ProjectData ? ) {
226
+ dest.parentFile.mkdirs()
227
+ FileWriter (dest).use { out ->
228
+ out .append(" name,url,version,revision,license\n " )
229
+ data?.allDependencies?.forEach { dep ->
230
+ val info = com.github.jk1.license.render.LicenseDataCollector .multiModuleLicenseInfo(dep)
231
+
232
+ val depVersion = dep.version
233
+ val depName = dep.group + " :" + dep.name
234
+ val depUrl = if (info.moduleUrls.isNotEmpty()) { info.moduleUrls.first() } else { " <NO MODULE URL>" }
235
+
236
+ val licenseIds = info.licenses.mapNotNull { license ->
237
+ license.name?.let {
238
+ checkNotNull(spdxIds[it]) { " No SPDX identifier for $license " }
239
+ }
240
+ }.toSet()
241
+
242
+ // Combine multiple licenses.
243
+ // See https://spdx.github.io/spdx-spec/appendix-IV-SPDX-license-expressions/#composite-license-expressions
244
+ val licenseId = licenseIds.joinToString(" OR " )
245
+ out .append(" ${quote(depName)} ,${quote(depUrl)} ,${quote(depVersion)} ,,${quote(licenseId)} \n " )
246
+ }
247
+ }
248
+ }
249
+ }
250
+
251
+ tasks.withType<Jar > {
252
+ doLast {
253
+ ant.withGroovyBuilder {
254
+ " checksum" (" algorithm" to " md5" , " file" to archiveFile.get())
255
+ " checksum" (" algorithm" to " sha1" , " file" to archiveFile.get())
256
+ " checksum" (" algorithm" to " sha-256" , " file" to archiveFile.get(), " fileext" to " .sha256" )
257
+ " checksum" (" algorithm" to " sha-512" , " file" to archiveFile.get(), " fileext" to " .sha512" )
258
+ }
259
+ }
260
+ }
261
+
262
+ spotless {
263
+ java {
264
+ target(" **/*.java" )
265
+
266
+ licenseHeaderFile(" ../LICENSE_HEADER.txt" )
267
+
268
+ // Use the default importOrder configuration
269
+ importOrder()
270
+ removeUnusedImports()
271
+
272
+ eclipse().configFile(" ../buildSrc/formatterConfig-generated.xml" )
273
+
274
+ trimTrailingWhitespace()
275
+ endWithNewline()
276
+ }
277
+ }
0 commit comments