1919
2020public class VelocityUtils {
2121
22- private static VelocityEngine velocityEngine ;
22+ private static File assetsDir = new File ("assets" );
23+ private static VelocityEngine velocityEngine = null ;
2324
24- static {
25- velocityEngine = new VelocityEngine ();
25+ private static VelocityEngine getVelocityEngine () {
2626
27- // specify resource loaders to use
28- velocityEngine .setProperty (RuntimeConstants .RESOURCE_LOADER , "file,class" );
29-
30- // for the loader 'file', set the FileResourceLoader as the class to use and use 'assets' directory for templates
31- velocityEngine .setProperty ("file.resource.loader.class" , FileResourceLoader .class .getName ());
32- velocityEngine .setProperty ("file.resource.loader.path" , "assets" );
33-
34- // for the loader 'class', set the ClasspathResourceLoader as the class to use
35- velocityEngine .setProperty ("class.resource.loader.class" , ClasspathResourceLoader .class .getName ());
36-
37- velocityEngine .init ();
38- }
39-
40- public static void render (String templatePath , File output , Object info ) throws MojoExecutionException {
41- try {
42- String data = render (templatePath , info );
43- data = data .replaceAll ("\\ r\\ n" , "\n " ).replaceAll ("\\ r" , "\n " );
44- writeStringToFile (output , data , "UTF-8" );
45- } catch (IOException e ) {
46- throw new MojoExecutionException (e .getMessage (), e );
27+ if (velocityEngine == null ) {
28+
29+ velocityEngine = new VelocityEngine ();
30+
31+ // specify resource loaders to use
32+ velocityEngine .setProperty (RuntimeConstants .RESOURCE_LOADER , "file,class" );
33+
34+ // for the loader 'file', set the FileResourceLoader as the class to use and use 'assets' directory for templates
35+ velocityEngine .setProperty ("file.resource.loader.class" , FileResourceLoader .class .getName ());
36+ velocityEngine .setProperty ("file.resource.loader.path" , assetsDir .getAbsolutePath ());
37+
38+ // for the loader 'class', set the ClasspathResourceLoader as the class to use
39+ velocityEngine .setProperty ("class.resource.loader.class" , ClasspathResourceLoader .class .getName ());
40+
41+ velocityEngine .init ();
42+
4743 }
44+
45+ return velocityEngine ;
4846 }
4947
5048 private static String render (String templatePath , Object info ) throws MojoExecutionException {
@@ -53,14 +51,24 @@ private static String render(String templatePath, Object info) throws MojoExecut
5351 context .put ("GUID" , UUID .class );
5452 context .put ("StringUtils" , StringUtils .class );
5553 context .put ("info" , info );
56- Template template = velocityEngine .getTemplate (templatePath , "UTF-8" );
54+ Template template = getVelocityEngine () .getTemplate (templatePath , "UTF-8" );
5755 StringBuilderWriter writer = new StringBuilderWriter ();
5856 template .merge (context , writer );
5957 return writer .toString ();
6058 }
61-
62- public static void setFileResourceLoaderPath (File assetsDir ) {
63- velocityEngine .setProperty ("file.resource.loader.path" , assetsDir .getAbsolutePath ());
59+
60+ public static void setAssetsDir (File assetsDir ) {
61+ VelocityUtils .assetsDir = assetsDir ;
62+ }
63+
64+ public static void render (String templatePath , File output , Object info ) throws MojoExecutionException {
65+ try {
66+ String data = render (templatePath , info );
67+ data = data .replaceAll ("\\ r\\ n" , "\n " ).replaceAll ("\\ r" , "\n " );
68+ writeStringToFile (output , data , "UTF-8" );
69+ } catch (IOException e ) {
70+ throw new MojoExecutionException (e .getMessage (), e );
71+ }
6472 }
6573
6674}
0 commit comments