Search in sources :

Example 1 with ArchetypeDefinition

use of org.apache.maven.archetype.ui.ArchetypeDefinition in project maven-archetype by apache.

the class DefaultArchetypeSelectionQueryerTest method testDefaultArchetypeInMapOtherSelection.

public void testDefaultArchetypeInMapOtherSelection() 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("1");
    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("set-groupId", archetype.getGroupId());
    assertEquals("set-artifactId", archetype.getArtifactId());
    assertEquals("set-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)

Example 2 with ArchetypeDefinition

use of org.apache.maven.archetype.ui.ArchetypeDefinition in project maven-archetype by apache.

the class DefaultArchetypeCreationConfigurator method configureArchetypeCreation.

public Properties configureArchetypeCreation(MavenProject project, Boolean interactiveMode, Properties commandLineProperties, File propertyFile, List<String> languages) throws IOException, ArchetypeNotDefined, ArchetypeNotConfigured, PrompterException, TemplateCreationException {
    Properties properties = initialiseArchetypeProperties(commandLineProperties, propertyFile);
    ArchetypeDefinition archetypeDefinition = archetypeFactory.createArchetypeDefinition(properties);
    if (!archetypeDefinition.isDefined()) {
        archetypeDefinition = defineDefaultArchetype(project, properties);
    }
    ArchetypeConfiguration archetypeConfiguration = archetypeFactory.createArchetypeConfiguration(project, archetypeDefinition, properties);
    String resolvedPackage = archetypeFilesResolver.resolvePackage(project.getBasedir(), languages);
    if (!archetypeConfiguration.isConfigured()) {
        archetypeConfiguration = defineDefaultConfiguration(project, archetypeDefinition, resolvedPackage, properties);
    }
    if (interactiveMode.booleanValue()) {
        getLogger().debug("Entering interactive mode");
        boolean confirmed = false;
        while (!confirmed) {
            if (// <editor-fold text="...">
            !archetypeDefinition.isDefined()) {
                getLogger().debug("Archetype is yet not defined");
                if (!archetypeDefinition.isGroupDefined()) {
                    getLogger().debug("Asking for archetype's groupId");
                    archetypeDefinition.setGroupId(archetypeCreationQueryer.getArchetypeGroupId(project.getGroupId()));
                }
                if (!archetypeDefinition.isArtifactDefined()) {
                    getLogger().debug("Asking for archetype's artifactId");
                    archetypeDefinition.setArtifactId(archetypeCreationQueryer.getArchetypeArtifactId(project.getArtifactId() + Constants.ARCHETYPE_SUFFIX));
                }
                if (!archetypeDefinition.isVersionDefined()) {
                    getLogger().debug("Asking for archetype's version");
                    archetypeDefinition.setVersion(archetypeCreationQueryer.getArchetypeVersion(project.getVersion()));
                }
                archetypeFactory.updateArchetypeConfiguration(archetypeConfiguration, archetypeDefinition);
            }
            if (// <editor-fold text="...">
            !archetypeConfiguration.isConfigured()) {
                getLogger().debug("Archetype is not yet configured");
                if (!archetypeConfiguration.isConfigured(Constants.GROUP_ID)) {
                    getLogger().debug("Asking for project's groupId");
                    archetypeConfiguration.setProperty(Constants.GROUP_ID, archetypeCreationQueryer.getGroupId(archetypeConfiguration.getDefaultValue(Constants.GROUP_ID)));
                }
                if (!archetypeConfiguration.isConfigured(Constants.ARTIFACT_ID)) {
                    getLogger().debug("Asking for project's artifactId");
                    archetypeConfiguration.setProperty(Constants.ARTIFACT_ID, archetypeCreationQueryer.getArtifactId(archetypeConfiguration.getDefaultValue(Constants.ARTIFACT_ID)));
                }
                if (!archetypeConfiguration.isConfigured(Constants.VERSION)) {
                    getLogger().debug("Asking for project's version");
                    archetypeConfiguration.setProperty(Constants.VERSION, archetypeCreationQueryer.getVersion(archetypeConfiguration.getDefaultValue(Constants.VERSION)));
                }
                if (!archetypeConfiguration.isConfigured(Constants.PACKAGE)) {
                    getLogger().debug("Asking for project's package");
                    archetypeConfiguration.setProperty(Constants.PACKAGE, archetypeCreationQueryer.getPackage(StringUtils.isEmpty(resolvedPackage) ? archetypeConfiguration.getDefaultValue(Constants.PACKAGE) : resolvedPackage));
                }
            }
            // </editor-fold>
            boolean stopAddingProperties = false;
            while (!stopAddingProperties) {
                getLogger().debug("Asking for another required property");
                stopAddingProperties = !archetypeCreationQueryer.askAddAnotherProperty();
                if (!stopAddingProperties) {
                    getLogger().debug("Asking for required property key");
                    String propertyKey = archetypeCreationQueryer.askNewPropertyKey();
                    getLogger().debug("Asking for required property value");
                    String replacementValue = archetypeCreationQueryer.askReplacementValue(propertyKey, archetypeConfiguration.getDefaultValue(propertyKey));
                    archetypeConfiguration.setDefaultProperty(propertyKey, replacementValue);
                    archetypeConfiguration.setProperty(propertyKey, replacementValue);
                }
            }
            getLogger().debug("Asking for configuration confirmation");
            if (archetypeCreationQueryer.confirmConfiguration(archetypeConfiguration)) {
                confirmed = true;
            } else {
                getLogger().debug("Reseting archetype's definition and configuration");
                archetypeConfiguration.reset();
                archetypeDefinition.reset();
            }
        }
    // end while
    } else {
        getLogger().debug("Entering batch mode");
        if (!archetypeDefinition.isDefined()) {
            throw new ArchetypeNotDefined("The archetype is not defined");
        } else if (!archetypeConfiguration.isConfigured()) {
            throw new ArchetypeNotConfigured("The archetype is not configured", null);
        }
    }
    return archetypeConfiguration.toProperties();
}
Also used : ArchetypeNotConfigured(org.apache.maven.archetype.exception.ArchetypeNotConfigured) ArchetypeDefinition(org.apache.maven.archetype.ui.ArchetypeDefinition) ArchetypeConfiguration(org.apache.maven.archetype.ui.ArchetypeConfiguration) Properties(java.util.Properties) ArchetypeNotDefined(org.apache.maven.archetype.exception.ArchetypeNotDefined)

Example 3 with ArchetypeDefinition

use of org.apache.maven.archetype.ui.ArchetypeDefinition in project maven-archetype by apache.

the class DefaultArchetypeGenerationConfigurator method configureArchetype.

public void configureArchetype(ArchetypeGenerationRequest request, Boolean interactiveMode, Properties executionProperties) throws ArchetypeNotDefined, UnknownArchetype, ArchetypeNotConfigured, IOException, PrompterException, ArchetypeGenerationConfigurationFailure {
    ArtifactRepository localRepository = request.getLocalRepository();
    ArtifactRepository archetypeRepository = null;
    List<ArtifactRepository> repositories = new ArrayList<ArtifactRepository>();
    Properties properties = new Properties(executionProperties);
    ArchetypeDefinition ad = new ArchetypeDefinition(request);
    if (!ad.isDefined()) {
        if (!interactiveMode.booleanValue()) {
            throw new ArchetypeNotDefined("No archetype was chosen");
        } else {
            throw new ArchetypeNotDefined("The archetype is not defined");
        }
    }
    if (request.getArchetypeRepository() != null) {
        archetypeRepository = createRepository(request.getArchetypeRepository(), ad.getArtifactId() + "-repo");
        repositories.add(archetypeRepository);
    }
    if (request.getRemoteArtifactRepositories() != null) {
        repositories.addAll(request.getRemoteArtifactRepositories());
    }
    if (!archetypeArtifactManager.exists(ad.getGroupId(), ad.getArtifactId(), ad.getVersion(), archetypeRepository, localRepository, repositories, request.getProjectBuildingRequest())) {
        throw new UnknownArchetype("The desired archetype does not exist (" + ad.getGroupId() + ":" + ad.getArtifactId() + ":" + ad.getVersion() + ")");
    }
    request.setArchetypeVersion(ad.getVersion());
    ArchetypeConfiguration archetypeConfiguration;
    if (archetypeArtifactManager.isFileSetArchetype(ad.getGroupId(), ad.getArtifactId(), ad.getVersion(), archetypeRepository, localRepository, repositories, request.getProjectBuildingRequest())) {
        org.apache.maven.archetype.metadata.ArchetypeDescriptor archetypeDescriptor = archetypeArtifactManager.getFileSetArchetypeDescriptor(ad.getGroupId(), ad.getArtifactId(), ad.getVersion(), archetypeRepository, localRepository, repositories, request.getProjectBuildingRequest());
        archetypeConfiguration = archetypeFactory.createArchetypeConfiguration(archetypeDescriptor, properties);
    } else if (archetypeArtifactManager.isOldArchetype(ad.getGroupId(), ad.getArtifactId(), ad.getVersion(), archetypeRepository, localRepository, repositories, request.getProjectBuildingRequest())) {
        org.apache.maven.archetype.old.descriptor.ArchetypeDescriptor archetypeDescriptor = archetypeArtifactManager.getOldArchetypeDescriptor(ad.getGroupId(), ad.getArtifactId(), ad.getVersion(), archetypeRepository, localRepository, repositories, request.getProjectBuildingRequest());
        archetypeConfiguration = archetypeFactory.createArchetypeConfiguration(archetypeDescriptor, properties);
    } else {
        throw new ArchetypeGenerationConfigurationFailure("The defined artifact is not an archetype");
    }
    Context context = new VelocityContext();
    if (interactiveMode.booleanValue()) {
        boolean confirmed = false;
        context.put(Constants.GROUP_ID, ad.getGroupId());
        context.put(Constants.ARTIFACT_ID, ad.getArtifactId());
        context.put(Constants.VERSION, ad.getVersion());
        while (!confirmed) {
            List<String> propertiesRequired = archetypeConfiguration.getRequiredProperties();
            getLogger().debug("Required properties before content sort: " + propertiesRequired);
            Collections.sort(propertiesRequired, new RequiredPropertyComparator(archetypeConfiguration));
            getLogger().debug("Required properties after content sort: " + propertiesRequired);
            if (!archetypeConfiguration.isConfigured()) {
                for (String requiredProperty : propertiesRequired) {
                    if (!archetypeConfiguration.isConfigured(requiredProperty)) {
                        if ("package".equals(requiredProperty)) {
                            // if the asked property is 'package', then
                            // use its default and if not defined,
                            // use the 'groupId' property value.
                            String packageDefault = archetypeConfiguration.getDefaultValue(requiredProperty);
                            packageDefault = (null == packageDefault || "".equals(packageDefault)) ? archetypeConfiguration.getProperty("groupId") : archetypeConfiguration.getDefaultValue(requiredProperty);
                            String value = getTransitiveDefaultValue(packageDefault, archetypeConfiguration, requiredProperty, context);
                            value = archetypeGenerationQueryer.getPropertyValue(requiredProperty, value, null);
                            archetypeConfiguration.setProperty(requiredProperty, value);
                            context.put(Constants.PACKAGE, value);
                        } else {
                            String value = archetypeConfiguration.getDefaultValue(requiredProperty);
                            value = getTransitiveDefaultValue(value, archetypeConfiguration, requiredProperty, context);
                            value = archetypeGenerationQueryer.getPropertyValue(requiredProperty, value, archetypeConfiguration.getPropertyValidationRegex(requiredProperty));
                            archetypeConfiguration.setProperty(requiredProperty, value);
                            context.put(requiredProperty, value);
                        }
                    } else {
                        getLogger().info("Using property: " + requiredProperty + " = " + archetypeConfiguration.getProperty(requiredProperty));
                        archetypeConfiguration.setProperty(requiredProperty, archetypeConfiguration.getProperty(requiredProperty));
                    }
                }
            } else {
                for (String requiredProperty : propertiesRequired) {
                    getLogger().info("Using property: " + requiredProperty + " = " + archetypeConfiguration.getProperty(requiredProperty));
                }
            }
            if (!archetypeConfiguration.isConfigured()) {
                getLogger().warn("Archetype is not fully configured");
            } else if (!archetypeGenerationQueryer.confirmConfiguration(archetypeConfiguration)) {
                getLogger().debug("Archetype generation configuration not confirmed");
                archetypeConfiguration.reset();
                restoreCommandLineProperties(archetypeConfiguration, executionProperties);
            } else {
                getLogger().debug("Archetype generation configuration confirmed");
                confirmed = true;
            }
        }
    } else {
        if (!archetypeConfiguration.isConfigured()) {
            for (String requiredProperty : archetypeConfiguration.getRequiredProperties()) {
                if (!archetypeConfiguration.isConfigured(requiredProperty) && (archetypeConfiguration.getDefaultValue(requiredProperty) != null)) {
                    String value = archetypeConfiguration.getDefaultValue(requiredProperty);
                    value = getTransitiveDefaultValue(value, archetypeConfiguration, requiredProperty, context);
                    archetypeConfiguration.setProperty(requiredProperty, value);
                    context.put(requiredProperty, value);
                }
            }
            // in batch mode, we assume the defaults, and if still not configured fail
            if (!archetypeConfiguration.isConfigured()) {
                StringBuffer exceptionMessage = new StringBuffer();
                exceptionMessage.append("Archetype ");
                exceptionMessage.append(request.getArchetypeGroupId());
                exceptionMessage.append(":");
                exceptionMessage.append(request.getArchetypeArtifactId());
                exceptionMessage.append(":");
                exceptionMessage.append(request.getArchetypeVersion());
                exceptionMessage.append(" is not configured");
                List<String> missingProperties = new ArrayList<String>(0);
                for (String requiredProperty : archetypeConfiguration.getRequiredProperties()) {
                    if (!archetypeConfiguration.isConfigured(requiredProperty)) {
                        exceptionMessage.append("\n\tProperty ");
                        exceptionMessage.append(requiredProperty);
                        missingProperties.add(requiredProperty);
                        exceptionMessage.append(" is missing.");
                        getLogger().warn("Property " + requiredProperty + " is missing. Add -D" + requiredProperty + "=someValue");
                    }
                }
                throw new ArchetypeNotConfigured(exceptionMessage.toString(), missingProperties);
            }
        }
    }
    request.setGroupId(archetypeConfiguration.getProperty(Constants.GROUP_ID));
    request.setArtifactId(archetypeConfiguration.getProperty(Constants.ARTIFACT_ID));
    request.setVersion(archetypeConfiguration.getProperty(Constants.VERSION));
    request.setPackage(archetypeConfiguration.getProperty(Constants.PACKAGE));
    properties = archetypeConfiguration.getProperties();
    request.setProperties(properties);
}
Also used : Context(org.apache.velocity.context.Context) VelocityContext(org.apache.velocity.VelocityContext) ArchetypeDefinition(org.apache.maven.archetype.ui.ArchetypeDefinition) ArchetypeConfiguration(org.apache.maven.archetype.ui.ArchetypeConfiguration) VelocityContext(org.apache.velocity.VelocityContext) ArrayList(java.util.ArrayList) ArtifactRepository(org.apache.maven.artifact.repository.ArtifactRepository) MavenArtifactRepository(org.apache.maven.artifact.repository.MavenArtifactRepository) Properties(java.util.Properties) ArchetypeGenerationConfigurationFailure(org.apache.maven.archetype.exception.ArchetypeGenerationConfigurationFailure) ArchetypeNotConfigured(org.apache.maven.archetype.exception.ArchetypeNotConfigured) UnknownArchetype(org.apache.maven.archetype.exception.UnknownArchetype) ArchetypeNotDefined(org.apache.maven.archetype.exception.ArchetypeNotDefined)

Example 4 with ArchetypeDefinition

use of org.apache.maven.archetype.ui.ArchetypeDefinition 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 5 with ArchetypeDefinition

use of org.apache.maven.archetype.ui.ArchetypeDefinition 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

ArchetypeDefinition (org.apache.maven.archetype.ui.ArchetypeDefinition)8 Archetype (org.apache.maven.archetype.catalog.Archetype)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 MockControl (org.easymock.MockControl)4 UnknownArchetype (org.apache.maven.archetype.exception.UnknownArchetype)3 Prompter (org.codehaus.plexus.components.interactivity.Prompter)3 Properties (java.util.Properties)2 ArchetypeNotConfigured (org.apache.maven.archetype.exception.ArchetypeNotConfigured)2 ArchetypeNotDefined (org.apache.maven.archetype.exception.ArchetypeNotDefined)2 ArchetypeConfiguration (org.apache.maven.archetype.ui.ArchetypeConfiguration)2 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 ArchetypeGenerationRequest (org.apache.maven.archetype.ArchetypeGenerationRequest)1 ArchetypeGenerationConfigurationFailure (org.apache.maven.archetype.exception.ArchetypeGenerationConfigurationFailure)1 ArchetypeSelectionFailure (org.apache.maven.archetype.exception.ArchetypeSelectionFailure)1 ArtifactRepository (org.apache.maven.artifact.repository.ArtifactRepository)1 MavenArtifactRepository (org.apache.maven.artifact.repository.MavenArtifactRepository)1 VelocityContext (org.apache.velocity.VelocityContext)1