forked from sensiasoft/sensorhub
-
Notifications
You must be signed in to change notification settings - Fork 16
/
common.gradle
378 lines (331 loc) · 12.5 KB
/
common.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
import java.nio.file.*;
ext.oshCoreVersion = '2.0-beta2'
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "biz.aQute.bnd:biz.aQute.bnd.gradle:6.1.0"
}
}
allprojects {
group = 'org.sensorhub'
repositories {
maven { url "https://repo.maven.apache.org/maven2" }
}
// set build number to HEAD SHA-1
def stdout = new ByteArrayOutputStream()
exec {
commandLine('git','rev-parse','--short','HEAD')
standardOutput = stdout
// hide errors and don't throw exception if not a git repo
errorOutput = new ByteArrayOutputStream()
ignoreExitValue = true
}
ext.buildNumber = "$stdout".trim()
}
subprojects {
apply plugin: 'java-library'
apply plugin: 'java-test-fixtures'
apply plugin: 'eclipse'
apply plugin: 'maven-publish'
sourceCompatibility = 11
targetCompatibility = 11
ext.details = null
ext.pom = {} // pom data that subprojects can append to
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
options.compilerArgs << "-Xlint:-options"
}
tasks.withType(Javadoc) {
options.encoding = 'UTF-8'
options.addStringOption('Xdoclint:none', '-quiet')
}
eclipse {
classpath {
downloadJavadoc = true
file.whenMerged {
entries.each {
if (it.hasProperty('exported'))
it.exported = true
}
}
}
}
// custom dependency configurations for embedding jars in OSGi bundle
configurations {
embeddedApi
embeddedImpl
embedded
embedded.extendsFrom(embeddedApi, embeddedImpl)
api.extendsFrom(embeddedApi)
implementation.extendsFrom(embeddedImpl)
}
// default test dependencies
dependencies {
testImplementation 'junit:junit:4.13'
testImplementation 'xmlunit:xmlunit:1.6'
}
// print test names
test {
testLogging {
events 'PASSED', 'FAILED'
showCauses true
showStackTraces true
exceptionFormat 'full'
}
}
// OSGi bundle jar task
task osgi(type: aQute.bnd.gradle.Bundle) {
from project.sourceSets.main.output
archiveClassifier = 'bundle'
bundle {
classpath = sourceSets.main.compileClasspath
}
// copy embedded libs into bundle jar
into('lib') {
from {
project.configurations.embedded
}
}
// helper method to list all non-empty packages in jar
ext.getPackagesFromJar = { jarFile, packageSet ->
//println jarFile
FileSystems.newFileSystem(jarFile.toPath(), ClassLoader.getSystemClassLoader()).withCloseable { fs ->
def rootDir = fs.getPath('/')
getPackagesFromDir(rootDir, packageSet)
}
}
// helper method to list all non-empty packages in class folder
ext.getPackagesFromDir = { rootDir, packageSet ->
rootDir.eachDirRecurse { dir ->
//println dir
// skip some directories
if (dir.toString().startsWith(File.separator + 'META-INF')) {
return;
}
// keep only packages that have java class files in them
Files.list(dir)
.filter { it.toString().endsWith('.class') }
.findAny()
.ifPresent { packageSet.add(rootDir.relativize(dir).toString()
.replace(java.io.File.separatorChar, (char)'.') + '.*') }
}
}
// help method to automatically find osgi activator class
ext.findActivator = {
for (def classDir: sourceSets.main.output.classesDirs) {
def rootDir = classDir.toPath()
def activatorClass = Files.walk(rootDir)
.filter { it.toString().endsWith('Activator.class') }
.map { rootDir.relativize(it).toString()
.replace(java.io.File.separatorChar, (char)'.')
.replaceAll('.class$', '') }
.findAny()
.orElse(null)
if (activatorClass)
return activatorClass
}
}
doFirst {
// configure bnd options before running the task
manifest {
//if (!attributes['Import-Package'])
// attributes 'Import-Package': '!java.*,!com.sun.*,!sun.*,!javax.xml.*,!org.xml.sax.*,!org.w3c.dom.*,*'
//attributes '-noimportjava': true
//attributes '-sources': true
// ignore some common errors
attributes '-fixupmessages': 'Classes found in the wrong directory; is:=ignore,' +
'Unused Import-Package instructions; is:=ignore,' +
'The default package \'.\' is not permitted by the Import-Package syntax; is:=ignore,' +
'Unused Export-Package instructions; is:=ignore,'
// disable DS annotation processing
if (!attributes['-dsannotations'])
attributes '-dsannotations': '!*'
// auto detect bundle activator if not configured
if (!attributes['Bundle-Activator']) {
def activatorClass = findActivator()
//println activatorClass
if (activatorClass)
attributes 'Bundle-Activator': activatorClass
}
// compute imports/exports based on gradle dependencies
def embeddedJars = project.configurations.embedded
def depJars = project.configurations.compileClasspath.minus(embeddedJars)
def apiJars = project.configurations.embeddedApi
// import all packages from all dependencies that are not embedded
def importedPackages = [] as Set
depJars.each {
getPackagesFromJar(it, importedPackages)
}
//println importedPackages
if (!attributes['Import-Package'])
attributes 'Import-Package': importedPackages.join(',')
// export packages in API dependencies and in this project
def exportedPackages = [] as Set
sourceSets.main.output.classesDirs.each {
getPackagesFromDir(it.toPath(), exportedPackages)
}
apiJars.each {
getPackagesFromJar(it, exportedPackages)
}
//println exportedPackages
if (!attributes['-exportcontents'])
attributes '-exportcontents': exportedPackages.join(',')
/*// add all dependencies that are not embedded to Require-Bundle
attributes 'Import-Package': '!*'
def embeddedDeps = project.configurations.embedded.allDependencies
def apiDeps = project.configurations.api.allDependencies.minus(embeddedDeps)
def implDeps = project.configurations.implementation.allDependencies.minus(apiDeps).minus(embeddedDeps)
def requiredBundles = ''
apiDeps.each {
if (!requiredBundles.isEmpty()) requiredBundles += ','
requiredBundles += it.group + '.' + it.name + ';bundle-version:="' + it.version + '";visibility:=reexport'
}
implDeps.each {
if (!requiredBundles.isEmpty()) requiredBundles += ','
requiredBundles += it.group + '.' + it.name + ';bundle-version:="' + it.version + '"'
}
attributes 'Require-Bundle': requiredBundles*/
/*// add embedded jars to OSGi classpath
// only add jars that were not substituted with newer version by gradle
def classpath = '.'
def embeddedJars = ''
def runtimeDeps = project.configurations.runtimeClasspath.collect { it.name };
project.configurations.embedded.each {
if (runtimeDeps.contains(it.name)) {
embeddedJars += 'lib/' + it.name + '=' + it.name + ';lib:=true,'
}
}
attributes '-includeresource': embeddedJars*/
// add embedded jars to OSGi classpath
// we do it like this so imports are not computed for these
def classpath = '.'
def embeddedClasspath = ''
def runtimeDeps = project.configurations.runtimeClasspath.collect { it.name }
project.configurations.embedded.each {
if (runtimeDeps.contains(it.name)) {
embeddedClasspath += 'lib/' + it.name + ','
}
}
attributes 'Bundle-ClassPath': embeddedClasspath + '.'
// auto-generate Bundle-NativeCode header if native libs are placed in
// the lib/native resource folder with the proper directory structure
def resourcesFolder = new File(projectDir, '/src/main/resources')
def nativeFolder = new File(resourcesFolder, '/lib/native')
if (nativeFolder.exists()) {
def currentPlatform = ''
def nativePaths = ''
fileTree(dir: nativeFolder).files.each {
def path = resourcesFolder.toPath().relativize(it.toPath())
if (path.nameCount != 5) {
throw new GradleException("Invalid native library path in resource folder: " + path +
". Path must be of the form 'lib/native/{osname}/{arch}/{libname}.{ext}'")
}
// lib paths must be grouped by platform (os and processor)
def osname = path.getName(2)
def proc = path.getName(3)
def platform = 'osname=' + osname + '; processor=' + proc
if (platform != currentPlatform) {
nativePaths = platform + (nativePaths.isEmpty() ? '' : ', ' + nativePaths)
currentPlatform = platform;
}
nativePaths = path.toString().replace('\\', '/') + '; ' + nativePaths
}
attributes 'Bundle-NativeCode': nativePaths
}
}
}
}
assemble.dependsOn osgi
// do stuff at the end in case subprojects add extra info
afterEvaluate { project ->
// set defaults for some OSGi manifest entries
project.osgi {
manifest {
// main info
attributes 'Bundle-SymbolicName': project.group + '.' + project.name
if (project.description != null && !attributes['Bundle-Name'])
attributes 'Bundle-Name': project.description
if (project.details != null && !attributes['Bundle-Description'])
attributes 'Bundle-Description': project.details
attributes 'Bundle-Version': project.version
if (project.buildNumber != null && !project.buildNumber.isEmpty())
attributes 'Bundle-BuildNumber': project.buildNumber
if (!attributes['Bundle-License'])
attributes 'Bundle-License': 'MPL 2.0 (http://mozilla.org/MPL/2.0)'
if (!attributes['Bundle-Copyright'] && attributes['Bundle-Vendor'])
attributes 'Bundle-Copyright': 'Copyright (c) ' + attributes['Bundle-Vendor'] + '. All Rights Reserved'
}
}
// also use osgi headers in JAR manifest
project.jar {
manifest {
from project.osgi.manifest
}
}
// maven artifact content
project.publishing {
publications {
mavenJava(MavenPublication) {
from components.java
pom.withXml {
asNode().get('version') + ({
resolveStrategy = Closure.DELEGATE_FIRST
name project.description
if (project.details != null)
description project.details
url 'http://www.opensensorhub.org'
licenses {
license {
name 'Mozilla Public License Version 2.0'
url 'http://www.mozilla.org/MPL/2.0'
distribution 'repo'
}
}
def repoName = projectDir.parentFile.name
scm {
url 'https://github.com/opensensorhub/' + repoName + '/tree/master/' + project.name
connection 'scm:git:git://github.com/opensensorhub/' + repoName + '.git'
}
issueManagement {
url 'https://github.com/opensensorhub/' + repoName + '/issues'
system 'GitHub Issues'
}
} >> project.pom)
}
}
}
}
}
// disable jar task if no source is included
if (!new File(project.projectDir, 'src').exists()) {
tasks.osgi.enabled = false
tasks.jar.enabled = false
}
// custom task to install in local maven repo
task install
install.dependsOn(build)
install.dependsOn(publishToMavenLocal)
}
// distribution zip files
apply plugin: 'java-library'
apply plugin: 'distribution'
targetCompatibility = 1.11
tasks.jar.enabled = false
afterEvaluate { // disable all distTar tasks
tasks.each {
if (it.name.endsWith('istTar'))
it.enabled = false
}
}
// collect all configured repositories in parent build
gradle.projectsEvaluated { g ->
if (gradle.parent != null) {
gradle.parent.rootProject {
repositories.addAll(g.rootProject.repositories)
}
}
}