简体   繁体   中英

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.

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)

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