Search in sources :

Example 1 with InvalidPackaging

use of org.apache.maven.archetype.exception.InvalidPackaging 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);
    }
}
Also used : ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) Properties(java.util.Properties) ArchetypeNotConfigured(org.apache.maven.archetype.exception.ArchetypeNotConfigured) RequiredProperty(org.apache.maven.archetype.metadata.RequiredProperty) DocumentException(org.dom4j.DocumentException) PomFileExists(org.apache.maven.archetype.exception.PomFileExists) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) ProjectDirectoryExists(org.apache.maven.archetype.exception.ProjectDirectoryExists) Context(org.apache.velocity.context.Context) VelocityContext(org.apache.velocity.VelocityContext) Binding(groovy.lang.Binding) InvalidPackaging(org.apache.maven.archetype.exception.InvalidPackaging) IOException(java.io.IOException) AbstractArchetypeDescriptor(org.apache.maven.archetype.metadata.AbstractArchetypeDescriptor) ArchetypeDescriptor(org.apache.maven.archetype.metadata.ArchetypeDescriptor) GroovyShell(groovy.lang.GroovyShell) ZipFile(java.util.zip.ZipFile) ZipFile(java.util.zip.ZipFile) File(java.io.File) Map(java.util.Map) ArchetypeGenerationFailure(org.apache.maven.archetype.exception.ArchetypeGenerationFailure)

Example 2 with InvalidPackaging

use of org.apache.maven.archetype.exception.InvalidPackaging in project maven-archetype by apache.

the class DefaultPomManager method addModule.

public void addModule(File pom, String artifactId) throws IOException, XmlPullParserException, DocumentException, InvalidPackaging {
    boolean found = false;
    StringWriter writer = new StringWriter();
    Reader fileReader = null;
    try {
        fileReader = ReaderFactory.newXmlReader(pom);
        SAXReader reader = new SAXReader();
        Document document = reader.read(fileReader);
        Element project = document.getRootElement();
        String packaging = null;
        Element packagingElement = project.element("packaging");
        if (packagingElement != null) {
            packaging = packagingElement.getStringValue();
        }
        if (!"pom".equals(packaging)) {
            throw new InvalidPackaging("Unable to add module to the current project as it is not of packaging type 'pom'");
        }
        Element modules = project.element("modules");
        if (modules == null) {
            modules = project.addText("  ").addElement("modules");
            modules.setText("\n  ");
            project.addText("\n");
        }
        // TODO: change to while loop
        for (@SuppressWarnings("unchecked") Iterator<Element> i = modules.elementIterator("module"); i.hasNext() && !found; ) {
            Element module = i.next();
            if (module.getText().equals(artifactId)) {
                found = true;
            }
        }
        if (!found) {
            Node lastTextNode = null;
            for (@SuppressWarnings("unchecked") Iterator<Node> i = modules.nodeIterator(); i.hasNext(); ) {
                Node node = i.next();
                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    lastTextNode = null;
                } else if (node.getNodeType() == Node.TEXT_NODE) {
                    lastTextNode = node;
                }
            }
            if (lastTextNode != null) {
                modules.remove(lastTextNode);
            }
            modules.addText("\n    ");
            modules.addElement("module").setText(artifactId);
            modules.addText("\n  ");
            XMLWriter xmlWriter = new XMLWriter(writer);
            xmlWriter.write(document);
            FileUtils.fileWrite(pom.getAbsolutePath(), writer.toString());
        }
    // end if
    } finally {
        IOUtil.close(fileReader);
    }
}
Also used : SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) InvalidPackaging(org.apache.maven.archetype.exception.InvalidPackaging) Node(org.dom4j.Node) SAXReader(org.dom4j.io.SAXReader) Reader(java.io.Reader) MavenXpp3Reader(org.apache.maven.model.io.xpp3.MavenXpp3Reader) Document(org.dom4j.Document) XMLWriter(org.dom4j.io.XMLWriter) StringWriter(java.io.StringWriter)

Aggregations

InvalidPackaging (org.apache.maven.archetype.exception.InvalidPackaging)2 Binding (groovy.lang.Binding)1 GroovyShell (groovy.lang.GroovyShell)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 Reader (java.io.Reader)1 StringWriter (java.io.StringWriter)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 Properties (java.util.Properties)1 ZipFile (java.util.zip.ZipFile)1 ArchetypeGenerationFailure (org.apache.maven.archetype.exception.ArchetypeGenerationFailure)1 ArchetypeNotConfigured (org.apache.maven.archetype.exception.ArchetypeNotConfigured)1 PomFileExists (org.apache.maven.archetype.exception.PomFileExists)1 ProjectDirectoryExists (org.apache.maven.archetype.exception.ProjectDirectoryExists)1 AbstractArchetypeDescriptor (org.apache.maven.archetype.metadata.AbstractArchetypeDescriptor)1 ArchetypeDescriptor (org.apache.maven.archetype.metadata.ArchetypeDescriptor)1 RequiredProperty (org.apache.maven.archetype.metadata.RequiredProperty)1 MavenXpp3Reader (org.apache.maven.model.io.xpp3.MavenXpp3Reader)1