Search in sources :

Example 1 with TemplateDescriptor

use of org.apache.maven.archetype.old.descriptor.TemplateDescriptor in project maven-archetype by apache.

the class DefaultOldArchetype method processTemplates.

private void processTemplates(File pomFile, String outputDirectory, Context context, ArchetypeDescriptor descriptor, String packageName, Model parentModel) throws ArchetypeTemplateProcessingException {
    if (!pomFile.exists()) {
        processTemplate(outputDirectory, context, ARCHETYPE_POM, new TemplateDescriptor(), false, null);
    }
    // ---------------------------------------------------------------------
    // Model generated for the new archetype, so process it now
    // ---------------------------------------------------------------------
    Model generatedModel;
    Reader pomReader = null;
    try {
        pomReader = ReaderFactory.newXmlReader(pomFile);
        MavenXpp3Reader reader = new MavenXpp3Reader();
        generatedModel = reader.read(pomReader);
    } catch (IOException e) {
        throw new ArchetypeTemplateProcessingException("Error reading POM", e);
    } catch (XmlPullParserException e) {
        throw new ArchetypeTemplateProcessingException("Error reading POM", e);
    } finally {
        IOUtil.close(pomReader);
    }
    if (parentModel != null) {
        Parent parent = new Parent();
        parent.setGroupId(parentModel.getGroupId());
        if (parent.getGroupId() == null) {
            parent.setGroupId(parentModel.getParent().getGroupId());
        }
        parent.setArtifactId(parentModel.getArtifactId());
        parent.setVersion(parentModel.getVersion());
        if (parent.getVersion() == null) {
            parent.setVersion(parentModel.getParent().getVersion());
        }
        generatedModel.setParent(parent);
        Writer pomWriter = null;
        try {
            pomWriter = WriterFactory.newXmlWriter(pomFile);
            MavenXpp3Writer writer = new MavenXpp3Writer();
            writer.write(pomWriter, generatedModel);
        } catch (IOException e) {
            throw new ArchetypeTemplateProcessingException("Error rewriting POM", e);
        } finally {
            IOUtil.close(pomWriter);
        }
    }
    // XXX: Following POM processing block may be a candidate for
    // refactoring out into service methods or moving to
    // createProjectDirectoryStructure(outputDirectory)
    Build build = generatedModel.getBuild();
    boolean overrideSrcDir = false;
    boolean overrideResourceDir = false;
    boolean overrideTestSrcDir = false;
    boolean overrideTestResourceDir = false;
    boolean foundBuildElement = build != null;
    if (getLogger().isDebugEnabled()) {
        getLogger().debug("********************* Debug info for resources created from generated Model ***********************");
        getLogger().debug("Was build element found in generated POM?: " + foundBuildElement);
    }
    // create source directory if specified in POM
    if (foundBuildElement && null != build.getSourceDirectory()) {
        getLogger().debug("Overriding default source directory ");
        overrideSrcDir = true;
        String srcDirectory = build.getSourceDirectory();
        srcDirectory = StringUtils.replace(srcDirectory, "\\", "/");
        FileUtils.mkdir(getOutputDirectory(outputDirectory, srcDirectory));
    }
    // create script source directory if specified in POM
    if (foundBuildElement && null != build.getScriptSourceDirectory()) {
        getLogger().debug("Overriding default script source directory ");
        String scriptSourceDirectory = build.getScriptSourceDirectory();
        scriptSourceDirectory = StringUtils.replace(scriptSourceDirectory, "\\", "/");
        FileUtils.mkdir(getOutputDirectory(outputDirectory, scriptSourceDirectory));
    }
    // create resource director(y/ies) if specified in POM
    if (foundBuildElement && build.getResources().size() > 0) {
        getLogger().debug("Overriding default resource directory ");
        overrideResourceDir = true;
        Iterator<?> resourceItr = build.getResources().iterator();
        while (resourceItr.hasNext()) {
            Resource resource = (Resource) resourceItr.next();
            String resourceDirectory = resource.getDirectory();
            resourceDirectory = StringUtils.replace(resourceDirectory, "\\", "/");
            FileUtils.mkdir(getOutputDirectory(outputDirectory, resourceDirectory));
        }
    }
    // create test source directory if specified in POM
    if (foundBuildElement && null != build.getTestSourceDirectory()) {
        getLogger().debug("Overriding default test directory ");
        overrideTestSrcDir = true;
        String testDirectory = build.getTestSourceDirectory();
        testDirectory = StringUtils.replace(testDirectory, "\\", "/");
        FileUtils.mkdir(getOutputDirectory(outputDirectory, testDirectory));
    }
    // create test resource directory if specified in POM
    if (foundBuildElement && build.getTestResources().size() > 0) {
        getLogger().debug("Overriding default test resource directory ");
        overrideTestResourceDir = true;
        Iterator<?> testResourceItr = build.getTestResources().iterator();
        while (testResourceItr.hasNext()) {
            Resource resource = (Resource) testResourceItr.next();
            String testResourceDirectory = resource.getDirectory();
            testResourceDirectory = StringUtils.replace(testResourceDirectory, "\\", "/");
            FileUtils.mkdir(getOutputDirectory(outputDirectory, testResourceDirectory));
        }
    }
    getLogger().debug("********************* End of debug info from resources from generated POM ***********************");
    if (descriptor.getSources().size() > 0) {
        if (!overrideSrcDir) {
            FileUtils.mkdir(outputDirectory + DEFAULT_SOURCE_DIR);
            processSources(outputDirectory, context, descriptor, packageName, DEFAULT_SOURCE_DIR);
        } else {
            processSources(outputDirectory, context, descriptor, packageName, build.getSourceDirectory());
        }
    }
    if (descriptor.getResources().size() > 0) {
        if (!overrideResourceDir) {
            FileUtils.mkdir(outputDirectory + DEFAULT_RESOURCE_DIR);
        }
        processResources(outputDirectory, context, descriptor, packageName);
    }
    if (descriptor.getTestSources().size() > 0) {
        if (!overrideTestSrcDir) {
            FileUtils.mkdir(outputDirectory + DEFAULT_TEST_SOURCE_DIR);
            processTestSources(outputDirectory, context, descriptor, packageName, DEFAULT_TEST_SOURCE_DIR);
        } else {
            processTestSources(outputDirectory, context, descriptor, packageName, build.getTestSourceDirectory());
        }
    }
    if (descriptor.getTestResources().size() > 0) {
        if (!overrideTestResourceDir) {
            FileUtils.mkdir(outputDirectory + DEFAULT_TEST_RESOURCE_DIR);
        }
        processTestResources(outputDirectory, context, descriptor, packageName);
    }
    if (descriptor.getSiteResources().size() > 0) {
        processSiteResources(outputDirectory, context, descriptor, packageName);
    }
}
Also used : Parent(org.apache.maven.model.Parent) Resource(org.apache.maven.model.Resource) SAXReader(org.dom4j.io.SAXReader) XmlStreamReader(org.apache.commons.io.input.XmlStreamReader) Reader(java.io.Reader) MavenXpp3Reader(org.apache.maven.model.io.xpp3.MavenXpp3Reader) TemplateDescriptor(org.apache.maven.archetype.old.descriptor.TemplateDescriptor) MavenXpp3Reader(org.apache.maven.model.io.xpp3.MavenXpp3Reader) IOException(java.io.IOException) Build(org.apache.maven.model.Build) Model(org.apache.maven.model.Model) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) XMLWriter(org.dom4j.io.XMLWriter) OutputStreamWriter(java.io.OutputStreamWriter) StringWriter(java.io.StringWriter) MavenXpp3Writer(org.apache.maven.model.io.xpp3.MavenXpp3Writer) Writer(java.io.Writer) MavenXpp3Writer(org.apache.maven.model.io.xpp3.MavenXpp3Writer)

Aggregations

IOException (java.io.IOException)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Reader (java.io.Reader)1 StringWriter (java.io.StringWriter)1 Writer (java.io.Writer)1 XmlStreamReader (org.apache.commons.io.input.XmlStreamReader)1 TemplateDescriptor (org.apache.maven.archetype.old.descriptor.TemplateDescriptor)1 Build (org.apache.maven.model.Build)1 Model (org.apache.maven.model.Model)1 Parent (org.apache.maven.model.Parent)1 Resource (org.apache.maven.model.Resource)1 MavenXpp3Reader (org.apache.maven.model.io.xpp3.MavenXpp3Reader)1 MavenXpp3Writer (org.apache.maven.model.io.xpp3.MavenXpp3Writer)1 XmlPullParserException (org.codehaus.plexus.util.xml.pull.XmlPullParserException)1 SAXReader (org.dom4j.io.SAXReader)1 XMLWriter (org.dom4j.io.XMLWriter)1