Omar Amr
Omar Amr

Reputation: 3

Why do I get this error with Velocity "Velocity is not initialized correctly."?

I initialize velocity engine inside an annotation processor that extends AbstractProcessor as follows:

public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv)
{

    String fqClassName = null;
    String className = null;
    String packageName = null;
    Map<String, VariableElement> fields = new HashMap<String, VariableElement>();
    Map<String, ExecutableElement> methods = new HashMap<String, ExecutableElement>();

    ...

    if (fqClassName != null)
    {

        Properties props = new Properties();
        URL url = this.getClass().getClassLoader().getResource("velocity.properties");

        VelocityContext velocityContext = null;
        Template template = null;
        JavaFileObject javaFileObject;
        Writer writer;

        try
        {
            processingEnv.getMessager().printMessage(Kind.NOTE, "Before");
            props.load(url.openStream());

            processingEnv.getMessager().printMessage(Kind.NOTE, "Props: " + props);

            VelocityEngine velocityEngine = new VelocityEngine();
            velocityEngine.setProperty("resource.loader", "classpath");
            velocityEngine.init();
            ...
        } catch (Exception e1)
        {
            processingEnv.getMessager().printMessage(Kind.ERROR,
                    "Exception: " + e1.getMessage() + " : " + e1.getStackTrace());
        }

    }

    return false;

}

The exception happens when the init() method is called.

I export the processor with Velocity jar files as a jar file and configure eclipse to use it as an annotation processor.

Upvotes: 0

Views: 651

Answers (1)

Claude Brisson
Claude Brisson

Reputation: 4130

You're loading your custom properties in the props variable, but never passing them to Velocity.

So you should simply do something like:

velocityEngine.init(props)

Upvotes: 1

Related Questions