Search in sources :

Example 11 with UnknownArchetype

use of org.apache.maven.archetype.exception.UnknownArchetype 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 12 with UnknownArchetype

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

the class DefaultArchetypeSelectorTest method testArchetypeArtifactIdNotInRequest.

public void testArchetypeArtifactIdNotInRequest() throws PrompterException, IOException, UnknownGroup, ArchetypeSelectionFailure, UnknownArchetype, ArchetypeNotDefined {
    ArchetypeGenerationRequest request = new ArchetypeGenerationRequest();
    MockControl control = MockControl.createControl(ArchetypeSelectionQueryer.class);
    ArchetypeSelectionQueryer queryer = (ArchetypeSelectionQueryer) control.getMock();
    queryer.selectArchetype(Collections.<String, List<Archetype>>emptyMap(), new ArchetypeDefinition());
    control.setMatcher(MockControl.ALWAYS_MATCHER);
    Archetype archetype = new Archetype();
    archetype.setArtifactId("set-artifactId");
    archetype.setGroupId("set-groupId");
    archetype.setVersion("set-version");
    control.setReturnValue(archetype);
    control.replay();
    selector.setArchetypeSelectionQueryer(queryer);
    selector.selectArchetype(request, Boolean.TRUE, "");
    control.verify();
    assertEquals("set-groupId", request.getArchetypeGroupId());
    assertEquals("set-artifactId", request.getArchetypeArtifactId());
    assertEquals("set-version", request.getArchetypeVersion());
}
Also used : ArchetypeDefinition(org.apache.maven.archetype.ui.ArchetypeDefinition) MockControl(org.easymock.MockControl) Archetype(org.apache.maven.archetype.catalog.Archetype) UnknownArchetype(org.apache.maven.archetype.exception.UnknownArchetype) ArchetypeGenerationRequest(org.apache.maven.archetype.ArchetypeGenerationRequest)

Aggregations

UnknownArchetype (org.apache.maven.archetype.exception.UnknownArchetype)12 IOException (java.io.IOException)5 Archetype (org.apache.maven.archetype.catalog.Archetype)5 File (java.io.File)4 ZipFile (java.util.zip.ZipFile)4 ArchetypeDefinition (org.apache.maven.archetype.ui.ArchetypeDefinition)3 ArtifactRepository (org.apache.maven.artifact.repository.ArtifactRepository)3 ArrayList (java.util.ArrayList)2 Properties (java.util.Properties)2 ArchetypeGenerationRequest (org.apache.maven.archetype.ArchetypeGenerationRequest)2 ArchetypeNotDefined (org.apache.maven.archetype.exception.ArchetypeNotDefined)2 MavenArtifactRepository (org.apache.maven.artifact.repository.MavenArtifactRepository)2 MockControl (org.easymock.MockControl)2 IPortletFramework (com.liferay.ide.project.core.IPortletFramework)1 NewLiferayProfile (com.liferay.ide.project.core.model.NewLiferayProfile)1 PluginType (com.liferay.ide.project.core.model.PluginType)1 ProjectName (com.liferay.ide.project.core.model.ProjectName)1 Reader (java.io.Reader)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1