use of org.apache.maven.archetype.exception.ArchetypeGenerationFailure in project maven-archetype by apache.
the class DefaultFilesetArchetypeGenerator method generateArchetype.
public void generateArchetype(ArchetypeGenerationRequest request, File archetypeFile) throws UnknownArchetype, ArchetypeNotConfigured, ProjectDirectoryExists, PomFileExists, OutputFileExists, ArchetypeGenerationFailure {
ClassLoader old = Thread.currentThread().getContextClassLoader();
try {
ArchetypeDescriptor archetypeDescriptor = archetypeArtifactManager.getFileSetArchetypeDescriptor(archetypeFile);
if (!isArchetypeConfigured(archetypeDescriptor, request)) {
if (request.isInteractiveMode()) {
throw new ArchetypeNotConfigured("No archetype was chosen.", null);
}
StringBuffer exceptionMessage = new StringBuffer("Archetype " + request.getArchetypeGroupId() + ":" + request.getArchetypeArtifactId() + ":" + request.getArchetypeVersion() + " is not configured");
List<String> missingProperties = new ArrayList<String>(0);
for (RequiredProperty requiredProperty : archetypeDescriptor.getRequiredProperties()) {
if (StringUtils.isEmpty(request.getProperties().getProperty(requiredProperty.getKey()))) {
exceptionMessage.append("\n\tProperty " + requiredProperty.getKey() + " is missing.");
missingProperties.add(requiredProperty.getKey());
}
}
throw new ArchetypeNotConfigured(exceptionMessage.toString(), missingProperties);
}
Context context = prepareVelocityContext(request);
String packageName = request.getPackage();
String artifactId = request.getArtifactId();
File outputDirectoryFile = new File(request.getOutputDirectory(), artifactId);
File basedirPom = new File(request.getOutputDirectory(), Constants.ARCHETYPE_POM);
File pom = new File(outputDirectoryFile, Constants.ARCHETYPE_POM);
List<String> archetypeResources = archetypeArtifactManager.getFilesetArchetypeResources(archetypeFile);
ZipFile archetypeZipFile = archetypeArtifactManager.getArchetypeZipFile(archetypeFile);
ClassLoader archetypeJarLoader = archetypeArtifactManager.getArchetypeJarLoader(archetypeFile);
Thread.currentThread().setContextClassLoader(archetypeJarLoader);
if (archetypeDescriptor.isPartial()) {
getLogger().debug("Processing partial archetype " + archetypeDescriptor.getName());
if (outputDirectoryFile.exists()) {
if (!pom.exists()) {
throw new PomFileExists("This is a partial archetype and the pom.xml file doesn't exist.");
}
processPomWithMerge(context, pom, "");
processArchetypeTemplatesWithWarning(archetypeDescriptor, archetypeResources, archetypeZipFile, "", context, packageName, outputDirectoryFile);
} else {
if (basedirPom.exists()) {
processPomWithMerge(context, basedirPom, "");
processArchetypeTemplatesWithWarning(archetypeDescriptor, archetypeResources, archetypeZipFile, "", context, packageName, new File(request.getOutputDirectory()));
} else {
processPom(context, pom, "");
processArchetypeTemplates(archetypeDescriptor, archetypeResources, archetypeZipFile, "", context, packageName, outputDirectoryFile);
}
}
if (archetypeDescriptor.getModules().size() > 0) {
getLogger().info("Modules ignored in partial mode");
}
} else {
getLogger().debug("Processing complete archetype " + archetypeDescriptor.getName());
if (outputDirectoryFile.exists() && pom.exists()) {
throw new ProjectDirectoryExists("A Maven 2 project already exists in the directory " + outputDirectoryFile.getPath());
}
if (outputDirectoryFile.exists()) {
getLogger().warn("The directory " + outputDirectoryFile.getPath() + " already exists.");
}
context.put("rootArtifactId", artifactId);
processFilesetModule(artifactId, artifactId, archetypeResources, pom, archetypeZipFile, "", basedirPom, outputDirectoryFile, packageName, archetypeDescriptor, context);
}
String postGenerationScript = archetypeArtifactManager.getPostGenerationScript(archetypeFile);
if (postGenerationScript != null) {
getLogger().info("Executing " + Constants.ARCHETYPE_POST_GENERATION_SCRIPT + " post-generation script");
Binding binding = new Binding();
final Properties archetypeGeneratorProperties = new Properties();
archetypeGeneratorProperties.putAll(System.getProperties());
if (request.getProperties() != null) {
archetypeGeneratorProperties.putAll(request.getProperties());
}
for (Map.Entry<Object, Object> entry : archetypeGeneratorProperties.entrySet()) {
binding.setVariable(entry.getKey().toString(), entry.getValue());
}
binding.setVariable("request", request);
GroovyShell shell = new GroovyShell(binding);
shell.evaluate(postGenerationScript);
}
// ----------------------------------------------------------------------
if (getLogger().isInfoEnabled()) {
getLogger().info("Project created from Archetype in dir: " + outputDirectoryFile.getAbsolutePath());
}
} catch (FileNotFoundException ex) {
throw new ArchetypeGenerationFailure(ex);
} catch (IOException ex) {
throw new ArchetypeGenerationFailure(ex);
} catch (XmlPullParserException ex) {
throw new ArchetypeGenerationFailure(ex);
} catch (DocumentException ex) {
throw new ArchetypeGenerationFailure(ex);
} catch (ArchetypeGenerationFailure ex) {
throw new ArchetypeGenerationFailure(ex);
} catch (InvalidPackaging ex) {
throw new ArchetypeGenerationFailure(ex);
} finally {
Thread.currentThread().setContextClassLoader(old);
}
}
use of org.apache.maven.archetype.exception.ArchetypeGenerationFailure 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;
}
Aggregations