Skip to content

Commit ed533eb

Browse files
ejona86larry-safran
authored andcommitted
servlet: Support configuration avoidance in build
Instead of adding explicit dependsOn to compileJava and sourcesJar, adding the sync task to the sourceSet automatically propagates the dependency to all consumers of the code.
1 parent b91710c commit ed533eb

File tree

2 files changed

+47
-30
lines changed

2 files changed

+47
-30
lines changed

servlet/build.gradle

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ dependencies {
6464
libraries.protobuf.java
6565
}
6666

67-
test {
67+
tasks.named("test").configure {
6868
if (JavaVersion.current().isJava9Compatible()) {
6969
jvmArgs += [
7070
// required for Lincheck
@@ -76,11 +76,11 @@ test {
7676

7777
// Set up individual classpaths for each test, to avoid any mismatch,
7878
// and ensure they are only used when supported by the current jvm
79-
check.dependsOn(tasks.register('undertowTest', Test) {
79+
def undertowTest = tasks.register('undertowTest', Test) {
8080
classpath = sourceSets.undertowTest.runtimeClasspath
8181
testClassesDirs = sourceSets.undertowTest.output.classesDirs
82-
})
83-
check.dependsOn(tasks.register('tomcat9Test', Test) {
82+
}
83+
def tomcat9Test = tasks.register('tomcat9Test', Test) {
8484
classpath = sourceSets.tomcatTest.runtimeClasspath
8585
testClassesDirs = sourceSets.tomcatTest.output.classesDirs
8686

@@ -97,19 +97,30 @@ check.dependsOn(tasks.register('tomcat9Test', Test) {
9797
if (JavaVersion.current().isJava9Compatible()) {
9898
jvmArgs += ['--add-opens=java.base/java.io=ALL-UNNAMED', '--add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED']
9999
}
100-
})
100+
}
101+
102+
tasks.named("check").configure {
103+
dependsOn undertowTest, tomcat9Test
104+
}
105+
106+
tasks.named("jacocoTestReport").configure {
107+
// Must use executionData(Task...) override. The executionData(Object...) override doesn't find
108+
// execution data correctly for tasks.
109+
executionData undertowTest.get(), tomcat9Test.get()
110+
}
101111

102112
// Only run these tests if java 11+ is being used
103113
if (JavaVersion.current().isJava11Compatible()) {
104-
check.dependsOn(tasks.register('jettyTest', Test) {
114+
def jettyTest = tasks.register('jettyTest', Test) {
105115
classpath = sourceSets.jettyTest.runtimeClasspath
106116
testClassesDirs = sourceSets.jettyTest.output.classesDirs
107-
})
108-
}
109-
110-
jacocoTestReport {
111-
executionData undertowTest, tomcat9Test
112-
if (JavaVersion.current().isJava11Compatible()) {
113-
executionData jettyTest
117+
}
118+
tasks.named("check").configure {
119+
dependsOn jettyTest
120+
}
121+
tasks.named("jacocoTestReport").configure {
122+
// Must use executionData(Task...) override. The executionData(Object...) override doesn't
123+
// find execution data correctly for tasks.
124+
executionData jettyTest.get()
114125
}
115126
}

servlet/jakarta/build.gradle

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,33 +42,30 @@ java {
4242
// Mechanically transform sources from grpc-servlet to use the corrected packages
4343
def migrate(String name, String inputDir, SourceSet sourceSet) {
4444
def outputDir = layout.buildDirectory.dir('generated/sources/jakarta-' + name)
45-
sourceSet.java.srcDir outputDir
46-
return tasks.register('migrateSources' + name.capitalize(), Sync) { task ->
45+
sourceSet.java.srcDir tasks.register('migrateSources' + name.capitalize(), Sync) { task ->
4746
into(outputDir)
4847
from("$inputDir/io/grpc/servlet") {
4948
into('io/grpc/servlet/jakarta')
5049
filter { String line ->
51-
line.replaceAll('javax\\.servlet', 'jakarta.servlet')
52-
.replaceAll('io\\.grpc\\.servlet', 'io.grpc.servlet.jakarta')
50+
line.replace('javax.servlet', 'jakarta.servlet')
51+
.replace('io.grpc.servlet', 'io.grpc.servlet.jakarta')
5352
}
5453
}
5554
}
5655
}
5756

58-
compileJava.dependsOn migrate('main', '../src/main/java', sourceSets.main)
59-
60-
sourcesJar.dependsOn migrateSourcesMain
57+
migrate('main', '../src/main/java', sourceSets.main)
6158

6259
// Build the set of sourceSets and classpaths to modify, since Jetty 11 requires Java 11
6360
// and must be skipped
64-
compileUndertowTestJava.dependsOn(migrate('undertowTest', '../src/undertowTest/java', sourceSets.undertowTest))
65-
compileTomcatTestJava.dependsOn(migrate('tomcatTest', '../src/tomcatTest/java', sourceSets.tomcatTest))
61+
migrate('undertowTest', '../src/undertowTest/java', sourceSets.undertowTest)
62+
migrate('tomcatTest', '../src/tomcatTest/java', sourceSets.tomcatTest)
6663
if (JavaVersion.current().isJava11Compatible()) {
67-
compileJettyTestJava.dependsOn(migrate('jettyTest', '../src/jettyTest/java', sourceSets.jettyTest))
64+
migrate('jettyTest', '../src/jettyTest/java', sourceSets.jettyTest)
6865
}
6966

7067
// Disable checkstyle for this project, since it consists only of generated code
71-
tasks.withType(Checkstyle) {
68+
tasks.withType(Checkstyle).configureEach {
7269
enabled = false
7370
}
7471

@@ -106,11 +103,11 @@ dependencies {
106103

107104
// Set up individual classpaths for each test, to avoid any mismatch,
108105
// and ensure they are only used when supported by the current jvm
109-
check.dependsOn(tasks.register('undertowTest', Test) {
106+
def undertowTest = tasks.register('undertowTest', Test) {
110107
classpath = sourceSets.undertowTest.runtimeClasspath
111108
testClassesDirs = sourceSets.undertowTest.output.classesDirs
112-
})
113-
check.dependsOn(tasks.register('tomcat10Test', Test) {
109+
}
110+
def tomcat10Test = tasks.register('tomcat10Test', Test) {
114111
classpath = sourceSets.tomcatTest.runtimeClasspath
115112
testClassesDirs = sourceSets.tomcatTest.output.classesDirs
116113

@@ -127,11 +124,20 @@ check.dependsOn(tasks.register('tomcat10Test', Test) {
127124
if (JavaVersion.current().isJava9Compatible()) {
128125
jvmArgs += ['--add-opens=java.base/java.io=ALL-UNNAMED', '--add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED']
129126
}
130-
})
127+
}
128+
129+
tasks.named("check").configure {
130+
dependsOn undertowTest, tomcat10Test
131+
}
132+
131133
// Only run these tests if java 11+ is being used
132134
if (JavaVersion.current().isJava11Compatible()) {
133-
check.dependsOn(tasks.register('jetty11Test', Test) {
135+
def jetty11Test = tasks.register('jetty11Test', Test) {
134136
classpath = sourceSets.jettyTest.runtimeClasspath
135137
testClassesDirs = sourceSets.jettyTest.output.classesDirs
136-
})
138+
}
139+
140+
tasks.named("check").configure {
141+
dependsOn jetty11Test
142+
}
137143
}

0 commit comments

Comments
 (0)