-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathtycho_build.gradle
572 lines (487 loc) · 17.2 KB
/
tycho_build.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
/*
Build Script can be executed via 'gradle install'
Build Script Dependencies can be downloaded via 'gradle collectDepenencies'
Executing the Build Script and download Dependencies can be executed via 'gradle completeInstall'
For a complete classPath Refresh please execute 'gradle deleteFromClassPath setClassPath'
Pom Generation can be executed via gradle deploy
If you have a product definition please add a buildProduct = true to your main build.gradle script
*/
apply plugin: 'base'
import groovy.io.FileType
project.ext.tychoVersion = "4.0.8"
try {
workspacePath = workspacePath
} catch (MissingPropertyException e) {
project.ext.workspacePath = ""
}
try {
dependencyFolder = dependencyFolder
} catch (MissingPropertyException e) {
project.ext.dependencyFolder = "lib/dependencies/" // Folder in each subproject where
}
try {
features = features
} catch (MissingPropertyException e) {
project.ext.features = []
for (int i = 0; i < subprojects.name.size(); i++){
if (subprojects.name[i] ==~ /.*\.[fF]eature/) {
features.add(subprojects.name[i])
}
}
}
try {
repositoryName = repositoryName
} catch (MissingPropertyException e) {
project.ext.repositoryName = groupID + ".repository"
}
try {
parentID = parentID
} catch (MissingPropertyException e) {
project.ext.parentID = groupID + ".parent"
}
// Add certain Jar to Library
def addLibToCP(def project, def libPar) {
def filePar = new File(workspacePath + project + "/.classpath")
boolean notYetAdded = true
filePar.eachLine {line ->
def pattern = ".*${dependencyFolder}${libPar}.*"
if (line ==~ /${pattern}/) {
notYetAdded = false
}
}
if (notYetAdded) {
File newCP = new File(workspacePath + project + '/.cp')
filePar.eachLine {line ->
if (line ==~ /.*<\/classpath>.*/) {
String entry = '\t<classpathentry exported="true" kind="lib" path="' + dependencyFolder + libPar + '"/>' + '\n</classpath>\n'
line = line.replaceAll(/<\/classpath>/, entry)
println project + " : " + libPar + " added to classpath"
newCP << line
} else {
newCP << line + "\n"
}
}
filePar.delete()
newCP.renameTo(workspacePath + project + '/.classpath')
} else {
println project + " : " + libPar + " classpath entry already present"
}
}
// deletes whole DependenciesLibrary Folder
def deleteLibFromCP(def project) {
def filePar = new File(workspacePath + project + "/.classpath")
boolean deleteCP = false
def newCP = new File(workspacePath + project + '/cp')
def pattern = ".*${dependencyFolder}[^<].*/>"
filePar.eachLine {line ->
if (line ==~ /\n(\ |\t)*/) {
// delete empty lines
line = line.replaceAll(/\n(\ |\t)*/, '')
}
if (line ==~ /${pattern}/) {
deleteCP = true
def pattern2 = '(\\ |\\t)*<classpathentry exported="true" kind="lib" path="' + dependencyFolder + "[^<].*"+'"/>(\\ |\\t)*'
line = line.replaceAll(/${pattern2}/, '')
newCP << line
} else {
newCP << line+"\n"
}
}
if (!deleteCP) {
println project + " : no dependencies from " + dependencyFolder + " found in classpath file! "
newCP.delete()
} else {
filePar.delete()
newCP.renameTo(workspacePath + project + '/.classpath')
println workspacePath + project + '/.classpath' + "!"
}
}
// Adds a Library to the current Bundle-ClassPath of the Manifest file
def addRunTimeLib(String libPar, String projectPar) {
def mf = new File(projectPar + "/META-INF/MANIFEST.MF")
def newMf = new File(projectPar + "/MF")
newMf.delete()
mf.eachLine {line ->
if (line ==~ /.*Bundle-ClassPath:.*\.jar.*/) {
def libsInLine = line.replaceAll(/[\ \t]*Bundle-ClassPath:[\ \t]*/, '')
line = line.replaceFirst(/[^\ \t]*\.jar[\ \,]*/, dependencyFolder + libPar + ",\n " + libsInLine) // /[^\ \t]*.\.jar[\ \,]*/
} else {
if (line ==~ /.*Bundle-ClassPath: \.[\ \t]*/) {
line = line + ",\n " + dependencyFolder + libPar
} else if (line ==~ /.*Bundle-ClassPath: \.\,[\ \t]*/) {
line = line + "\n " + dependencyFolder + libPar + ","
}
}
newMf << line + "\n"
}
mf.delete()
newMf.renameTo(projectPar + "/META-INF/MANIFEST.MF")
}
// Creates a Bundle-ClassPath Section in the Manifest file
def createRunTimeLib(String libPar, String projectPar) {
def mf = new File(projectPar + "/META-INF/MANIFEST.MF")
mf << "Bundle-ClassPath: lib/dependencies/" + libPar
}
// checks for a Bundle-ClassPath section in the Manifest file
def boolean checkForRunTimeLibs(String projectPar) {
boolean returnBool = false
def mf = new File(projectPar + "/META-INF/MANIFEST.MF")
mf.eachLine {line ->
if (line ==~ /.*Bundle-ClassPath:.*/) {
returnBool = true
}
}
return returnBool
}
// checks if lib is already present in Manifest file
// if lib is present but is referenced from another folder than dependencyFolder the library from dependencyFolder will be referenced
def boolean checkRunTimeLib(String libPar, String projectPar){
// checks if lib is in RunTimeLibrary present and changes reference if necessary
def mf = new File(projectPar + "/META-INF/MANIFEST.MF")
def newMf = new File(projectPar + "/MF")
newMf.delete()
boolean returnBool = false
boolean changed = false
mf.eachLine {line ->
if (line ==~ /.*${libPar}.*/) {
if (!(line ==~ /.*(\ |\t)*${dependencyFolder}${libPar}(\ |\t)*.*/)){
// if lib is already referenced
// reference lib from dependencyFolder
line = line.replaceFirst( /[^\ \t]*${libPar}/, dependencyFolder+libPar )
changed = true
println line
println "yolo ${libPar}\n"
}
returnBool = true
}
newMf << line + "\n"
}
if (changed) {
mf.delete()
newMf.renameTo(projectPar + "/META-INF/MANIFEST.MF")
} else {
newMf.delete()
}
return returnBool
}
///////////////////////////////////
// !!! DEFINING SUB PROJECTS !!! //
///////////////////////////////////
subprojects {
apply plugin: 'base'
apply plugin: 'java'
// apply plugin: 'eclipse'
task deleteArtifacts(type: Delete) {
delete 'target', 'pom.xml', dependencyFolder
}
// Copy Dependencies into subprojects DependencyFolder
task collectDependencies(type: Copy) {
from configurations.runtimeClasspath
into "${dependencyFolder}"
}
task setClassPath(dependsOn: 'collectDependencies') {
description = "\tAdds all your Dependencies from your local lib folder in each project to it's classpath"
doLast {
def dependencyList = []
def depsFolder = new File(workspacePath + project.name + "/" + dependencyFolder)
if (depsFolder.exists()) {
depsFolder.eachFile() {file ->
if (!(file.getName() ==~ /.*\.txt/)) {
dependencyList << file.getName()
}
}
if (features.every {it != project.name}) {
boolean BundleClassPathPresent = checkForRunTimeLibs(project.name)
for (int icp = 0; icp < dependencyList.size; icp++) {
// Adds Lib to .classPath file
addLibToCP(project.name, dependencyList[icp])
if (!BundleClassPathPresent) {
// Adds Lib to Manifest File
createRunTimeLib(dependencyList[icp], project.name)
BundleClassPathPresent = true
} else if (!checkRunTimeLib(dependencyList[icp], project.name)) {
// if library not present add it to RunTimeLibrary
addRunTimeLib(dependencyList[icp], project.name)
}
}
// could still be usefull for debugging, that's why it's not deleted
/*dependencyList.each {dep ->
println project.name + ": " + dep
}*/
}
def warningReadMe = new File(workspacePath + project.name + "/" + dependencyFolder + "_README.txt")
warningReadMe.delete()
warningReadMe << "Do Not Remove any Jars/Libraries in this Folder!\nThis folder contains all of your dependencies defined in your gradle script.\n"
warningReadMe << "Removing or renaming any of these files will result in an Error in your .classpath file\n"
warningReadMe << "If any error concerning missing dependencies should occur please run 'gradle deleteFromClassPath setClassPath' in your workspace folder from your shell."
}
}
}
task deleteFromClassPath(){
description = "\tDeletes all your Dependencies located in your local lib folder from each project's classpath"
doLast {
boolean depsDelete = false
def depsFolder = new File(workspacePath + project.name + '/' + dependencyFolder)
if (depsFolder.exists()) {
depsDelete = true
}
depsFolder.deleteDir()
if (features.every {it != project.name} && depsDelete) {
deleteLibFromCP(project.name)
}
}
}
// For a complete classPath Refresh please execute 'gradle deleteFromClassPath setClassPath'
////////////////////////////////////////////////////////////////////////////////////////////
task deploy() {
description = "\tGenerating the Tycho Poms. Please remember to add a '.qualifier' to the version numbers!"
doLast {
String versionNumber = 'Version Number Error:\tcheck Manifest for Bundle-Version Number and make sure to add a ".qualifier" to the version numbers!\n'
String artifactId = 'Could not find Bundle-SymbolicName in Manifest.file'
// -- In case of changed Manifest File in Eclipse:
// Version Numbers of the projects are collected via
// regular expressions in the Manifest.MF File.
// versionnumber of the projects are equal to their
// Bundle-Version Number
// Generating Poms for sub projects except features
if (features.every {it != project.name}) {
def content = new File(workspacePath + "${project.name}/META-INF/MANIFEST.MF").getText("UTF-8")
def printFileLine = {
if (it ==~ /Bundle-Version.+qualifier/) {
versionNumber = it.substring(16) // possibile error: cuts off first 16 chars
// Version Number is taken from Bundle-Version in Manifest.MF
// If there is no Bundle-Version or the versionnumber needs to
// be taken from a different key word, please change the
// regular expression and the substring above
versionNumber = versionNumber.replace(".qualifier", "-SNAPSHOT")
}
// Artifact ID is taken from Bundle-SymbolicName minus the 16 chars ';singleton:=true'
if (it ==~ /Bundle-SymbolicName:.+/) {
artifactId = it
artifactId = artifactId.replace("Bundle-SymbolicName:", '')
artifactId = artifactId.replace(";", '')
artifactId = artifactId.replace(" ", '')
artifactId = artifactId.replace("singleton:=true", '')
}
}
content.eachLine(printFileLine)
def f = new File(workspacePath + artifactId + '/pom.xml')
f.delete()
if (project.name ==~ /.*\.[tT]ests*/) {
// Test Cases
f << start() + elder() + testArtifact(artifactId, versionNumber) + end()
} else {
// Normal Plugin
f << start() + elder() + artifact(artifactId, versionNumber) + end()
// old pom.xml files are deleted and replaced by new auto generated Tycho pom.xml files
}
} else if (features.any {it == project.name}) {
// Features
def parsedXml = new XmlParser().parse(workspacePath + "${project.name}/feature.xml")
artifactId = parsedXml.attribute("id")
versionNumber = parsedXml.attribute("version")
versionNumber = versionNumber.replace(".qualifier", "-SNAPSHOT")
def f = new File(workspacePath + artifactId + '/pom.xml')
f.delete()
f << feature(artifactId, versionNumber)
}
}
}
}
clean {
dependsOn += subprojects.deleteArtifacts
}
// define Parent
task createParent() {
doLast {
new File(workspacePath + "${parentID}").mkdir()
String versionNumber = '1.0.0.qualifier'
def f = new File(workspacePath + parentID + '/pom.xml')
f.delete()
f << parentPom(parentID)
for (int i = 0; i < targetRepositories.size(); i++) {
f << repos(targetRepositories[i], i)
}
f << endRepos()
f << moduleStart()
subprojects.each {subproj ->
f << module(subproj.name)
}
f << module(repositoryName)
f << endParent()
}
}
task deleteParent(type: Delete) {
delete(workspacePath + "${parentID}")
}
clean.dependsOn(deleteParent)
// define Repository
task createRepository() {
doLast{
def pom = new File(workspacePath + repositoryName + '/pom.xml')
pom.delete()
pom << reposi()
}
}
task deleteRepository(type: Delete) {
delete(workspacePath + repositoryName + "/pom.xml")
delete(workspacePath + repositoryName + "/target")
}
clean.dependsOn(deleteRepository)
task createPoms(dependsOn: [createParent, createRepository, subprojects.deploy])
task install(dependsOn: [createPoms], type: Exec) {
description = "\tExecutes a 'mvn install' of the parent pom.xml and auto-generates Tycho Poms"
commandLine 'mvn', 'install', '-f', workspacePath + parentID + '/pom.xml'
}
task prepareMaven(dependsOn: [subprojects.collectDependencies, createPoms])
task completeInstall(dependsOn: [prepareMaven], type: Exec) {
description = "\tCopies dependencies into dependencyFolder of each subproject and executes a 'mvn install' of the parent pom.xml and auto-generates Tycho Poms"
commandLine 'mvn', 'install', '-f', workspacePath + parentID + '/pom.xml'
}
// --- Defining Tycho POM parts -- //
def artifact(artifactId,versionNumber) {"""
<groupId>${groupID}</groupId>
<artifactId>${artifactId}</artifactId>
<version>${versionNumber}</version>
<packaging>eclipse-plugin</packaging>
<build>
<plugins>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-compiler-plugin</artifactId>
<version>\${tycho-version}</version>
<configuration>
<release>11</release>
</configuration>
</plugin>
</plugins>
</build>
"""}
def testArtifact(artifactId,versionNumber) {"""
<groupId>${groupID}</groupId>
<artifactId>${artifactId}</artifactId>
<version>${versionNumber}</version>
<packaging>eclipse-test-plugin</packaging>
<build>
<plugins>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-surefire-plugin</artifactId>
<version>\${tycho-version}</version>
</plugin>
</plugins>
</build>
"""}
def start() {"""\
<?xml version="1.0" encoding="UTF-8"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
"""}
def elder() {"""
<parent>
<groupId>${groupID}</groupId>
<artifactId>${parentID}</artifactId>
<version>1.0.0.qualifier</version>
<relativePath>../${parentID}/pom.xml</relativePath>
</parent>
"""}
def end() {"""
</project>
"""}
// -- defining Parent Pom -- //
def parentPom(artifactId) {"""\
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>${groupID}</groupId>
<artifactId>${artifactId}</artifactId>
<version>1.0.0.qualifier</version>
<packaging>pom</packaging>
<!-- this is the parent POM from which all modules inherit common settings -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<tycho-version>${tychoVersion}</tycho-version>
</properties>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<url>https://repo1.maven.org/maven2</url>
</pluginRepository>
</pluginRepositories>
<repositories>
<!-- configure p2 repository to resolve against -->
"""}
def repos(String targetRepo, int i) {"""
<repository>
<id>targetRepository${i}</id>
<layout>p2</layout>
<url>${targetRepo}</url>
</repository>
"""}
def endRepos() {"""
</repositories>
<build>
<plugins>
<plugin>
<!-- enable tycho build extension -->
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-maven-plugin</artifactId>
<version>${tychoVersion}</version>
<extensions>true</extensions>
</plugin>
</plugins>
</build>
"""}
def moduleStart() {"""
<!-- the modules that should be built together -->
<modules>
"""}
def module(String project) {"""\
<module>../${project}</module>
"""}
def endParent() {"""\
</modules>
</project>
"""}
// end of defining parent pom.xml
// repository Pom
def reposi() {"""\
<?xml version="1.0" encoding="UTF-8"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>${groupID}</groupId>
<artifactId>${parentID}</artifactId>
<version>1.0.0.qualifier</version>
<relativePath>../${parentID}/pom.xml</relativePath>
</parent>
<groupId>${groupID}</groupId>
<artifactId>${repositoryName}</artifactId>
<version>1.0.0.qualifier</version>
<packaging>eclipse-repository</packaging>
</project>
"""}
// feature pom
def feature(artifactId, versionNumber) {"""\
<?xml version="1.0" encoding="UTF-8"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>${groupID}</groupId>
<artifactId>${parentID}</artifactId>
<version>1.0.0.qualifier</version>
<relativePath>../${parentID}/pom.xml</relativePath>
</parent>
<groupId>${groupID}</groupId>
<artifactId>${artifactId}</artifactId>
<version>${versionNumber}</version>
<packaging>eclipse-feature</packaging>
</project>
"""}
// end of feature pom
// Build Script can be executed via gradle install
// For a complete classPath Refresh please execute 'gradle deleteFromClassPath setClassPath'
// Pom Generation can be executed via gradle deploy