fd025
fd025

Reputation: 41

Velocity Reflection on Maven Archetypes: get data from Json

I would like to get data from a json file to build a class in a maven-archetype. I'm trying to do it with reflection in velocity, but I can only use java SE classes. I tried to add dependency to archetype pom, but velocity doesn't read that. How can I put other classes (as JsonTool) in velocity context for a mvn archetype?

This is what I tried:

#set($str = $package.getClass().forName("org.apache.velocity.tools.generic.JsonTool").newInstance());

On install I have this exception:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-archetype-plugin:3.0.1:integration-test (default-integration-test) on project:
[ERROR] **Archetype IT 'basic' failed: org.apache.maven.archetype.exception.ArchetypeGenerationFailure: Error merging velocity templates: Invocation of method 'forName' in  class java.lang.Class threw exception java.lang.ClassNotFoundException: org.apache.velocity.tools.generic.JsonTool at archetype-resources/src/main/java/entity/__classname__Entity.java[line 6, column 33]**

Upvotes: 1

Views: 488

Answers (1)

Claude Brisson
Claude Brisson

Reputation: 4130

Check your dependencies and your versions.

Which version of velocity-engine-core and velocity-tools-generic are you using? JsonTool appeared in velocity-tools-generic 3.0, which requires at least velocity-engine-core 2.0.

The proper dependencies section should look like:

<dependency>
    <groupId>org.apache.velocity.tools</groupId>
    <artifactId>velicity-tools-generic</artifactId>
    <version>3.0</version>
</dependency>

Upvotes: 1

Related Questions