com.github.sviperll.maven.plugin.mustache.MustacheMojo Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mustache-maven-plugin Show documentation
Show all versions of mustache-maven-plugin Show documentation
Render mustache templates during the build
/*
* Copyright 2015 Victor Nazarov .
*/
package com.github.sviperll.maven.plugin.mustache;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.mustachejava.Mustache;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.util.Map;
import java.util.Properties;
import java.util.TreeMap;
import org.apache.maven.plugin.AbstractMojo;
/**
*
* @author Victor Nazarov <[email protected]>
*/
abstract class MustacheMojo extends AbstractMojo {
Map loadPropertiesContext(File contextFile, Charset charset) throws IOException {
Properties properties = loadProperties(contextFile, charset);
Map result = new TreeMap();
MapFiller filler = new MapFiller(result);
for (String key: properties.stringPropertyNames()) {
filler.put(key, properties.getProperty(key));
}
return result;
}
Object loadJsonContext(File contextFile, Charset charset) throws IOException {
JsonFactory jsonFactory = new JsonFactory();
ObjectMapper mapper = new ObjectMapper(jsonFactory);
JsonLoader jsonLoader = new JsonLoader(mapper, getLog());
return jsonLoader.load(contextFile, charset);
}
void renderMustache(Mustache mustache, Object context, File outputFile, Charset charset) throws IOException {
File outputDirectory = outputFile.getCanonicalFile().getParentFile();
if (outputDirectory != null && !outputDirectory.exists()) {
boolean success = outputDirectory.mkdirs();
if (!success)
throw new FileNotFoundException("Unable to create directory: " + outputDirectory);
}
FileOutputStream fileOutputStream = new FileOutputStream(outputFile);
try {
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
try {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(bufferedOutputStream, charset);
try {
BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
try {
MustacheRenderer renderer = new MustacheRenderer(bufferedWriter);
renderer.render(mustache, context);
} finally {
try {
bufferedWriter.close();
} catch (IOException ex) {
getLog().error("Unable to close bufferedWriter", ex);
} catch (RuntimeException ex) {
getLog().error("Unable to close bufferedWriter", ex);
}
}
} finally {
try {
outputStreamWriter.close();
} catch (IOException ex) {
getLog().error("Unable to close outputStreamWriter", ex);
} catch (RuntimeException ex) {
getLog().error("Unable to close outputStreamWriter", ex);
}
}
} finally {
try {
bufferedOutputStream.close();
} catch (IOException ex) {
getLog().error("Unable to close bufferedOutputStream", ex);
} catch (RuntimeException ex) {
getLog().error("Unable to close bufferedOutputStream", ex);
}
}
} finally {
try {
fileOutputStream.close();
} catch (IOException ex) {
getLog().error("Unable to close fileOutputStream", ex);
} catch (RuntimeException ex) {
getLog().error("Unable to close fileOutputStream", ex);
}
}
}
Properties loadProperties(File file, Charset charset) throws FileNotFoundException, IOException {
FileInputStream fileInputStream = new FileInputStream(file);
try {
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
try {
InputStreamReader inputStreamReader = new InputStreamReader(bufferedInputStream, charset);
try {
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
try {
Properties properties = new Properties();
properties.load(bufferedReader);
return properties;
} finally {
try {
bufferedReader.close();
} catch (IOException ex) {
getLog().error("Error closing bufferedReader", ex);
} catch (RuntimeException ex) {
getLog().error("Error closing bufferedReader", ex);
}
}
} finally {
try {
inputStreamReader.close();
} catch (IOException ex) {
getLog().error("Error closing inputStreamReader", ex);
} catch (RuntimeException ex) {
getLog().error("Error closing inputStreamReader", ex);
}
}
} finally {
try {
bufferedInputStream.close();
} catch (IOException ex) {
getLog().error("Error closing bufferedInputStream", ex);
} catch (RuntimeException ex) {
getLog().error("Error closing bufferedInputStream", ex);
}
}
} finally {
try {
fileInputStream.close();
} catch (IOException ex) {
getLog().error("Error closing fileInputStream", ex);
} catch (RuntimeException ex) {
getLog().error("Error closing fileInputStream", ex);
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy