Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import path in Java, Maven

Tags:

java

maven

Following the tutorial about Kafka Streams located at: https://github.com/confluentinc/kafka-streams-examples/blob/4.0.0-post/src/main/java/io/confluent/examples/streams/WikipediaFeedAvroExample.java

There is a line:

import io.confluent.examples.streams.avro.WikiFeed

As I suppose it relates to this file: https://github.com/confluentinc/kafka-streams-examples/blob/4.0.0-post/src/main/resources/avro/io/confluent/examples/streams/wikifeed.avsc

  • How does Maven knows it is in resource not java folder?
  • Why io/confluent/examples/streams/avro/wikifeed.avsc instead of avro/io/confluent/examples/streams/wikifeed.avsc?

The other import is even more fantastic:

import io.confluent.kafka.serializers.AbstractKafkaAvroSerDeConfig;
  • There is no kafka folder in the java/io/confluent folder.

https://github.com/confluentinc/kafka-streams-examples/tree/4.0.0-post/src/main/resources/avro/io/confluent.

How does all this magic suppose to work?

like image 346
0leg Avatar asked Apr 30 '26 22:04

0leg


2 Answers

The magic is made by avro-maven-plugin which you can find in the pom.xml:

<plugin>
    <groupId>org.apache.avro</groupId>
    <artifactId>avro-maven-plugin</artifactId>
    <version>${avro.version}</version>
    <executions>
        <execution>
            <phase>generate-sources</phase>
            <goals>
                <goal>schema</goal>
            </goals>
            <configuration>
                <sourceDirectory>src/main/resources/avro/io/confluent/examples/streams</sourceDirectory>
                <outputDirectory>${project.build.directory}/generated-sources</outputDirectory>
                <stringType>String</stringType>
            </configuration>
        </execution>
    </executions>
</plugin>

Quoting from the documentation of the plugin:

Simple integration with dynamic languages. Code generation is not required to read or write data files nor to use or implement RPC protocols. Code generation as an optional optimization, only worth implementing for statically typed languages.

This is, at pre compile time, the plugin reads the content of avsc files and generate binary sources (for this case, Java classes) that then can be used in the code.

You can see the code generated by the plugin in target/generated-sources. There will be a folder structure and proper java (not class) files there.

like image 81
Luiggi Mendoza Avatar answered May 03 '26 11:05

Luiggi Mendoza


The WikiFeed class is created dynamically at build time using the avro-maven-plugin from the .avsc file you linked to. You can check how it's configured in the <plugins> section of pom.xml.

The AbstractKafkaAvroSerDeConfig class comes from the kafka-avro-serializer dependency. Eclipse has a nice way of navigating from the individual class in the Editor view back to the Package Explorer which includes the Maven dependencies, like this:

package explorer: maven dependencies

like image 45
kryger Avatar answered May 03 '26 13:05

kryger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!