Search in sources :

Example 21 with Archetype

use of org.apache.maven.archetype.catalog.Archetype in project indy by Commonjava.

the class ArchetypeCatalogMerger method merge.

public byte[] merge(final Collection<Transfer> sources, final Group group, final String path) {
    final ArchetypeCatalog master = new ArchetypeCatalog();
    final ArchetypeCatalogXpp3Reader reader = new ArchetypeCatalogXpp3Reader();
    final FileReader fr = null;
    boolean merged = false;
    final Set<String> seen = new HashSet<String>();
    for (final Transfer src : sources) {
        try (InputStream stream = src.openInputStream()) {
            final ArchetypeCatalog catalog = reader.read(stream, false);
            for (final Archetype arch : catalog.getArchetypes()) {
                final String key = arch.getGroupId() + ":" + arch.getArtifactId() + ":" + arch.getVersion();
                if (seen.add(key)) {
                    master.addArchetype(arch);
                }
            }
            merged = true;
        } catch (final IOException e) {
            final StoreKey key = getKey(src);
            logger.error(String.format("Cannot read archetype catalog: %s from artifact-store: %s. Reason: %s", src.getPath(), key, e.getMessage()), e);
        } catch (final XmlPullParserException e) {
            final StoreKey key = getKey(src);
            logger.error(String.format("Cannot parse archetype catalog: %s from artifact-store: %s. Reason: %s", src.getPath(), key, e.getMessage()), e);
        } finally {
            closeQuietly(fr);
        }
    }
    if (merged) {
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            new ArchetypeCatalogXpp3Writer().write(baos, master);
            return baos.toByteArray();
        } catch (final IOException e) {
            logger.error(String.format("Cannot write consolidated archetype catalog: %s to: %s. Reason: %s", path, group.getKey(), e.getMessage()), e);
        }
    }
    return null;
}
Also used : Archetype(org.apache.maven.archetype.catalog.Archetype) InputStream(java.io.InputStream) ArchetypeCatalogXpp3Writer(org.apache.maven.archetype.catalog.io.xpp3.ArchetypeCatalogXpp3Writer) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) StoreKey(org.commonjava.indy.model.core.StoreKey) ArchetypeCatalogXpp3Reader(org.apache.maven.archetype.catalog.io.xpp3.ArchetypeCatalogXpp3Reader) Transfer(org.commonjava.maven.galley.model.Transfer) FileReader(java.io.FileReader) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) ArchetypeCatalog(org.apache.maven.archetype.catalog.ArchetypeCatalog) HashSet(java.util.HashSet)

Example 22 with Archetype

use of org.apache.maven.archetype.catalog.Archetype in project maven-archetype by apache.

the class DefaultArchetypeSelectionQueryer method selectArchetype.

public Archetype selectArchetype(Map<String, List<Archetype>> catalogs, ArchetypeDefinition defaultDefinition) throws PrompterException {
    Archetype selection = null;
    Map<String, List<Archetype>> filteredCatalogs = catalogs;
    do {
        StringBuilder query = new StringBuilder("Choose archetype:\n");
        Set<String> archetypeKeys = new HashSet<String>();
        List<String> answers = new ArrayList<String>();
        Map<String, Archetype> archetypeAnswerMap = new HashMap<String, Archetype>();
        int counter = 0;
        int defaultSelection = 0;
        for (Map.Entry<String, List<Archetype>> entry : filteredCatalogs.entrySet()) {
            String catalog = entry.getKey();
            for (Archetype archetype : entry.getValue()) {
                String archetypeKey = archetype.getGroupId() + ":" + archetype.getArtifactId();
                if (!archetypeKeys.add(archetypeKey)) {
                    continue;
                }
                counter++;
                String description = archetype.getDescription();
                if (description == null) {
                    description = "-";
                }
                String answer = String.valueOf(counter);
                query.append(answer + ": " + catalog + " -> " + archetype.getGroupId() + ":" + archetype.getArtifactId() + " (" + description + ")\n");
                answers.add(answer);
                archetypeAnswerMap.put(answer, archetype);
                // the version is not tested. This is intentional.
                if (defaultDefinition != null && archetype.getGroupId().equals(defaultDefinition.getGroupId()) && archetype.getArtifactId().equals(defaultDefinition.getArtifactId())) {
                    defaultSelection = counter;
                }
            }
        }
        if (counter == 0) {
            query.append("   Your filter doesn't match any archetype (hint: enter to return to initial list)\n");
        }
        query.append("Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): ");
        String answer;
        if (defaultSelection == 0) {
            answer = prompter.prompt(query.toString());
        } else {
            answer = prompter.prompt(query.toString(), Integer.toString(defaultSelection));
        }
        if (NumberUtils.isNumber(answer)) {
            selection = archetypeAnswerMap.get(answer);
        } else {
            // not a number so apply filter and ask again
            filteredCatalogs = ArchetypeSelectorUtils.getFilteredArchetypesByCatalog(catalogs, answer);
        }
    } while (selection == null);
    return selectVersion(catalogs, selection.getGroupId(), selection.getArtifactId());
}
Also used : Archetype(org.apache.maven.archetype.catalog.Archetype) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map) SortedMap(java.util.SortedMap) HashSet(java.util.HashSet)

