1*e07d83d3SAndroid Build Coastguard Workerplugins { 2*e07d83d3SAndroid Build Coastguard Worker id "java-library" 3*e07d83d3SAndroid Build Coastguard Worker id "maven-publish" 4*e07d83d3SAndroid Build Coastguard Worker} 5*e07d83d3SAndroid Build Coastguard Worker 6*e07d83d3SAndroid Build Coastguard Workerdescription = "gRPC: Jakarta Servlet" 7*e07d83d3SAndroid Build Coastguard WorkersourceCompatibility = 1.8 8*e07d83d3SAndroid Build Coastguard WorkertargetCompatibility = 1.8 9*e07d83d3SAndroid Build Coastguard Worker 10*e07d83d3SAndroid Build Coastguard Worker// Set up classpaths and source directories for different servlet tests 11*e07d83d3SAndroid Build Coastguard Workerconfigurations { 12*e07d83d3SAndroid Build Coastguard Worker itImplementation.extendsFrom(implementation) 13*e07d83d3SAndroid Build Coastguard Worker jettyTestImplementation.extendsFrom(itImplementation) 14*e07d83d3SAndroid Build Coastguard Worker tomcatTestImplementation.extendsFrom(itImplementation) 15*e07d83d3SAndroid Build Coastguard Worker undertowTestImplementation.extendsFrom(itImplementation) 16*e07d83d3SAndroid Build Coastguard Worker} 17*e07d83d3SAndroid Build Coastguard Worker 18*e07d83d3SAndroid Build Coastguard WorkersourceSets { 19*e07d83d3SAndroid Build Coastguard Worker undertowTest { 20*e07d83d3SAndroid Build Coastguard Worker java { 21*e07d83d3SAndroid Build Coastguard Worker include '**/Undertow*.java' 22*e07d83d3SAndroid Build Coastguard Worker } 23*e07d83d3SAndroid Build Coastguard Worker } 24*e07d83d3SAndroid Build Coastguard Worker tomcatTest { 25*e07d83d3SAndroid Build Coastguard Worker java { 26*e07d83d3SAndroid Build Coastguard Worker include '**/Tomcat*.java' 27*e07d83d3SAndroid Build Coastguard Worker } 28*e07d83d3SAndroid Build Coastguard Worker } 29*e07d83d3SAndroid Build Coastguard Worker // Only run these tests if java 11+ is being used 30*e07d83d3SAndroid Build Coastguard Worker if (JavaVersion.current().isJava11Compatible()) { 31*e07d83d3SAndroid Build Coastguard Worker jettyTest { 32*e07d83d3SAndroid Build Coastguard Worker java { 33*e07d83d3SAndroid Build Coastguard Worker include '**/Jetty*.java' 34*e07d83d3SAndroid Build Coastguard Worker } 35*e07d83d3SAndroid Build Coastguard Worker } 36*e07d83d3SAndroid Build Coastguard Worker } 37*e07d83d3SAndroid Build Coastguard Worker} 38*e07d83d3SAndroid Build Coastguard Worker 39*e07d83d3SAndroid Build Coastguard Worker// Mechanically transform sources from grpc-servlet to use the corrected packages 40*e07d83d3SAndroid Build Coastguard Workerdef migrate(String name, String inputDir, SourceSet sourceSet) { 41*e07d83d3SAndroid Build Coastguard Worker def outputDir = layout.buildDirectory.dir('generated/sources/jakarta-' + name) 42*e07d83d3SAndroid Build Coastguard Worker sourceSet.java.srcDir outputDir 43*e07d83d3SAndroid Build Coastguard Worker return tasks.register('migrateSources' + name.capitalize(), Sync) { task -> 44*e07d83d3SAndroid Build Coastguard Worker into(outputDir) 45*e07d83d3SAndroid Build Coastguard Worker from("$inputDir/io/grpc/servlet") { 46*e07d83d3SAndroid Build Coastguard Worker into('io/grpc/servlet/jakarta') 47*e07d83d3SAndroid Build Coastguard Worker filter { String line -> 48*e07d83d3SAndroid Build Coastguard Worker line.replaceAll('javax\\.servlet', 'jakarta.servlet') 49*e07d83d3SAndroid Build Coastguard Worker .replaceAll('io\\.grpc\\.servlet', 'io.grpc.servlet.jakarta') 50*e07d83d3SAndroid Build Coastguard Worker } 51*e07d83d3SAndroid Build Coastguard Worker } 52*e07d83d3SAndroid Build Coastguard Worker } 53*e07d83d3SAndroid Build Coastguard Worker} 54*e07d83d3SAndroid Build Coastguard Worker 55*e07d83d3SAndroid Build Coastguard WorkercompileJava.dependsOn migrate('main', '../src/main/java', sourceSets.main) 56*e07d83d3SAndroid Build Coastguard Worker 57*e07d83d3SAndroid Build Coastguard WorkersourcesJar.dependsOn migrateSourcesMain 58*e07d83d3SAndroid Build Coastguard Worker 59*e07d83d3SAndroid Build Coastguard Worker// Build the set of sourceSets and classpaths to modify, since Jetty 11 requires Java 11 60*e07d83d3SAndroid Build Coastguard Worker// and must be skipped 61*e07d83d3SAndroid Build Coastguard WorkercompileUndertowTestJava.dependsOn(migrate('undertowTest', '../src/undertowTest/java', sourceSets.undertowTest)) 62*e07d83d3SAndroid Build Coastguard WorkercompileTomcatTestJava.dependsOn(migrate('tomcatTest', '../src/tomcatTest/java', sourceSets.tomcatTest)) 63*e07d83d3SAndroid Build Coastguard Workerif (JavaVersion.current().isJava11Compatible()) { 64*e07d83d3SAndroid Build Coastguard Worker compileJettyTestJava.dependsOn(migrate('jettyTest', '../src/jettyTest/java', sourceSets.jettyTest)) 65*e07d83d3SAndroid Build Coastguard Worker} 66*e07d83d3SAndroid Build Coastguard Worker 67*e07d83d3SAndroid Build Coastguard Worker// Disable checkstyle for this project, since it consists only of generated code 68*e07d83d3SAndroid Build Coastguard Workertasks.withType(Checkstyle) { 69*e07d83d3SAndroid Build Coastguard Worker enabled = false 70*e07d83d3SAndroid Build Coastguard Worker} 71*e07d83d3SAndroid Build Coastguard Worker 72*e07d83d3SAndroid Build Coastguard Workerdependencies { 73*e07d83d3SAndroid Build Coastguard Worker api project(':grpc-api') 74*e07d83d3SAndroid Build Coastguard Worker compileOnly 'jakarta.servlet:jakarta.servlet-api:5.0.0', 75*e07d83d3SAndroid Build Coastguard Worker libraries.javax.annotation 76*e07d83d3SAndroid Build Coastguard Worker 77*e07d83d3SAndroid Build Coastguard Worker implementation project(':grpc-core'), 78*e07d83d3SAndroid Build Coastguard Worker libraries.guava 79*e07d83d3SAndroid Build Coastguard Worker 80*e07d83d3SAndroid Build Coastguard Worker itImplementation project(':grpc-servlet-jakarta'), 81*e07d83d3SAndroid Build Coastguard Worker project(':grpc-netty'), 82*e07d83d3SAndroid Build Coastguard Worker project(':grpc-core').sourceSets.test.runtimeClasspath, 83*e07d83d3SAndroid Build Coastguard Worker libraries.junit 84*e07d83d3SAndroid Build Coastguard Worker itImplementation(project(':grpc-interop-testing')) { 85*e07d83d3SAndroid Build Coastguard Worker // Avoid grpc-netty-shaded dependency 86*e07d83d3SAndroid Build Coastguard Worker exclude group: 'io.grpc', module: 'grpc-alts' 87*e07d83d3SAndroid Build Coastguard Worker exclude group: 'io.grpc', module: 'grpc-xds' 88*e07d83d3SAndroid Build Coastguard Worker } 89*e07d83d3SAndroid Build Coastguard Worker 90*e07d83d3SAndroid Build Coastguard Worker tomcatTestImplementation 'org.apache.tomcat.embed:tomcat-embed-core:10.0.14' 91*e07d83d3SAndroid Build Coastguard Worker 92*e07d83d3SAndroid Build Coastguard Worker jettyTestImplementation "org.eclipse.jetty:jetty-servlet:11.0.7", 93*e07d83d3SAndroid Build Coastguard Worker "org.eclipse.jetty.http2:http2-server:11.0.7" 94*e07d83d3SAndroid Build Coastguard Worker 95*e07d83d3SAndroid Build Coastguard Worker undertowTestImplementation 'io.undertow:undertow-servlet-jakartaee9:2.2.13.Final' 96*e07d83d3SAndroid Build Coastguard Worker} 97*e07d83d3SAndroid Build Coastguard Worker 98*e07d83d3SAndroid Build Coastguard Worker// Set up individual classpaths for each test, to avoid any mismatch, 99*e07d83d3SAndroid Build Coastguard Worker// and ensure they are only used when supported by the current jvm 100*e07d83d3SAndroid Build Coastguard Workercheck.dependsOn(tasks.register('undertowTest', Test) { 101*e07d83d3SAndroid Build Coastguard Worker classpath = sourceSets.undertowTest.runtimeClasspath 102*e07d83d3SAndroid Build Coastguard Worker testClassesDirs = sourceSets.undertowTest.output.classesDirs 103*e07d83d3SAndroid Build Coastguard Worker}) 104*e07d83d3SAndroid Build Coastguard Workercheck.dependsOn(tasks.register('tomcat10Test', Test) { 105*e07d83d3SAndroid Build Coastguard Worker classpath = sourceSets.tomcatTest.runtimeClasspath 106*e07d83d3SAndroid Build Coastguard Worker testClassesDirs = sourceSets.tomcatTest.output.classesDirs 107*e07d83d3SAndroid Build Coastguard Worker 108*e07d83d3SAndroid Build Coastguard Worker // Provide a temporary directory for tomcat to be deleted after test finishes 109*e07d83d3SAndroid Build Coastguard Worker def tomcatTempDir = "$buildDir/tomcat_catalina_base" 110*e07d83d3SAndroid Build Coastguard Worker systemProperty 'catalina.base', tomcatTempDir 111*e07d83d3SAndroid Build Coastguard Worker doLast { 112*e07d83d3SAndroid Build Coastguard Worker file(tomcatTempDir).deleteDir() 113*e07d83d3SAndroid Build Coastguard Worker } 114*e07d83d3SAndroid Build Coastguard Worker 115*e07d83d3SAndroid Build Coastguard Worker // tomcat-embed-core 10 presently performs illegal reflective access on 116*e07d83d3SAndroid Build Coastguard Worker // java.io.ObjectStreamClass$Caches.localDescs and sun.rmi.transport.Target.ccl, 117*e07d83d3SAndroid Build Coastguard Worker // see https://lists.apache.org/thread/s0xr7tk2kfkkxfjps9n7dhh4cypfdhyy 118*e07d83d3SAndroid Build Coastguard Worker if (JavaVersion.current().isJava9Compatible()) { 119*e07d83d3SAndroid Build Coastguard Worker jvmArgs += ['--add-opens=java.base/java.io=ALL-UNNAMED', '--add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED'] 120*e07d83d3SAndroid Build Coastguard Worker } 121*e07d83d3SAndroid Build Coastguard Worker}) 122*e07d83d3SAndroid Build Coastguard Worker// Only run these tests if java 11+ is being used 123*e07d83d3SAndroid Build Coastguard Workerif (JavaVersion.current().isJava11Compatible()) { 124*e07d83d3SAndroid Build Coastguard Worker check.dependsOn(tasks.register('jetty11Test', Test) { 125*e07d83d3SAndroid Build Coastguard Worker classpath = sourceSets.jettyTest.runtimeClasspath 126*e07d83d3SAndroid Build Coastguard Worker testClassesDirs = sourceSets.jettyTest.output.classesDirs 127*e07d83d3SAndroid Build Coastguard Worker }) 128*e07d83d3SAndroid Build Coastguard Worker} 129