forked from line/armeria
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjava.gradle
250 lines (219 loc) · 9.53 KB
/
java.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
import java.util.regex.Pattern
// Determine which version of JDK should be used for builds.
def buildJdkVersion = Integer.parseInt(JavaVersion.current().getMajorVersion())
if (rootProject.hasProperty('buildJdkVersion')) {
def jdkVersion = Integer.parseInt(String.valueOf(rootProject.findProperty('buildJdkVersion')))
if (buildJdkVersion != jdkVersion) {
buildJdkVersion = jdkVersion
logger.quiet("Overriding JDK for build with ${buildJdkVersion}")
}
}
logger.info("Using JDK ${buildJdkVersion} for build")
rootProject.ext.set("buildJdkVersion", buildJdkVersion)
// Enable checkstyle if the rule file exists.
def checkstyleConfigDir = "${rootProject.projectDir}/settings/checkstyle"
def checkstyleEnabled = !rootProject.hasProperty('noCheckstyle') &&
!rootProject.hasProperty('noLint') &&
new File(checkstyleConfigDir).isDirectory() &&
new File("${checkstyleConfigDir}/checkstyle.xml").isFile()
configure(rootProject) {
apply plugin: 'eclipse'
apply plugin: 'idea'
}
/**
* Checks each flag of the specified project for flags of format "java(\\d+)".
* If such a flag exists, the minimum Java version is extracted and returned.
* Otherwise, {@code null} is returned.
*/
static def extractTargetJavaVersion(Project project) {
def pattern = Pattern.compile('^java(\\d+)$')
def flags = project.ext.flags
for (def flag : flags) {
def matcher = pattern.matcher(flag)
if (!matcher.matches()) {
continue
}
return Integer.valueOf(matcher.group(1))
}
return null
}
configure(projectsWithFlags('java')) {
apply plugin: 'java-library'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'jvm-test-suite'
base {
archivesName = project.ext.artifactId
}
// Delete the generated source directory on clean.
ext {
genSrcDir = "${projectDir}/gen-src"
}
clean {
delete project.ext.genSrcDir
}
def minimumJavaVersion = null
if (project.hasProperty('minimumJavaVersion')) {
minimumJavaVersion = Integer.valueOf(String.valueOf(project.findProperty('minimumJavaVersion')))
if (minimumJavaVersion < 8) {
throw new IllegalArgumentException("'minimumJavaVersion' cannot be smaller than 8. " +
"minimumJavaVersion: ${minimumJavaVersion}")
}
}
// parse flags for the targetJavaVersion
def targetJavaVersion = extractTargetJavaVersion(project)
if (targetJavaVersion != null && targetJavaVersion < 8) {
throw new IllegalArgumentException("The minimum target Java version ${version} specified " +
"for '${project.path}' cannot be smaller than 8")
}
if (minimumJavaVersion != null && (targetJavaVersion == null || targetJavaVersion < minimumJavaVersion)) {
targetJavaVersion = minimumJavaVersion
logger.quiet("Overriding target Java version for ${project.path} with ${targetJavaVersion}")
}
def targetJavaVersionStr = null
if (targetJavaVersion != null) {
targetJavaVersionStr = JavaVersion.toVersion(targetJavaVersion).toString()
}
def javaSourceCompatibility = targetJavaVersionStr ?: project.findProperty('javaSourceCompatibility') ?: '1.8'
def javaTargetCompatibility = targetJavaVersionStr ?: project.findProperty('javaTargetCompatibility') ?: '1.8'
// The default targetJavaVersion is 'javaTargetCompatibility'
targetJavaVersion = Integer.valueOf(JavaVersion.toVersion(javaTargetCompatibility).majorVersion)
project.ext.set('targetJavaVersion', targetJavaVersion)
def testJavaVersion = buildJdkVersion
if (project.hasProperty('testJavaVersion')) {
def testVersion = Integer.parseInt(String.valueOf(project.findProperty('testJavaVersion')))
if (testJavaVersion != testVersion) {
testJavaVersion = testVersion
logger.quiet("Overriding JRE for testing ${project.path} with ${testJavaVersion}")
}
}
logger.info("Using JRE ${testJavaVersion} to test ${project.path}")
project.ext.set("testJavaVersion", testJavaVersion)
java {
toolchain {
languageVersion = JavaLanguageVersion.of(rootProject.ext.buildJdkVersion)
implementation = JvmImplementation.VENDOR_SPECIFIC
}
}
afterEvaluate {
// Add the generated source directories to the source sets.
// This should run in 'afterEvaluate' so that protobuf plugin which also adds
// generated files to source set applied first.
project.sourceSets.all { sourceSet ->
def javaSrcDir = file("${project.ext.genSrcDir}/${sourceSet.name}/java")
def resourceSrcDir = file("${project.ext.genSrcDir}/${sourceSet.name}/resources")
if (!sourceSet.java.srcDirs.contains(javaSrcDir)) {
sourceSet.java.srcDir javaSrcDir
}
if (!sourceSet.resources.srcDirs.contains(resourceSrcDir)) {
sourceSet.resources.srcDir resourceSrcDir
}
}
Task generateSourcesTask = project.tasks.findByName('generateSources')
if (generateSourcesTask != null) {
tasks.sourcesJar.dependsOn(generateSourcesTask)
}
}
java {
withJavadocJar()
withSourcesJar()
// Adds an optional configuration that can be used to define optional dependencies. While it's more
// idiomatic to define features for each set of optional dependencies (e.g., "prometheus",
// "dropwizard"), it's convenient to have a single one to fallback to.
registerFeature('optional') {
usingSourceSet(sourceSets.main)
}
// Do not let Gradle infer the module path if automatic module name is enabled,
// because it means the JAR will rely on JDK's automatic module metadata generation.
if (project.ext.automaticModuleName != null) {
modularity.inferModulePath = false
}
}
// Set the sensible compiler options.
tasks.withType(JavaCompile) {
sourceCompatibility = javaSourceCompatibility
targetCompatibility = javaTargetCompatibility
if (rootProject.ext.buildJdkVersion >= 9) {
// Supported since java 9 https://openjdk.org/jeps/247
options.release.set(targetJavaVersion)
}
options.encoding = 'UTF-8'
options.warnings = false
options.compilerArgs += '-parameters'
}
// Set the 'Automatic-Module-Name' property in 'MANIFEST.MF' if `automaticModuleName` is not null.
if (project.ext.automaticModuleName != null) {
tasks.named('jar') {
doFirst {
manifest {
attributes('Automatic-Module-Name': project.ext.automaticModuleName.get())
}
}
}
}
project.ext.configureFlakyTests = { Test testTask ->
def flakyTests = rootProject.findProperty('flakyTests')
if (flakyTests == 'true') {
testTask.options {
includeTags 'FLAKY_TESTS'
}
} else if (flakyTests == 'false') {
testTask.options {
excludeTags 'FLAKY_TESTS'
}
} else if (flakyTests != null) {
throw new IllegalArgumentException("flakyTests: $flakyTests (expected: true, false or null)")
}
}
project.ext.configureCommonTestSettings = { Test testTask ->
testTask.testLogging.exceptionFormat = 'full'
// Use a different JRE for testing if necessary.
if (rootProject.ext.buildJdkVersion != project.ext.testJavaVersion) {
testTask.javaLauncher = javaToolchains.launcherFor {
languageVersion = JavaLanguageVersion.of(project.ext.testJavaVersion)
}
}
// disable tests for projects which target a higher java version
if (project.ext.testJavaVersion < project.ext.targetJavaVersion) {
project.logger.lifecycle("Skipping tests for ${project.path} since the " +
"testJavaVersion(${project.ext.testJavaVersion}) is smaller than the " +
"targetJavaVersion(${project.ext.targetJavaVersion})")
testTask.enabled = false
}
if (testTask.enabled) {
project.ext.configureFlakyTests(testTask)
}
}
testing.suites {
test {
// Use JUnit platform.
useJUnitJupiter()
targets.configureEach {
testTask.configure {
project.ext.configureCommonTestSettings(it)
}
}
}
}
// Enforce checkstyle rules.
if (checkstyleEnabled) {
apply plugin: 'checkstyle'
checkstyle {
configDirectory = new File("${checkstyleConfigDir}")
configFile = new File("${checkstyleConfigDir}/checkstyle.xml")
if (managedVersions.containsKey('com.puppycrawl.tools:checkstyle')) {
toolVersion = managedVersions['com.puppycrawl.tools:checkstyle']
}
}
task checkstyle(group: 'Verification', description: 'Runs the checkstyle rules.') {}
project.sourceSets.all { SourceSet sourceSet ->
def dependencyTask = project.tasks.findByName("checkstyle${sourceSet.name.capitalize()}")
if (dependencyTask instanceof Checkstyle) {
tasks.checkstyle.dependsOn dependencyTask
}
}
def lintTask = project.ext.getLintTask()
lintTask.dependsOn tasks.checkstyle
tasks.check.dependsOn lintTask
}
}