Search in sources :

Example 1 with OutputFileExists

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;
}
Also used : StringWriter(java.io.StringWriter) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) OutputFileExists(org.apache.maven.archetype.exception.OutputFileExists) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) StringWriter(java.io.StringWriter) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) FileNotFoundException(java.io.FileNotFoundException) DocumentException(org.dom4j.DocumentException) IOException(java.io.IOException) ArchetypeGenerationFailure(org.apache.maven.archetype.exception.ArchetypeGenerationFailure)

Example 2 with OutputFileExists

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;
}
Also used : InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) OutputFileExists(org.apache.maven.archetype.exception.OutputFileExists)

Aggregations

FileOutputStream (java.io.FileOutputStream)2 OutputFileExists (org.apache.maven.archetype.exception.OutputFileExists)2 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 StringWriter (java.io.StringWriter)1 Writer (java.io.Writer)1 ZipEntry (java.util.zip.ZipEntry)1 ArchetypeGenerationFailure (org.apache.maven.archetype.exception.ArchetypeGenerationFailure)1 XmlPullParserException (org.codehaus.plexus.util.xml.pull.XmlPullParserException)1 DocumentException (org.dom4j.DocumentException)1