[英]How to fix “org.apache.velocity.exception.ResourceNotFoundException” during schema generating
I am trying to integrate avro maven plugin into my application.我正在尝试将 avro maven 插件集成到我的应用程序中。 I was forced to use custom templates because of Avro limitations.
由于 Avro 的限制,我被迫使用自定义模板。
When I include that plugin into build it fails (on windows, not unix) with exception:当我将该插件包含在构建中时,它会失败(在 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'
But when I do cat C:\..........\cloud-microservices\cloud-poc/src/main/resources/avro/templates/record.vm
from PowerShell then it prints the file - it can find it.但是当我从 PowerShell 执行
cat C:\..........\cloud-microservices\cloud-poc/src/main/resources/avro/templates/record.vm
时,它会打印文件 - 它可以找到它。
The configuration works on unix systems without any issues.该配置适用于 unix 系统,没有任何问题。 Here is pom:
这是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>
I also tried to use ablsoute path, different maven variables and relative path.我还尝试使用绝对路径、不同的 maven 变量和相对路径。 I tried it on few projects, without luck.
我在几个项目上尝试过,但没有运气。
I would expect the classes to be generated instead of.我希望生成这些类而不是。
The root of problem is about how velocity looking for a templates using a file path.问题的根源在于速度如何使用文件路径查找模板。 In another hand, classpath lookup have issues with maven plugin classpath.
另一方面,类路径查找与 maven 插件类路径存在问题。 At the moment I was able to fix this in a two ways: using classpath resource and using file resource.
目前我可以通过两种方式解决这个问题:使用类路径资源和使用文件资源。
Solution 1: Using a classpath.解决方案 1:使用类路径。
Since you already have you templates in resourources, it will be copied to classpath and you may refers to it once it copied to target/classes.由于您已经在资源中拥有模板,因此它将被复制到类路径,一旦复制到目标/类,您就可以引用它。 Change templateDirectory to refer classpath resource, and change phase of avro-plugin execution to be after resources copied:
更改 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>
Solutuion 2: Using a relative path.解决方案2:使用相对路径。
You must refers to a template directory, using the relative path from project root.您必须使用项目根目录的相对路径来引用模板目录。 At the moment, I'm not sure would it work properly for multimodular maven project or not.
目前,我不确定它是否适用于多模块 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>
You can use profiles by OS family.您可以按操作系统系列使用配置文件。 Add to your pom:
添加到你的pom:
<profiles>
<profile>
<id>Windows</id>
<activation>
<os>
<family>Windows</family>
</os>
</activation>
<properties>
<avro.template.dir>src/main/resources /avro/templates/</avro.template.dir>
</properties>
</profile>
<profile>
<id>unix</id>
<activation>
<os>
<family>unix</family>
</os>
</activation>
<properties>
<avro.template.dir>${basedir}/src/main/resources /avro/templates/</avro.template.dir>
</properties>
</profile>
</profiles>
And then set templateDirectory
to ${avro.template.dir}
然后将
templateDirectory
设置为${avro.template.dir}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.