Search in sources :

Example 1 with ArchetypeSelectionFailure

use of org.apache.maven.archetype.exception.ArchetypeSelectionFailure 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)

Aggregations

HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 Archetype (org.apache.maven.archetype.catalog.Archetype)1 ArchetypeSelectionFailure (org.apache.maven.archetype.exception.ArchetypeSelectionFailure)1 UnknownArchetype (org.apache.maven.archetype.exception.UnknownArchetype)1 ArchetypeDefinition (org.apache.maven.archetype.ui.ArchetypeDefinition)1