简体   繁体   中英

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

I am trying to integrate avro maven plugin into my application. I was forced to use custom templates because of Avro limitations.

When I include that plugin into build it fails (on windows, not unix) with exception:

[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.

The configuration works on unix systems without any issues. Here is 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. 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. 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.

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:

        <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.

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.

        <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:

<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}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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