From 8753641feefd024a5d60cef1d9f3e95eecd694c5 Mon Sep 17 00:00:00 2001 From: "kateryna.hrytsaenko" Date: Sun, 24 Mar 2024 22:51:46 +0200 Subject: [PATCH] BAEL-7445, add custom task --- .../gradle-avro/build.gradle | 38 +++++++++++++++++++ .../gradle-avro/gradle/libs.versions.toml | 4 +- .../gradle-avro/src/main/avro/user.avsc | 25 ++++++++---- .../gradle-avro/src/main/custom/Pet.avsc | 11 ++++++ .../AvroCodeGenerationUnitTest.java | 26 ++++++++++++- 5 files changed, 93 insertions(+), 11 deletions(-) create mode 100644 gradle-modules/gradle-customization/gradle-avro/src/main/custom/Pet.avsc diff --git a/gradle-modules/gradle-customization/gradle-avro/build.gradle b/gradle-modules/gradle-customization/gradle-avro/build.gradle index a81886cc22..7a9565cb0c 100644 --- a/gradle-modules/gradle-customization/gradle-avro/build.gradle +++ b/gradle-modules/gradle-customization/gradle-avro/build.gradle @@ -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() diff --git a/gradle-modules/gradle-customization/gradle-avro/gradle/libs.versions.toml b/gradle-modules/gradle-customization/gradle-avro/gradle/libs.versions.toml index 6c2cee93b8..8a2b7df961 100644 --- a/gradle-modules/gradle-customization/gradle-avro/gradle/libs.versions.toml +++ b/gradle-modules/gradle-customization/gradle-avro/gradle/libs.versions.toml @@ -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" } diff --git a/gradle-modules/gradle-customization/gradle-avro/src/main/avro/user.avsc b/gradle-modules/gradle-customization/gradle-avro/src/main/avro/user.avsc index 0936672532..7f9cde4d9e 100644 --- a/gradle-modules/gradle-customization/gradle-avro/src/main/avro/user.avsc +++ b/gradle-modules/gradle-customization/gradle-avro/src/main/avro/user.avsc @@ -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" + } + ] } diff --git a/gradle-modules/gradle-customization/gradle-avro/src/main/custom/Pet.avsc b/gradle-modules/gradle-customization/gradle-avro/src/main/custom/Pet.avsc new file mode 100644 index 0000000000..e510de2189 --- /dev/null +++ b/gradle-modules/gradle-customization/gradle-avro/src/main/custom/Pet.avsc @@ -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" } + ] +} diff --git a/gradle-modules/gradle-customization/gradle-avro/src/test/java/com.baeldung.avro/AvroCodeGenerationUnitTest.java b/gradle-modules/gradle-customization/gradle-avro/src/test/java/com.baeldung.avro/AvroCodeGenerationUnitTest.java index 57b3b98bb8..4c63836c65 100644 --- a/gradle-modules/gradle-customization/gradle-avro/src/test/java/com.baeldung.avro/AvroCodeGenerationUnitTest.java +++ b/gradle-modules/gradle-customization/gradle-avro/src/test/java/com.baeldung.avro/AvroCodeGenerationUnitTest.java @@ -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()); + } }