use of org.apache.maven.archetype.exception.OutputFileExists in project maven-archetype by apache.
the class DefaultFilesetArchetypeGenerator method processTemplate.
@SuppressWarnings("deprecation")
private boolean processTemplate(File outFile, Context context, String templateFileName, String encoding, boolean failIfExists) throws OutputFileExists, ArchetypeGenerationFailure {
templateFileName = templateFileName.replace(File.separatorChar, '/');
String localTemplateFileName = templateFileName.replace('/', File.separatorChar);
if (!templateFileName.equals(localTemplateFileName) && !velocity.getEngine().templateExists(templateFileName) && velocity.getEngine().templateExists(localTemplateFileName)) {
templateFileName = localTemplateFileName;
}
getLogger().debug("Processing template " + templateFileName);
if (outFile.exists()) {
if (failIfExists) {
throw new OutputFileExists("Don't override file " + outFile.getAbsolutePath());
}
getLogger().warn("Don't override file " + outFile);
return false;
}
if (templateFileName.endsWith("/")) {
getLogger().debug("Creating directory " + outFile);
outFile.mkdirs();
return true;
}
if (!outFile.getParentFile().exists()) {
outFile.getParentFile().mkdirs();
}
getLogger().debug("Merging into " + outFile);
Writer writer = null;
try {
StringWriter stringWriter = new StringWriter();
velocity.getEngine().mergeTemplate(templateFileName, encoding, context, stringWriter);
writer = new OutputStreamWriter(new FileOutputStream(outFile), encoding);
writer.write(StringUtils.unifyLineSeparators(stringWriter.toString()));
writer.flush();
} catch (Exception e) {
throw new ArchetypeGenerationFailure("Error merging velocity templates: " + e.getMessage(), e);
} finally {
IOUtil.close(writer);
}
return true;
}
use of org.apache.maven.archetype.exception.OutputFileExists in project maven-archetype by apache.
the class DefaultFilesetArchetypeGenerator method copyFile.
private boolean copyFile(final File outFile, final String template, final boolean failIfExists, final ZipFile archetypeZipFile) throws FileNotFoundException, OutputFileExists, IOException {
getLogger().debug("Copying file " + template);
if (failIfExists && outFile.exists()) {
throw new OutputFileExists("Don't rewrite file " + outFile.getName());
} else if (outFile.exists()) {
getLogger().warn("CP Don't override file " + outFile);
return false;
}
ZipEntry input = archetypeZipFile.getEntry(Constants.ARCHETYPE_RESOURCES + "/" + template);
if (input.isDirectory()) {
outFile.mkdirs();
} else {
InputStream inputStream = null;
OutputStream out = null;
try {
inputStream = archetypeZipFile.getInputStream(input);
outFile.getParentFile().mkdirs();
out = new FileOutputStream(outFile);
IOUtil.copy(inputStream, out);
} finally {
IOUtil.close(inputStream);
IOUtil.close(out);
}
}
return true;
}
Aggregations