Example 23 with Archetype

use of org.apache.maven.archetype.catalog.Archetype in project maven-archetype by apache.

the class DefaultArchetypeSelector method selectArchetype.

public void selectArchetype(ArchetypeGenerationRequest request, Boolean interactiveMode, String catalogs) throws ArchetypeNotDefined, UnknownArchetype, UnknownGroup, IOException, PrompterException, ArchetypeSelectionFailure {
    ArchetypeDefinition definition = new ArchetypeDefinition(request);
    if (definition.isDefined() && StringUtils.isNotEmpty(request.getArchetypeRepository())) {
        getLogger().info("Archetype defined by properties");
        return;
    }
    Map<String, List<Archetype>> archetypes = getArchetypesByCatalog(request.getProjectBuildingRequest(), catalogs);
    if (StringUtils.isNotBlank(request.getFilter())) {
        // applying some filtering depending on filter parameter
        archetypes = ArchetypeSelectorUtils.getFilteredArchetypesByCatalog(archetypes, request.getFilter());
        if (archetypes.isEmpty()) {
            getLogger().info("Your filter doesn't match any archetype, so try again with another value.");
            return;
        }
    }
    if (definition.isDefined()) {
        Map.Entry<String, Archetype> found = findArchetype(archetypes, request.getArchetypeGroupId(), request.getArchetypeArtifactId());
        if (found != null) {
            String catalogKey = found.getKey();
            Archetype archetype = found.getValue();
            updateRepository(definition, archetype);
            getLogger().info("Archetype repository not defined. Using the one from " + archetype + " found in catalog " + catalogKey);
        } else {
            getLogger().warn("Archetype not found in any catalog. Falling back to central repository.");
            getLogger().warn("Add a repository with id 'archetype' in your settings.xml if archetype's repository is elsewhere.");
        }
    } else if (definition.isPartiallyDefined()) {
        Map.Entry<String, Archetype> found = findArchetype(archetypes, request.getArchetypeGroupId(), request.getArchetypeArtifactId());
        if (found != null) {
            String catalogKey = found.getKey();
            Archetype archetype = found.getValue();
            updateDefinition(definition, archetype);
            getLogger().info("Archetype " + archetype + " found in catalog " + catalogKey);
        } else {
            getLogger().warn("Specified archetype not found.");
            if (interactiveMode.booleanValue()) {
                definition.setVersion(null);
                definition.setGroupId(null);
                definition.setArtifactId(null);
            }
        }
    }
    // set the defaults - only group and version can be auto-defaulted
    if (definition.getGroupId() == null) {
        definition.setGroupId(DEFAULT_ARCHETYPE_GROUPID);
    }
    if (definition.getVersion() == null) {
        definition.setVersion(DEFAULT_ARCHETYPE_VERSION);
    }
    if (!definition.isPartiallyDefined()) {
        // if artifact ID is set to its default, we still prompt to confirm
        if (definition.getArtifactId() == null) {
            getLogger().info("No archetype defined. Using " + DEFAULT_ARCHETYPE_ARTIFACTID + " (" + definition.getGroupId() + ":" + DEFAULT_ARCHETYPE_ARTIFACTID + ":" + definition.getVersion() + ")");
            definition.setArtifactId(DEFAULT_ARCHETYPE_ARTIFACTID);
        }
        if (interactiveMode.booleanValue() && (archetypes.size() > 0)) {
            Archetype selectedArchetype = archetypeSelectionQueryer.selectArchetype(archetypes, definition);
            updateDefinition(definition, selectedArchetype);
        }
        // the latest release.
        if (!definition.isPartiallyDefined()) {
            throw new ArchetypeSelectionFailure("No valid archetypes could be found to choose.");
        }
    }
    // finally update the request with gathered information
    definition.updateRequest(request);
}
Also used : ArchetypeDefinition(org.apache.maven.archetype.ui.ArchetypeDefinition) Archetype(org.apache.maven.archetype.catalog.Archetype) UnknownArchetype(org.apache.maven.archetype.exception.UnknownArchetype) List(java.util.List) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ArchetypeSelectionFailure(org.apache.maven.archetype.exception.ArchetypeSelectionFailure)

