-
Notifications
You must be signed in to change notification settings - Fork 38
/
archetype-post-generate.groovy
170 lines (138 loc) · 6.36 KB
/
archetype-post-generate.groovy
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
import java.nio.file.Files
import org.apache.commons.io.FileUtils
def jakartaVersion = request.properties["jakartaVersion"].trim()
def profile = request.properties["profile"].trim()
def javaVersion = request.properties["javaVersion"].trim()
def runtime = request.properties["runtime"].trim()
def docker = request.properties["docker"].trim()
def outputDirectory = new File(request.getOutputDirectory(), request.getArtifactId())
validateInput(jakartaVersion, profile, javaVersion, runtime, outputDirectory)
generateRuntime(runtime, jakartaVersion, docker, outputDirectory)
bindEEPackage(jakartaVersion, outputDirectory)
generateDocker(docker, runtime, outputDirectory)
generateMavenWrapper(outputDirectory)
printSummary()
private validateInput(jakartaVersion, profile, javaVersion, runtime, File outputDirectory){
if (profile.equalsIgnoreCase("core") && jakartaVersion != '10') {
FileUtils.forceDelete(outputDirectory)
throw new RuntimeException("Failed, the Core Profile is only supported for Jakarta EE 10")
}
if (runtime.equalsIgnoreCase("payara") && (jakartaVersion != '8') && (javaVersion == '8')) {
FileUtils.forceDelete(outputDirectory)
throw new RuntimeException("Failed, Payara 6 does not support Java SE 8")
}
if (runtime.equalsIgnoreCase("glassfish")) {
if (profile.equalsIgnoreCase("core")) {
FileUtils.forceDelete(outputDirectory)
throw new RuntimeException("Failed, GlassFish does not support the Core Profile")
}
if ((jakartaVersion != '8') && (javaVersion == '8')) {
FileUtils.forceDelete(outputDirectory)
throw new RuntimeException("Failed, GlassFish 7 does not support Java SE 8")
}
}
if (runtime.equalsIgnoreCase("tomee")) {
if (jakartaVersion == '10') {
FileUtils.forceDelete(outputDirectory)
throw new RuntimeException("Failed, TomEE does not yet support Jakarta EE 10")
}
if (!profile.equalsIgnoreCase("web")) {
FileUtils.forceDelete(outputDirectory)
throw new RuntimeException("Failed, TomEE does not support the full and Core Profiles")
}
if ((jakartaVersion != '8') && (javaVersion == '8')) {
FileUtils.forceDelete(outputDirectory)
throw new RuntimeException("Failed, TomEE 9 does not support Java SE 8")
}
}
if (runtime.equalsIgnoreCase("wildfly")) {
if ((jakartaVersion == '9') || (jakartaVersion == '9.1')) {
FileUtils.forceDelete(outputDirectory)
throw new RuntimeException("Failed, WildFly does not offer a release for Jakarta EE 9 or Jakarta EE 9.1")
}
if ((jakartaVersion == '10') && (javaVersion == '8')) {
FileUtils.forceDelete(outputDirectory)
throw new RuntimeException("Failed, WildFly 27 does not support Java SE 8")
}
}
if (runtime.equalsIgnoreCase("open-liberty") && (jakartaVersion == '10')) {
FileUtils.forceDelete(outputDirectory)
throw new RuntimeException("Failed, Open Liberty does not yet support Jakarta EE 10")
}
}
private generateRuntime(runtime, jakartaVersion, docker, File outputDirectory) {
switch (runtime) {
case "glassfish": println "Generating code for GlassFish"
if (docker.equalsIgnoreCase("yes")) {
println "WARNING: GlassFish does not support Docker"
FileUtils.forceDelete(new File(outputDirectory, "Dockerfile"))
}
break
case "tomee": println "Generating code for TomEE"
if (jakartaVersion != '8') {
println "WARNING: TomEE 9 is not yet a production ready release"
}
break
case "payara": println "Generating code for Payara"
break
case "wildfly": println "Generating code for WildFly"
break
case "open-liberty": println "Generating code for Open Liberty"
break
default: println "No runtime will be included in the sample"
}
if (!runtime.equalsIgnoreCase("open-liberty")) {
// We do not need the liberty configuration directory, let's delete it.
FileUtils.forceDelete(new File(outputDirectory, "src/main/liberty"))
}
}
private bindEEPackage(jakartaVersion, File outputDirectory) {
def eePackage = 'jakarta';
if (jakartaVersion == '8') {
eePackage = 'javax'
}
println "Binding EE package: " + eePackage
def binding = ["eePackage": eePackage]
def engine = new groovy.text.SimpleTemplateEngine()
outputDirectory.traverse(type: groovy.io.FileType.FILES, nameFilter: ~/.*\.(xml|java)$/) { it ->
if (!it.name.endsWith("pom.xml")) {
it.withReader('UTF-8') { reader ->
try {
def template = engine.createTemplate(reader).make(binding)
new FileWriter(it).write(template)
} catch (ignored) {
println ignored
}
}
}
}
}
private generateDocker(docker, runtime, File outputDirectory) {
if (docker.equalsIgnoreCase("no")) {
println "Docker support was not requested"
FileUtils.forceDelete(new File(outputDirectory, "Dockerfile"))
} else if (runtime.equalsIgnoreCase("none")) {
println "WARNING: Docker support is not possible without choosing a runtime"
FileUtils.forceDelete(new File(outputDirectory, "Dockerfile"))
}
}
private generateMavenWrapper(File outputDirectory) {
println "Adding Maven Wrapper"
def pomFile = new File(outputDirectory, "pom.xml")
def isWindows = System.properties['os.name'].toLowerCase().contains('windows')
def starter = isWindows ? "cmd.exe" : "/bin/sh"
def switcher = isWindows ? "/c" : "-c"
def command = "mvn -f \"${pomFile.getAbsolutePath()}\" wrapper:wrapper"
def output = new StringBuilder()
def proc = [starter, switcher, command].execute();
proc.consumeProcessOutput(output, output)
proc.waitFor()
if (proc.exitValue() != 0 || output == null || !output.contains("BUILD SUCCESS")) {
println("${output}")
FileUtils.forceDelete(outputDirectory)
throw new RuntimeException("Failed to generate Maven wrapper.")
}
}
private printSummary(){
println "The README.md file in the " + request.properties["artifactId"] + " directory explains how to run the generated application"
}