在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):davidmc24/gradle-avro-plugin开源软件地址(OpenSource Url):https://github.com/davidmc24/gradle-avro-plugin开源编程语言(OpenSource Language):Java 53.1%开源软件介绍(OpenSource Introduction):OverviewThis is a Gradle plugin to allow easily performing Java code generation for Apache Avro. It supports JSON schema declaration files, JSON protocol declaration files, and Avro IDL files. CompatibilityNOTE: Pre-1.0 versions used a different publishing process/namespace. It is strongly recommended to upgrade to a newer version. Further details can be found in the change log.
UsageAdd the following to your build files. Substitute the desired version based on CHANGES.md.
pluginManagement {
repositories {
gradlePluginPortal()
mavenCentral()
}
}
plugins {
id "com.github.davidmc24.gradle.plugin.avro" version "VERSION"
} Additionally, ensure that you have an implementation dependency on Avro, such as: repositories {
mavenCentral()
}
dependencies {
implementation "org.apache.avro:avro:1.11.0"
} If you now run ConfigurationThere are a number of configuration options supported in the
Additionally, the
createSettersValid values: Set to Example: avro {
createSetters = false
} createOptionalGettersValid values: Set to Example: avro {
createOptionalGetters = false
} gettersReturnOptionalValid values: Set to Example: avro {
gettersReturnOptional = false
} optionalGettersForNullableFieldsOnlyValid values: Set to Example: avro {
gettersReturnOptional = true
optionalGettersForNullableFieldsOnly = true
} fieldVisibilityValid values: any FieldVisibility or equivalent By default, the fields in generated Java files will have private visibility.
Set to Example: avro {
fieldVisibility = "PUBLIC"
} outputCharacterEncodingValid values: any Charset or equivalent Controls the character encoding of generated Java files.
If using the plugin's conventions (i.e., not just the base plugin), the associated Examples: // Option 1: configure compilation task (avro plugin will automatically match)
tasks.withType(JavaCompile).configureEach {
options.encoding = 'UTF-8'
}
// Option 2: just configure avro plugin
avro {
outputCharacterEncoding = "UTF-8"
} stringTypeValid values: any StringType or equivalent By default, the generated Java files will use avro {
stringType = "CharSequence"
} templateDirectoryBy default, files will be generated using Avro's default templates.
If desired, you can override the template set used by either setting this property or the avro {
templateDirectory = "/path/to/velocity/templates"
} enableDecimalLogicalTypeValid values: By default, generated Java files will use Example: avro {
enableDecimalLogicalType = false
} IntelliJ IntegrationThe plugin attempts to make IntelliJ play more smoothly with generated sources when using Gradle-generated project files.
However, there are still some rough edges. It will work best if you first run Alternate UsageIf the defaults used by the plugin don't work for you, you can still use the tasks by themselves.
In this case, use the Here's a short example of what this might look like: import com.github.davidmc24.gradle.plugin.avro.GenerateAvroJavaTask
apply plugin: "java"
apply plugin: "com.github.davidmc24.gradle.plugin.avro-base"
dependencies {
implementation "org.apache.avro:avro:1.11.0"
}
def generateAvro = tasks.register("generateAvro", GenerateAvroJavaTask) {
source("src/avro")
outputDir = file("dest/avro")
}
tasks.named("compileJava").configure {
source(generateAvro)
} File ProcessingWhen using this plugin, it is recommended to define each record/enum/fixed type in its own file rather than using inline type definitions.
This approach allows defining any type of schema structure, and eliminates the potential for conflicting definitions of a type between multiple files.
The plugin will automatically recognize the dependency and compile the files in the correct order.
For example, instead of {
"name": "Cat",
"namespace": "example",
"type": "record",
"fields" : [
{
"name": "breed",
"type": {
"name": "Breed",
"type": "enum",
"symbols" : [
"ABYSSINIAN", "AMERICAN_SHORTHAIR", "BIRMAN", "MAINE_COON", "ORIENTAL", "PERSIAN", "RAGDOLL", "SIAMESE", "SPHYNX"
]
}
}
]
} use {
"name": "Breed",
"namespace": "example",
"type": "enum",
"symbols" : ["ABYSSINIAN", "AMERICAN_SHORTHAIR", "BIRMAN", "MAINE_COON", "ORIENTAL", "PERSIAN", "RAGDOLL", "SIAMESE", "SPHYNX"]
} and {
"name": "Cat",
"namespace": "example",
"type": "record",
"fields" : [
{"name": "breed", "type": "Breed"}
]
} There may be cases where the schema files contain inline type definitions and it is undesirable to modify them. In this case, the plugin will automatically recognize any duplicate type definitions and check if they match. If any conflicts are identified, it will cause a build failure. Kotlin SupportThe Java classes generated from your Avro files should be automatically accessible in the classpath to Kotlin classes in the same sourceset, and transitively to any sourcesets that depend on that sourceset. This is accomplished by this plugin detecting that the Kotlin plugin has been applied, and informing the Kotlin compilation tasks of the presence of the generated sources directories for cross-compilation. This support does not support producing the Avro generated classes as Kotlin classes, as that functionality is not currently provided by the upstream Avro library. Kotlin DSL SupportSpecial notes relevant to using this plugin via the Gradle Kotlin DSL:
Example Kotlin DSL Setup:In plugins {
// Find latest release here: https://github.com/davidmc24/gradle-avro-plugin/releases
id("com.github.davidmc24.gradle.plugin.avro") version "VERSION"
} And then in your pluginManagement {
repositories {
gradlePluginPortal()
mavenCentral()
}
} The syntax for configuring the extension looks like this: avro {
isCreateSetters.set(true)
isCreateOptionalGetters.set(false)
isGettersReturnOptional.set(false)
isOptionalGettersForNullableFieldsOnly(false)
fieldVisibility.set("PUBLIC_DEPRECATED")
outputCharacterEncoding.set("UTF-8")
stringType.set("String")
templateDirectory.set(null as String?)
isEnableDecimalLogicalType.set(true)
} Resolving schema dependenciesIf desired, you can generate JSON schema with dependencies resolved. Example build: import com.github.davidmc24.gradle.plugin.avro.ResolveAvroDependenciesTask
apply plugin: "com.github.davidmc24.gradle.plugin.avro-base"
tasks.register("resolveAvroDependencies", ResolveAvroDependenciesTask) {
source file("src/avro/normalized")
outputDir = file("build/avro/resolved")
} Generating schema files from protocol/IDLIf desired, you can generate JSON schema files.
To do this, apply the plugin (either Example using base plugin with support for both IDL and JSON protocol files in import com.github.davidmc24.gradle.plugin.avro.GenerateAvroProtocolTask
import com.github.davidmc24.gradle.plugin.avro.GenerateAvroSchemaTask
apply plugin: "com.github.davidmc24.gradle.plugin.avro-base"
def generateProtocol = tasks.register("generateProtocol", GenerateAvroProtocolTask) {
source file("src/main/avro")
include("**/*.avdl")
outputDir = file("build/generated-avro-main-avpr")
}
tasks.register("generateSchema", GenerateAvroSchemaTask) {
dependsOn generateProtocol
source file("src/main/avro")
source file("build/generated-avro-main-avpr")
include("**/*.avpr")
outputDir = file("build/generated-main-avro-avsc")
} |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论