繁体   English   中英

如何在模式生成期间修复“org.apache.velocity.exception.ResourceNotFoundException”

[英]How to fix “org.apache.velocity.exception.ResourceNotFoundException” during schema generating

我正在尝试将 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'

但是当我从 PowerShell 执行cat C:\..........\cloud-microservices\cloud-poc/src/main/resources/avro/templates/record.vm时,它会打印文件 - 它可以找到它。

该配置适用于 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>

您可以按操作系统系列使用配置文件。 添加到你的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>

然后将templateDirectory设置为${avro.template.dir}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM