java - 如何在模式生成期间修复“org.apache.velocity.exception.ResourceNotFoundException”
问题描述
我正在尝试将 avro maven 插件集成到我的应用程序中。由于 Avro 的限制,我被迫使用自定义模板。
当我将该插件包含到构建中时,它会失败(在 Windows 上,而不是 unix 上),但有异常:
[ERROR] Failed to execute goal org.apache.avro:avro-maven-plugin:1.9.1:schema (schemas) on project cloud-poc: Execution schemas of goal org.apache.avro:avro-maven-plugin:1.9.1:schema failed: org.apache.velocity
.exception.ResourceNotFoundException: Unable to find resource 'C:\..........\cloud-microservices\cloud-poc/src/main/resources/avro/templates/record.vm'
但是当我cat C:\..........\cloud-microservices\cloud-poc/src/main/resources/avro/templates/record.vm
从 PowerShell 执行时,它会打印文件 - 它可以找到它。
该配置适用于 unix 系统,没有任何问题。这是pom:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.....</groupId>
<artifactId>cloud-poc</artifactId>
<version>0.8.0</version>
<packaging>jar</packaging>
<name>cloud-poc</name>
<description>Proof of concept microservice</description>
<properties>
<kafka.version>2.2.8.RELEASE</kafka.version>
</properties>
<dependencies>
.... avro + kafka
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.avro</groupId>
<artifactId>avro-maven-plugin</artifactId>
<version>1.9.1</version>
<executions>
<execution>
<id>schemas</id>
<phase>generate-sources</phase>
<goals>
<goal>schema</goal>
<goal>protocol</goal>
<goal>idl-protocol</goal>
</goals>
<configuration>
<sourceDirectory>${project.basedir}/src/main/resources/avro</sourceDirectory>
<outputDirectory>${project.basedir}/src/main/java/</outputDirectory>
<fieldVisibility>PRIVATE</fieldVisibility>
<stringType>String</stringType>
<createSetters>false</createSetters>
<templateDirectory>${project.basedir}/src/main/resources/avro/templates/</templateDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
我还尝试使用绝对路径、不同的 Maven 变量和相对路径。我在几个项目上尝试过,但没有运气。
我希望生成这些类而不是。
解决方案
问题的根源在于速度如何使用文件路径查找模板。另一方面,类路径查找与 maven 插件类路径存在问题。目前我可以通过两种方式解决这个问题:使用类路径资源和使用文件资源。
解决方案 1:使用类路径。
由于您已经在资源中拥有模板,因此它将被复制到类路径,一旦复制到目标/类,您就可以引用它。更改 templateDirectory 以引用类路径资源,并将 avro-plugin 执行阶段更改为资源复制后:
<plugin>
<groupId>org.apache.avro</groupId>
<artifactId>avro-maven-plugin</artifactId>
<version>1.9.1</version>
<executions>
<execution>
<id>schemas</id>
<phase>process-resources</phase>
<goals>
<goal>schema</goal>
<goal>protocol</goal>
<goal>idl-protocol</goal>
</goals>
<configuration>
<sourceDirectory>${project.basedir}/src/main/resources/avro</sourceDirectory>
<outputDirectory>${project.basedir}/src/main/java/</outputDirectory>
<fieldVisibility>PRIVATE</fieldVisibility>
<stringType>String</stringType>
<createSetters>false</createSetters>
<templateDirectory>/avro/templates/</templateDirectory>
</configuration>
</execution>
</executions>
</plugin>
解决方案2:使用相对路径。
您必须使用项目根目录的相对路径来引用模板目录。目前,我不确定它是否适用于多模块 Maven 项目。
<plugin>
<groupId>org.apache.avro</groupId>
<artifactId>avro-maven-plugin</artifactId>
<version>1.9.1</version>
<executions>
<execution>
<id>schemas</id>
<phase>generate-sources</phase>
<goals>
<goal>schema</goal>
<goal>protocol</goal>
<goal>idl-protocol</goal>
</goals>
<configuration>
<sourceDirectory>${project.basedir}/src/main/resources/avro</sourceDirectory>
<outputDirectory>${project.basedir}/src/main/java/</outputDirectory>
<fieldVisibility>PRIVATE</fieldVisibility>
<stringType>String</stringType>
<createSetters>false</createSetters>
<templateDirectory>/src/main/resources/avro/templates/</templateDirectory>
</configuration>
</execution>
</executions>
</plugin>
推荐阅读
- kubernetes - 安装 kubernetes 时未找到有效的 GPG
- php - 当控制器中存在 PHP 错误时获取旧的缓存视图 - Laravel 5.6
- javascript - 为什么我们必须永远继续使用服务器和客户端语言
- java - 类加载器子系统创建的类对象存储在哪里?
- azure - Azure DevOps API 添加公钥
- java - 使用 jlink 为胖 Jar 文件优化 JVM 模块
- javascript - 从 HTML 中删除烦人的双引号
- android - 具有新依赖项的 Firestore 崩溃应用程序
- java - 尝试执行 java 函数时出错
- javascript - JWT 令牌无法使用相同的密钥进行验证