BAEL-7445, add custom task

This commit is contained in:
kateryna.hrytsaenko 2024-03-24 22:51:46 +02:00
parent 07dc4d1272
commit 8753641fee
5 changed files with 93 additions and 11 deletions

View File

@ -1,3 +1,9 @@
buildscript {
dependencies {
classpath libs.avro.tools
}
}
plugins {
id 'java'
alias libs.plugins.avro
@ -16,9 +22,41 @@ dependencies {
}
import org.apache.avro.tool.SpecificCompilerTool
sourceCompatibility = 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') {
// Use JUnit Platform for unit tests.
useJUnitPlatform()

View File

@ -3,10 +3,12 @@
[versions]
junit-jupiter = "5.10.0"
avro = "1.11.0"
[libraries]
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]
avro = { id = "com.github.davidmc24.gradle.plugin.avro", version = "1.9.1" }

View File

@ -1,10 +1,19 @@
{
"type": "record",
"name": "User",
"namespace": "com.example",
"fields": [
{ "name": "firstName", "type": "string" },
{ "name": "lastName", "type": "string" },
{ "name": "phoneNumber", "type": "string" }
]
"type": "record",
"name": "User",
"namespace": "avro",
"fields": [
{
"name": "firstName",
"type": "string"
},
{
"name": "lastName",
"type": "string"
},
{
"name": "phoneNumber",
"type": "string"
}
]
}

View File

@ -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" }
]
}

View File

@ -1,6 +1,7 @@
package com.baeldung.avro;
import com.example.User;
import avro.User;
import custom.avro.Pet;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
@ -8,7 +9,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
public class AvroCodeGenerationUnitTest {
@Test
void givenUserData_whenObjectCreated_thenDataShouldMatch() {
void givenUserData_whenJavaClassGeneratedWithPlugin_thenDataShouldMatch() {
final String firstName = "John";
final String lastName = "Doe";
final String phoneNumber = "+380659443235";
@ -23,4 +24,25 @@ public class AvroCodeGenerationUnitTest {
assertEquals(lastName, user.getLastName());
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());
}
}