Search code examples
javamavenavro

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.


Solution

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