【问题标题】:Cannot generate class from avsc file using maven无法使用 maven 从 avsc 文件生成类
【发布时间】:2021-08-26 09:53:08
【问题描述】:

我在从 avsc 文件生成类时遇到问题。在 pom.xml 我添加了适当的依赖项:

<dependencies>
        <dependency>
            <groupId>org.apache.avro</groupId>
            <artifactId>avro</artifactId>
            <version>1.10.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.avro</groupId>
            <artifactId>avro-compiler</artifactId>
            <version>1.10.2</version>
        </dependency>
    </dependencies>

在存储库包中,我创建了一个 avro 文件夹,其中放置了扩展名为 .avsc 的类架构。

{"type":"record",
"name":"Customer",
"namespace":"com.avroGenerator",
"fields":[
 {"name":"name","type":"string"},
 {"name":"age","type":"int"},
 {"name":"isOfAge","type":"boolean"},
 {"name":"currencies","type":
  {"type":"array","items":{
    "type":"record",
    "name":"Currency",
    "namespace":"com.avroGenerator",
    "fields":[
     {"name":"name","type":"string"},
     {"name":"shortcut","type":"string"}]}},
  "default":[]}]}

这是一个客户类,包括货币数组,也从 avsc 生成。

在 target-generated-sources 中调用 Maven-> Package 时,我应该看到我的类,但它没有。有没有人遇到过类似的问题?

【问题讨论】:

    标签: java maven avro avsc


    【解决方案1】:

    您需要添加&lt;plugin&gt; 部分中提到的avro-maven-plugin

    https://avro.apache.org/docs/current/gettingstartedjava.html

    或者,如果您使用的是 Maven,请将以下依赖项添加到 你的 POM:

     <dependency>   
        <groupId>org.apache.avro</groupId>  
        <artifactId>avro</artifactId>   
        <version>1.10.2</version>
    </dependency>
    

    以及 Avro Maven 插件...

    <plugin>
      <groupId>org.apache.avro</groupId>
      <artifactId>avro-maven-plugin</artifactId>
      <version>1.10.2</version>
      <executions>
        <execution>
          <phase>generate-sources</phase>
          <goals>
            <goal>schema</goal>
          </goals>
          <configuration>
            <sourceDirectory>${project.basedir}/src/main/avro/</sourceDirectory>
            <outputDirectory>${project.basedir}/src/main/java/</outputDirectory>
          </configuration>
        </execution>
      </executions>
    </plugin>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <configuration>
        <source>1.8</source>
        <target>1.8</target>
      </configuration>
    </plugin>
    

    如果您只想将架构资源文件转换为类,则不需要 avro-compiler 作为自己代码中的依赖项

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-27
      • 1970-01-01
      • 2016-01-13
      • 1970-01-01
      • 2023-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多