BAEL-7445, add custom task
This commit is contained in:
parent
07dc4d1272
commit
8753641fee
|
@ -1,3 +1,9 @@
|
||||||
|
buildscript {
|
||||||
|
dependencies {
|
||||||
|
classpath libs.avro.tools
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
plugins {
|
plugins {
|
||||||
id 'java'
|
id 'java'
|
||||||
alias libs.plugins.avro
|
alias libs.plugins.avro
|
||||||
|
@ -16,9 +22,41 @@ dependencies {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
import org.apache.avro.tool.SpecificCompilerTool
|
||||||
|
|
||||||
sourceCompatibility = JavaVersion.VERSION_21
|
sourceCompatibility = JavaVersion.VERSION_21
|
||||||
targetCompatibility = JavaVersion.VERSION_21
|
targetCompatibility = JavaVersion.VERSION_21
|
||||||
|
|
||||||
|
def avroSchemasDir = "src/main/custom"
|
||||||
|
def avroCodeGenerationDir = "build/generated-main-avro-custom-java"
|
||||||
|
|
||||||
|
// Add the generated Avro Java code to the Gradle source files.
|
||||||
|
sourceSets.main.java.srcDirs += [avroCodeGenerationDir]
|
||||||
|
|
||||||
|
tasks.register('customAvroCodeGeneration') {
|
||||||
|
// Define the task inputs and outputs for the Gradle up-to-date checks.
|
||||||
|
inputs.dir(avroSchemasDir)
|
||||||
|
outputs.dir(avroCodeGenerationDir)
|
||||||
|
// The Avro code generation logs to the standard streams. Redirect the standard streams to the Gradle log.
|
||||||
|
logging.captureStandardOutput(LogLevel.INFO);
|
||||||
|
logging.captureStandardError(LogLevel.ERROR)
|
||||||
|
doLast {
|
||||||
|
// Run the Avro code generation.
|
||||||
|
new SpecificCompilerTool().run(System.in, System.out, System.err, List.of(
|
||||||
|
"-encoding", "UTF-8",
|
||||||
|
"-string",
|
||||||
|
"-fieldVisibility", "private",
|
||||||
|
"-noSetters",
|
||||||
|
"schema", "$projectDir/$avroSchemasDir".toString(), "$projectDir/$avroCodeGenerationDir".toString()
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tasks.withType(JavaCompile).configureEach {
|
||||||
|
// Make Java compilation tasks depend on the Avro code generation task.
|
||||||
|
dependsOn('customAvroCodeGeneration')
|
||||||
|
}
|
||||||
|
|
||||||
tasks.named('test') {
|
tasks.named('test') {
|
||||||
// Use JUnit Platform for unit tests.
|
// Use JUnit Platform for unit tests.
|
||||||
useJUnitPlatform()
|
useJUnitPlatform()
|
||||||
|
|
|
@ -3,10 +3,12 @@
|
||||||
|
|
||||||
[versions]
|
[versions]
|
||||||
junit-jupiter = "5.10.0"
|
junit-jupiter = "5.10.0"
|
||||||
|
avro = "1.11.0"
|
||||||
|
|
||||||
[libraries]
|
[libraries]
|
||||||
junit-jupiter = { module = "org.junit.jupiter:junit-jupiter", version.ref = "junit-jupiter" }
|
junit-jupiter = { module = "org.junit.jupiter:junit-jupiter", version.ref = "junit-jupiter" }
|
||||||
avro = {module = "org.apache.avro:avro", version = "1.11.0"}
|
avro = {module = "org.apache.avro:avro", version.ref = "avro"}
|
||||||
|
avro-tools = {module = "org.apache.avro:avro-tools", version.ref = "avro"}
|
||||||
|
|
||||||
[plugins]
|
[plugins]
|
||||||
avro = { id = "com.github.davidmc24.gradle.plugin.avro", version = "1.9.1" }
|
avro = { id = "com.github.davidmc24.gradle.plugin.avro", version = "1.9.1" }
|
||||||
|
|
|
@ -1,10 +1,19 @@
|
||||||
{
|
{
|
||||||
"type": "record",
|
"type": "record",
|
||||||
"name": "User",
|
"name": "User",
|
||||||
"namespace": "com.example",
|
"namespace": "avro",
|
||||||
"fields": [
|
"fields": [
|
||||||
{ "name": "firstName", "type": "string" },
|
{
|
||||||
{ "name": "lastName", "type": "string" },
|
"name": "firstName",
|
||||||
{ "name": "phoneNumber", "type": "string" }
|
"type": "string"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "lastName",
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "phoneNumber",
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,11 @@
|
||||||
|
{
|
||||||
|
"type": "record",
|
||||||
|
"name": "Pet",
|
||||||
|
"namespace": "custom.avro",
|
||||||
|
"fields": [
|
||||||
|
{ "name": "petId", "type": "string" },
|
||||||
|
{ "name": "name", "type": "string" },
|
||||||
|
{ "name": "species", "type": "string" },
|
||||||
|
{ "name": "age", "type": "int" }
|
||||||
|
]
|
||||||
|
}
|
|
@ -1,6 +1,7 @@
|
||||||
package com.baeldung.avro;
|
package com.baeldung.avro;
|
||||||
|
|
||||||
import com.example.User;
|
import avro.User;
|
||||||
|
import custom.avro.Pet;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
@ -8,7 +9,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
public class AvroCodeGenerationUnitTest {
|
public class AvroCodeGenerationUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void givenUserData_whenObjectCreated_thenDataShouldMatch() {
|
void givenUserData_whenJavaClassGeneratedWithPlugin_thenDataShouldMatch() {
|
||||||
final String firstName = "John";
|
final String firstName = "John";
|
||||||
final String lastName = "Doe";
|
final String lastName = "Doe";
|
||||||
final String phoneNumber = "+380659443235";
|
final String phoneNumber = "+380659443235";
|
||||||
|
@ -23,4 +24,25 @@ public class AvroCodeGenerationUnitTest {
|
||||||
assertEquals(lastName, user.getLastName());
|
assertEquals(lastName, user.getLastName());
|
||||||
assertEquals(phoneNumber, user.getPhoneNumber());
|
assertEquals(phoneNumber, user.getPhoneNumber());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenUserData_whenJavaClassGeneratedWithTask_thenDataShouldMatch() {
|
||||||
|
final String petId = "123";
|
||||||
|
final String name = "Fluffy";
|
||||||
|
final String species = "Cat";
|
||||||
|
final int age = 3;
|
||||||
|
|
||||||
|
Pet pet = Pet.newBuilder()
|
||||||
|
.setPetId(petId)
|
||||||
|
.setName(name)
|
||||||
|
.setSpecies(species)
|
||||||
|
.setAge(age)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
// Assertions to verify the values
|
||||||
|
assertEquals(petId, pet.getPetId());
|
||||||
|
assertEquals(name, pet.getName());
|
||||||
|
assertEquals(species, pet.getSpecies());
|
||||||
|
assertEquals(age, pet.getAge());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue