【问题标题】:Import path in Java, MavenJava,Maven中的导入路径
【发布时间】:2018-03-17 03:34:04
【问题描述】:

按照位于 https://github.com/confluentinc/kafka-streams-examples/blob/4.0.0-post/src/main/java/io/confluent/examples/streams/WikipediaFeedAvroExample.java 的有关 Kafka Streams 的教程进行操作

有一行:

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

我想它与这个文件有关:https://github.com/confluentinc/kafka-streams-examples/blob/4.0.0-post/src/main/resources/avro/io/confluent/examples/streams/wikifeed.avsc

  • Maven 怎么知道它在 resource 而不是 java 文件夹中?
  • 为什么 io/confluent/examples/streams/avro/wikifeed.avsc 而不是 avro/io /confluent/examples/streams/wikifeed.avsc?

另一个导入更精彩:

import io.confluent.kafka.serializers.AbstractKafkaAvroSerDeConfig;
  • java/io/confluent 文件夹中没有kafka 文件夹。

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

所有这些魔法是如何发挥作用的?

【问题讨论】:

  • @IanMcLaird 在这种情况下,maven 没有下载任何东西......

标签: java maven


【解决方案1】:

魔法avro-maven-plugin制作,您可以在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>

引自插件的文档:

与动态语言的简单集成。代码生成不需要读取或写入数据文件,也不需要使用或实现 RPC 协议。代码生成作为一种可选的优化,只值得为静态类型语言实现。

这是,在预编译时,插件读取 avsc 文件的内容并生成二进制源(在本例中为 Java 类),然后可以在代码中使用。

你可以在target/generated-sources看到插件生成的代码。那里会有一个文件夹结构和适当的 java(不是类)文件。

【讨论】:

    【解决方案2】:

    WikiFeed 类是在构建时使用您链接到的 .avsc 文件中的 avro-maven-plugin 动态创建的。你可以查看它是如何配置的in the &lt;plugins&gt; section of pom.xml

    AbstractKafkaAvroSerDeConfig 类来自 kafka-avro-serializer 依赖项。 Eclipse 有一种很好的方法可以从编辑器视图中的单个类导航回包含 Maven 依赖项的包资源管理器,如下所示:

    【讨论】:

      猜你喜欢
      • 2015-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-13
      • 2011-03-18
      • 1970-01-01
      • 2018-07-09
      相关资源
      最近更新 更多