Example 24 with Archetype

use of org.apache.maven.archetype.catalog.Archetype in project maven-archetype by apache.

the class DefaultArchetypeSelector method findArchetype.

private Map.Entry<String, Archetype> findArchetype(Map<String, List<Archetype>> archetypes, String groupId, String artifactId) {
    Archetype example = new Archetype();
    example.setGroupId(groupId);
    example.setArtifactId(artifactId);
    for (Map.Entry<String, List<Archetype>> entry : archetypes.entrySet()) {
        List<Archetype> catalog = entry.getValue();
        if (catalog.contains(example)) {
            Archetype archetype = catalog.get(catalog.indexOf(example));
            return newMapEntry(entry.getKey(), archetype);
        }
    }
    return null;
}
Also used : Archetype(org.apache.maven.archetype.catalog.Archetype) UnknownArchetype(org.apache.maven.archetype.exception.UnknownArchetype) List(java.util.List) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 25 with Archetype

use of org.apache.maven.archetype.catalog.Archetype in project maven-archetype by apache.

the class DefaultArchetypeSelectionQueryerTest method testDefaultArchetypeInMapDefaultSelection.

public void testDefaultArchetypeInMapDefaultSelection() throws PrompterException {
    Map<String, List<Archetype>> map = createDefaultArchetypeCatalog();
    MockControl control = MockControl.createControl(Prompter.class);
    Prompter prompter = (Prompter) control.getMock();
    prompter.prompt("", "2");
    control.setMatcher(createArgumentMatcher());
    control.setReturnValue("2");
    queryer.setPrompter(prompter);
    control.replay();
    ArchetypeDefinition defaultDefinition = new ArchetypeDefinition();
    defaultDefinition.setGroupId("default-groupId");
    defaultDefinition.setArtifactId("default-artifactId");
    defaultDefinition.setVersion("default-version");
    Archetype archetype = queryer.selectArchetype(map, defaultDefinition);
    control.verify();
    assertEquals("default-groupId", archetype.getGroupId());
    assertEquals("default-artifactId", archetype.getArtifactId());
    assertEquals("default-version", archetype.getVersion());
}
Also used : Prompter(org.codehaus.plexus.components.interactivity.Prompter) ArchetypeDefinition(org.apache.maven.archetype.ui.ArchetypeDefinition) MockControl(org.easymock.MockControl) Archetype(org.apache.maven.archetype.catalog.Archetype) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

Archetype (org.apache.maven.archetype.catalog.Archetype)30 ArchetypeCatalog (org.apache.maven.archetype.catalog.ArchetypeCatalog)12 File (java.io.File)10 List (java.util.List)10 ArrayList (java.util.ArrayList)8 ArchetypeGenerationRequest (org.apache.maven.archetype.ArchetypeGenerationRequest)8 Properties (java.util.Properties)7 ArtifactRepository (org.apache.maven.artifact.repository.ArtifactRepository)7 MockControl (org.easymock.MockControl)7 ArchetypeCatalogXpp3Writer (org.apache.maven.archetype.catalog.io.xpp3.ArchetypeCatalogXpp3Writer)6 UnknownArchetype (org.apache.maven.archetype.exception.UnknownArchetype)6 FileWriter (java.io.FileWriter)5 Writer (java.io.Writer)5 Map (java.util.Map)5 ArchetypeGenerationResult (org.apache.maven.archetype.ArchetypeGenerationResult)5 ArchetypeDefinition (org.apache.maven.archetype.ui.ArchetypeDefinition)5 Prompter (org.codehaus.plexus.components.interactivity.Prompter)5 IOException (java.io.IOException)4 HashMap (java.util.HashMap)4 ArchetypeManager (org.apache.maven.archetype.ArchetypeManager)4