11package io .github .fvarrui .javapackager .utils ;
22
3+ import static org .apache .commons .io .FileUtils .writeStringToFile ;
4+
35import java .io .File ;
4- import java .io .FileWriter ;
56import java .io .IOException ;
67import java .util .Map ;
78
1213import org .apache .velocity .runtime .RuntimeConstants ;
1314import org .apache .velocity .runtime .resource .loader .ClasspathResourceLoader ;
1415import org .apache .velocity .runtime .resource .loader .FileResourceLoader ;
16+ import org .apache .velocity .util .StringBuilderWriter ;
1517
1618public class VelocityUtils {
1719
@@ -35,22 +37,21 @@ public class VelocityUtils {
3537
3638 public static void render (String templatePath , File output , Map <String , Object > info ) throws MojoExecutionException {
3739 try {
38-
39- VelocityContext context = new VelocityContext ();
40- context .put ("info" , info );
41-
42- Template template = velocityEngine .getTemplate (templatePath , "UTF-8" );
43-
44- FileWriter fw = new FileWriter (output );
45-
46- template .merge (context , fw );
47-
48- fw .flush ();
49- fw .close ();
50-
40+ String data = render (templatePath , info );
41+ data = data .replaceAll ("\\ r\\ n" , "\n " ).replaceAll ("\\ r" , "\n " );
42+ writeStringToFile (output , data , "UTF-8" );
5143 } catch (IOException e ) {
5244 throw new MojoExecutionException (e .getMessage (), e );
5345 }
5446 }
47+
48+ public static String render (String templatePath , Map <String , Object > info ) throws MojoExecutionException {
49+ VelocityContext context = new VelocityContext ();
50+ context .put ("info" , info );
51+ Template template = velocityEngine .getTemplate (templatePath , "UTF-8" );
52+ StringBuilderWriter writer = new StringBuilderWriter ();
53+ template .merge (context , writer );
54+ return writer .toString ();
55+ }
5556
5657}
0 commit comments