use of org.apache.maven.archetype.exception.ArchetypeGenerationConfigurationFailure 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);
}
Aggregations