mirror of
https://github.com/apache/thrift.git
synced 2026-10-29 08:55:16 +08:00
Client: java, tutorial Migrated tutorial/java from Apache Ant (build.xml, build.properties) to Gradle: - Added build.gradle with Java and Application plugins. - Configured Gradle composite build (includeBuild) to build and consume libthrift directly. - Added generateThrift task to invoke thrift compiler on tutorial.thrift and shared.thrift. - Added tutorial, tutorialServer, and tutorialClient tasks for running the tutorial. - Updated JavaServer and JavaClient SSL keystore/truststore paths to valid crossTest resource locations. - Updated Makefile.am to invoke Gradle instead of Ant and updated EXTRA_DIST. - Updated README.md with Gradle execution instructions. - Added /tutorial/java/.gradle/ to .gitignore. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
180 lines
5.2 KiB
Groovy
180 lines
5.2 KiB
Groovy
/*
|
|
* Licensed to the Apache Software Foundation (ASF) under one
|
|
* or more contributor license agreements. See the NOTICE file
|
|
* distributed with this work for additional information
|
|
* regarding copyright ownership. The ASF licenses this file
|
|
* to you under the Apache License, Version 2.0 (the
|
|
* "License"); you may not use this file except in compliance
|
|
* with the License. You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing,
|
|
* software distributed under the License is distributed on an
|
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
* KIND, either express or implied. See the License for the
|
|
* specific language governing permissions and limitations
|
|
* under the License.
|
|
*/
|
|
|
|
plugins {
|
|
id 'java'
|
|
id 'application'
|
|
}
|
|
|
|
description = 'Apache Thrift Java Tutorial'
|
|
|
|
repositories {
|
|
mavenCentral()
|
|
}
|
|
|
|
dependencies {
|
|
implementation 'org.apache.thrift:libthrift:INCLUDED'
|
|
implementation 'javax.annotation:javax.annotation-api:1.3.2'
|
|
implementation 'org.slf4j:slf4j-api:1.7.36'
|
|
runtimeOnly 'org.slf4j:slf4j-simple:1.7.36'
|
|
}
|
|
|
|
java {
|
|
sourceCompatibility = JavaVersion.VERSION_1_8
|
|
targetCompatibility = JavaVersion.VERSION_1_8
|
|
}
|
|
|
|
tasks.withType(JavaCompile).configureEach {
|
|
options.encoding = 'UTF-8'
|
|
}
|
|
|
|
def thriftBin = findProperty('thrift.compiler') ?: "$rootDir/../../compiler/cpp/thrift"
|
|
def genSrc = layout.buildDirectory.dir('generated-sources').get().asFile
|
|
|
|
task generateThrift(type: Exec) {
|
|
group = 'Build'
|
|
description = 'Generate Java sources from Thrift IDL files'
|
|
|
|
def tutorialThrift = file("$rootDir/../tutorial.thrift")
|
|
def sharedThrift = file("$rootDir/../shared.thrift")
|
|
|
|
inputs.file tutorialThrift
|
|
inputs.file sharedThrift
|
|
outputs.dir genSrc
|
|
|
|
doFirst {
|
|
genSrc.mkdirs()
|
|
}
|
|
|
|
commandLine file(thriftBin).absolutePath, '-r', '--gen', 'java', '-out', genSrc.absolutePath, tutorialThrift.absolutePath
|
|
}
|
|
|
|
compileJava.dependsOn generateThrift
|
|
|
|
sourceSets {
|
|
main {
|
|
java {
|
|
srcDirs = ['src', genSrc]
|
|
}
|
|
}
|
|
}
|
|
|
|
clean {
|
|
delete 'gen-java'
|
|
delete 'tutorial.jar'
|
|
}
|
|
|
|
application {
|
|
mainClass = 'JavaClient'
|
|
}
|
|
|
|
task tutorialServer(type: JavaExec) {
|
|
group = 'Execution'
|
|
description = 'Run the tutorial server'
|
|
classpath = sourceSets.main.runtimeClasspath
|
|
mainClass = 'JavaServer'
|
|
}
|
|
|
|
task tutorialClient(type: JavaExec) {
|
|
group = 'Execution'
|
|
description = 'Run the tutorial client (simple)'
|
|
classpath = sourceSets.main.runtimeClasspath
|
|
mainClass = 'JavaClient'
|
|
args = ['simple']
|
|
}
|
|
|
|
task tutorialSecureClient(type: JavaExec) {
|
|
group = 'Execution'
|
|
description = 'Run the tutorial client (secure)'
|
|
classpath = sourceSets.main.runtimeClasspath
|
|
mainClass = 'JavaClient'
|
|
args = ['secure']
|
|
}
|
|
|
|
task tutorialserver(dependsOn: tutorialServer) {
|
|
group = 'Execution'
|
|
description = 'Alias for tutorialServer'
|
|
}
|
|
|
|
task tutorialclient(dependsOn: tutorialClient) {
|
|
group = 'Execution'
|
|
description = 'Alias for tutorialClient'
|
|
}
|
|
|
|
task tutorial {
|
|
group = 'Execution'
|
|
description = 'Run tutorial server and client together'
|
|
dependsOn assemble
|
|
|
|
doLast {
|
|
def javaBin = "${System.getProperty('java.home')}/bin/java"
|
|
def cp = sourceSets.main.runtimeClasspath.asPath
|
|
|
|
def serverProcess = new ProcessBuilder(javaBin, '-cp', cp, 'JavaServer')
|
|
.directory(projectDir)
|
|
.redirectErrorStream(true)
|
|
.start()
|
|
|
|
try {
|
|
def connected = false
|
|
for (int i = 0; i < 50; i++) {
|
|
try {
|
|
new java.net.Socket('localhost', 9090).close()
|
|
connected = true
|
|
break
|
|
} catch (Exception ignored) {
|
|
Thread.sleep(100)
|
|
}
|
|
}
|
|
if (!connected) {
|
|
throw new GradleException('Timed out waiting for tutorial server to start')
|
|
}
|
|
|
|
println 'tutorial client simple:'
|
|
def clientSimple = new ProcessBuilder(javaBin, '-cp', cp, 'JavaClient', 'simple')
|
|
.directory(projectDir)
|
|
.redirectErrorStream(true)
|
|
.start()
|
|
clientSimple.inputStream.eachLine { println it }
|
|
def clientSimpleExit = clientSimple.waitFor()
|
|
if (clientSimpleExit != 0) {
|
|
throw new GradleException("tutorial client simple failed with exit code $clientSimpleExit")
|
|
}
|
|
|
|
println 'tutorial client secure:'
|
|
def clientSecure = new ProcessBuilder(javaBin, '-cp', cp, 'JavaClient', 'secure')
|
|
.directory(projectDir)
|
|
.redirectErrorStream(true)
|
|
.start()
|
|
clientSecure.inputStream.eachLine { println it }
|
|
def clientSecureExit = clientSecure.waitFor()
|
|
if (clientSecureExit != 0) {
|
|
throw new GradleException("tutorial client secure failed with exit code $clientSecureExit")
|
|
}
|
|
} finally {
|
|
serverProcess.destroyForcibly()
|
|
serverProcess.waitFor()
|
|
}
|
|
}
|
|
}
|
|
|
|
test {
|
|
dependsOn tutorial
|
|
}
|