|
22 | 22 |
|
23 | 23 | import java.io.File;
|
24 | 24 | import java.io.FileOutputStream;
|
| 25 | +import java.io.IOException; |
25 | 26 | import java.io.OutputStreamWriter;
|
26 | 27 | import java.io.Writer;
|
| 28 | +import java.nio.charset.Charset; |
27 | 29 | import java.util.ArrayList;
|
28 | 30 | import java.util.Collections;
|
29 | 31 | import java.util.List;
|
|
57 | 59 | import org.apache.velocity.exception.ResourceNotFoundException;
|
58 | 60 | import org.apache.velocity.exception.VelocityException;
|
59 | 61 | import org.apache.velocity.tools.ToolManager;
|
60 |
| -import org.codehaus.plexus.util.ReaderFactory; |
61 | 62 | import org.codehaus.plexus.velocity.VelocityComponent;
|
62 | 63 |
|
63 | 64 | /**
|
@@ -637,61 +638,50 @@ protected void doGenerate(List<Release> releases, Release release) throws MojoEx
|
637 | 638 | }
|
638 | 639 |
|
639 | 640 | /**
|
640 |
| - * Create the velocity template |
| 641 | + * Create the velocity template. |
641 | 642 | *
|
642 | 643 | * @param context velocity context that has the parameter values
|
643 | 644 | * @param outputDirectory directory where the file will be generated
|
644 | 645 | * @param template velocity template which will the context be merged
|
645 |
| - * @param announcementFile The file name of the generated announcement |
646 |
| - * @throws VelocityException in case of errors. |
647 |
| - * @throws MojoExecutionException in case of errors. |
| 646 | + * @param announcementFile the file name of the generated announcement |
| 647 | + * @throws VelocityException in case of error processing the Velocty template |
| 648 | + * @throws MojoExecutionException in case of errors |
648 | 649 | */
|
649 | 650 | public void processTemplate(Context context, File outputDirectory, String template, String announcementFile)
|
650 | 651 | throws VelocityException, MojoExecutionException {
|
651 |
| - File f; |
652 | 652 |
|
653 | 653 | // Use the name of the template as a default value
|
654 | 654 | if (announcementFile == null || announcementFile.isEmpty()) {
|
655 | 655 | announcementFile = template;
|
656 | 656 | }
|
657 | 657 |
|
658 |
| - try { |
659 |
| - f = new File(outputDirectory, announcementFile); |
660 |
| - |
661 |
| - if (!f.getParentFile().exists()) { |
662 |
| - f.getParentFile().mkdirs(); |
| 658 | + if (!outputDirectory.exists()) { |
| 659 | + if (!outputDirectory.mkdirs()) { |
| 660 | + throw new MojoExecutionException("Faield to create directory " + outputDirectory); |
663 | 661 | }
|
| 662 | + } |
664 | 663 |
|
665 |
| - VelocityEngine engine = velocity.getEngine(); |
666 |
| - |
667 |
| - engine.setApplicationAttribute("baseDirectory", basedir); |
| 664 | + File f = new File(outputDirectory, announcementFile); |
668 | 665 |
|
669 |
| - if (templateEncoding == null || templateEncoding.isEmpty()) { |
670 |
| - templateEncoding = ReaderFactory.FILE_ENCODING; |
671 |
| - getLog().warn("File encoding has not been set, using platform encoding " + templateEncoding |
672 |
| - + ", i.e. build is platform dependent!"); |
673 |
| - } |
| 666 | + VelocityEngine engine = velocity.getEngine(); |
674 | 667 |
|
675 |
| - Writer writer = new OutputStreamWriter(new FileOutputStream(f), templateEncoding); |
| 668 | + engine.setApplicationAttribute("baseDirectory", basedir); |
676 | 669 |
|
| 670 | + if (templateEncoding == null || templateEncoding.isEmpty()) { |
| 671 | + templateEncoding = Charset.defaultCharset().name(); |
| 672 | + getLog().warn("File encoding has not been set, using platform encoding " + templateEncoding |
| 673 | + + ", i.e. build is platform dependent!"); |
| 674 | + } |
| 675 | + try (Writer writer = new OutputStreamWriter(new FileOutputStream(f), templateEncoding)) { |
677 | 676 | Template velocityTemplate = engine.getTemplate(templateDirectory + "/" + template, templateEncoding);
|
678 |
| - |
679 | 677 | velocityTemplate.merge(context, writer);
|
680 |
| - |
681 |
| - writer.flush(); |
682 |
| - |
683 |
| - writer.close(); |
684 |
| - |
685 | 678 | getLog().info("Created template " + f);
|
686 | 679 | } catch (ResourceNotFoundException rnfe) {
|
687 | 680 | throw new ResourceNotFoundException("Template not found. ( " + templateDirectory + "/" + template + " )");
|
688 | 681 | } catch (VelocityException ve) {
|
689 |
| - throw new VelocityException(ve.toString()); |
690 |
| - } catch (Exception e) { |
691 |
| - if (e.getCause() != null) { |
692 |
| - getLog().warn(e.getCause()); |
693 |
| - } |
694 |
| - throw new MojoExecutionException(e.toString(), e.getCause()); |
| 682 | + throw ve; |
| 683 | + } catch (RuntimeException | IOException e) { |
| 684 | + throw new MojoExecutionException(e.toString(), e); |
695 | 685 | }
|
696 | 686 | }
|
697 | 687 |
|
|
0 commit comments