use of org.apache.maven.archetype.exception.ArchetypeNotConfigured 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();
}
use of org.apache.maven.archetype.exception.ArchetypeNotConfigured in project maven-archetype by apache.
the class DefaultFilesetArchetypeGenerator method generateArchetype.
public void generateArchetype(ArchetypeGenerationRequest request, File archetypeFile) throws UnknownArchetype, ArchetypeNotConfigured, ProjectDirectoryExists, PomFileExists, OutputFileExists, ArchetypeGenerationFailure {
ClassLoader old = Thread.currentThread().getContextClassLoader();
try {
ArchetypeDescriptor archetypeDescriptor = archetypeArtifactManager.getFileSetArchetypeDescriptor(archetypeFile);
if (!isArchetypeConfigured(archetypeDescriptor, request)) {
if (request.isInteractiveMode()) {
throw new ArchetypeNotConfigured("No archetype was chosen.", null);
}
StringBuffer exceptionMessage = new StringBuffer("Archetype " + request.getArchetypeGroupId() + ":" + request.getArchetypeArtifactId() + ":" + request.getArchetypeVersion() + " is not configured");
List<String> missingProperties = new ArrayList<String>(0);
for (RequiredProperty requiredProperty : archetypeDescriptor.getRequiredProperties()) {
if (StringUtils.isEmpty(request.getProperties().getProperty(requiredProperty.getKey()))) {
exceptionMessage.append("\n\tProperty " + requiredProperty.getKey() + " is missing.");
missingProperties.add(requiredProperty.getKey());
}
}
throw new ArchetypeNotConfigured(exceptionMessage.toString(), missingProperties);
}
Context context = prepareVelocityContext(request);
String packageName = request.getPackage();
String artifactId = request.getArtifactId();
File outputDirectoryFile = new File(request.getOutputDirectory(), artifactId);
File basedirPom = new File(request.getOutputDirectory(), Constants.ARCHETYPE_POM);
File pom = new File(outputDirectoryFile, Constants.ARCHETYPE_POM);
List<String> archetypeResources = archetypeArtifactManager.getFilesetArchetypeResources(archetypeFile);
ZipFile archetypeZipFile = archetypeArtifactManager.getArchetypeZipFile(archetypeFile);
ClassLoader archetypeJarLoader = archetypeArtifactManager.getArchetypeJarLoader(archetypeFile);
Thread.currentThread().setContextClassLoader(archetypeJarLoader);
if (archetypeDescriptor.isPartial()) {
getLogger().debug("Processing partial archetype " + archetypeDescriptor.getName());
if (outputDirectoryFile.exists()) {
if (!pom.exists()) {
throw new PomFileExists("This is a partial archetype and the pom.xml file doesn't exist.");
}
processPomWithMerge(context, pom, "");
processArchetypeTemplatesWithWarning(archetypeDescriptor, archetypeResources, archetypeZipFile, "", context, packageName, outputDirectoryFile);
} else {
if (basedirPom.exists()) {
processPomWithMerge(context, basedirPom, "");
processArchetypeTemplatesWithWarning(archetypeDescriptor, archetypeResources, archetypeZipFile, "", context, packageName, new File(request.getOutputDirectory()));
} else {
processPom(context, pom, "");
processArchetypeTemplates(archetypeDescriptor, archetypeResources, archetypeZipFile, "", context, packageName, outputDirectoryFile);
}
}
if (archetypeDescriptor.getModules().size() > 0) {
getLogger().info("Modules ignored in partial mode");
}
} else {
getLogger().debug("Processing complete archetype " + archetypeDescriptor.getName());
if (outputDirectoryFile.exists() && pom.exists()) {
throw new ProjectDirectoryExists("A Maven 2 project already exists in the directory " + outputDirectoryFile.getPath());
}
if (outputDirectoryFile.exists()) {
getLogger().warn("The directory " + outputDirectoryFile.getPath() + " already exists.");
}
context.put("rootArtifactId", artifactId);
processFilesetModule(artifactId, artifactId, archetypeResources, pom, archetypeZipFile, "", basedirPom, outputDirectoryFile, packageName, archetypeDescriptor, context);
}
String postGenerationScript = archetypeArtifactManager.getPostGenerationScript(archetypeFile);
if (postGenerationScript != null) {
getLogger().info("Executing " + Constants.ARCHETYPE_POST_GENERATION_SCRIPT + " post-generation script");
Binding binding = new Binding();
final Properties archetypeGeneratorProperties = new Properties();
archetypeGeneratorProperties.putAll(System.getProperties());
if (request.getProperties() != null) {
archetypeGeneratorProperties.putAll(request.getProperties());
}
for (Map.Entry<Object, Object> entry : archetypeGeneratorProperties.entrySet()) {
binding.setVariable(entry.getKey().toString(), entry.getValue());
}
binding.setVariable("request", request);
GroovyShell shell = new GroovyShell(binding);
shell.evaluate(postGenerationScript);
}
// ----------------------------------------------------------------------
if (getLogger().isInfoEnabled()) {
getLogger().info("Project created from Archetype in dir: " + outputDirectoryFile.getAbsolutePath());
}
} catch (FileNotFoundException ex) {
throw new ArchetypeGenerationFailure(ex);
} catch (IOException ex) {
throw new ArchetypeGenerationFailure(ex);
} catch (XmlPullParserException ex) {
throw new ArchetypeGenerationFailure(ex);
} catch (DocumentException ex) {
throw new ArchetypeGenerationFailure(ex);
} catch (ArchetypeGenerationFailure ex) {
throw new ArchetypeGenerationFailure(ex);
} catch (InvalidPackaging ex) {
throw new ArchetypeGenerationFailure(ex);
} finally {
Thread.currentThread().setContextClassLoader(old);
}
}
use of org.apache.maven.archetype.exception.ArchetypeNotConfigured 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);
}
use of org.apache.maven.archetype.exception.ArchetypeNotConfigured in project maven-archetype by apache.
the class DefaultArchetypeGenerationConfiguratorTest method testOldArchetypeGeneratedFieldsDefaultsMissingGroupId.
// TODO: should test this in interactive mode to check for prompting
public void testOldArchetypeGeneratedFieldsDefaultsMissingGroupId() throws PrompterException, IOException, UnknownGroup, ArchetypeSelectionFailure, UnknownArchetype, ArchetypeNotDefined, ArchetypeGenerationConfigurationFailure, ArchetypeNotConfigured {
ArchetypeGenerationRequest request = new ArchetypeGenerationRequest();
request.setArchetypeGroupId("archetypeGroupId");
request.setArchetypeArtifactId("archetypeArtifactId");
request.setArchetypeVersion("archetypeVersion");
Properties properties = new Properties();
properties.setProperty("artifactId", "preset-artifactId");
try {
configurator.configureArchetype(request, Boolean.FALSE, properties);
fail("An exception must be thrown");
} catch (ArchetypeNotConfigured e) {
}
}
use of org.apache.maven.archetype.exception.ArchetypeNotConfigured in project maven-archetype by apache.
the class IntegrationTestMojo method generate.
private ArchetypeGenerationRequest generate(String archetypeGroupId, String archetypeArtifactId, String archetypeVersion, File archetypeFile, Properties properties, String basedir) throws IntegrationTestFailure {
// @formatter:off
ArchetypeGenerationRequest request = new ArchetypeGenerationRequest().setArchetypeGroupId(archetypeGroupId).setArchetypeArtifactId(archetypeArtifactId).setArchetypeVersion(archetypeVersion).setGroupId(properties.getProperty(Constants.GROUP_ID)).setArtifactId(properties.getProperty(Constants.ARTIFACT_ID)).setVersion(properties.getProperty(Constants.VERSION)).setPackage(properties.getProperty(Constants.PACKAGE)).setOutputDirectory(basedir).setProperties(properties);
// @formatter:on
ArchetypeGenerationResult result = new ArchetypeGenerationResult();
archetypeGenerator.generateArchetype(request, archetypeFile, result);
if (result.getCause() != null) {
if (result.getCause() instanceof ArchetypeNotConfigured) {
ArchetypeNotConfigured anc = (ArchetypeNotConfigured) result.getCause();
throw new IntegrationTestFailure("Missing required properties in archetype.properties: " + StringUtils.join(anc.getMissingProperties().iterator(), ", "), anc);
}
throw new IntegrationTestFailure(result.getCause().getMessage(), result.getCause());
}
return request;
}
Aggregations