use of org.apache.maven.archetype.catalog.Archetype in project indy by Commonjava.
the class ArchetypeCatalogMerger method merge.
public byte[] merge(final Collection<Transfer> sources, final Group group, final String path) {
final ArchetypeCatalog master = new ArchetypeCatalog();
final ArchetypeCatalogXpp3Reader reader = new ArchetypeCatalogXpp3Reader();
final FileReader fr = null;
boolean merged = false;
final Set<String> seen = new HashSet<String>();
for (final Transfer src : sources) {
try (InputStream stream = src.openInputStream()) {
final ArchetypeCatalog catalog = reader.read(stream, false);
for (final Archetype arch : catalog.getArchetypes()) {
final String key = arch.getGroupId() + ":" + arch.getArtifactId() + ":" + arch.getVersion();
if (seen.add(key)) {
master.addArchetype(arch);
}
}
merged = true;
} catch (final IOException e) {
final StoreKey key = getKey(src);
logger.error(String.format("Cannot read archetype catalog: %s from artifact-store: %s. Reason: %s", src.getPath(), key, e.getMessage()), e);
} catch (final XmlPullParserException e) {
final StoreKey key = getKey(src);
logger.error(String.format("Cannot parse archetype catalog: %s from artifact-store: %s. Reason: %s", src.getPath(), key, e.getMessage()), e);
} finally {
closeQuietly(fr);
}
}
if (merged) {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
new ArchetypeCatalogXpp3Writer().write(baos, master);
return baos.toByteArray();
} catch (final IOException e) {
logger.error(String.format("Cannot write consolidated archetype catalog: %s to: %s. Reason: %s", path, group.getKey(), e.getMessage()), e);
}
}
return null;
}
use of org.apache.maven.archetype.catalog.Archetype in project maven-archetype by apache.
the class DefaultArchetypeSelectionQueryer method selectArchetype.
public Archetype selectArchetype(Map<String, List<Archetype>> catalogs, ArchetypeDefinition defaultDefinition) throws PrompterException {
Archetype selection = null;
Map<String, List<Archetype>> filteredCatalogs = catalogs;
do {
StringBuilder query = new StringBuilder("Choose archetype:\n");
Set<String> archetypeKeys = new HashSet<String>();
List<String> answers = new ArrayList<String>();
Map<String, Archetype> archetypeAnswerMap = new HashMap<String, Archetype>();
int counter = 0;
int defaultSelection = 0;
for (Map.Entry<String, List<Archetype>> entry : filteredCatalogs.entrySet()) {
String catalog = entry.getKey();
for (Archetype archetype : entry.getValue()) {
String archetypeKey = archetype.getGroupId() + ":" + archetype.getArtifactId();
if (!archetypeKeys.add(archetypeKey)) {
continue;
}
counter++;
String description = archetype.getDescription();
if (description == null) {
description = "-";
}
String answer = String.valueOf(counter);
query.append(answer + ": " + catalog + " -> " + archetype.getGroupId() + ":" + archetype.getArtifactId() + " (" + description + ")\n");
answers.add(answer);
archetypeAnswerMap.put(answer, archetype);
// the version is not tested. This is intentional.
if (defaultDefinition != null && archetype.getGroupId().equals(defaultDefinition.getGroupId()) && archetype.getArtifactId().equals(defaultDefinition.getArtifactId())) {
defaultSelection = counter;
}
}
}
if (counter == 0) {
query.append(" Your filter doesn't match any archetype (hint: enter to return to initial list)\n");
}
query.append("Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): ");
String answer;
if (defaultSelection == 0) {
answer = prompter.prompt(query.toString());
} else {
answer = prompter.prompt(query.toString(), Integer.toString(defaultSelection));
}
if (NumberUtils.isNumber(answer)) {
selection = archetypeAnswerMap.get(answer);
} else {
// not a number so apply filter and ask again
filteredCatalogs = ArchetypeSelectorUtils.getFilteredArchetypesByCatalog(catalogs, answer);
}
} while (selection == null);
return selectVersion(catalogs, selection.getGroupId(), selection.getArtifactId());
}
use of org.apache.maven.archetype.catalog.Archetype 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);
}
use of org.apache.maven.archetype.catalog.Archetype in project maven-archetype by apache.
the class DefaultArchetypeSelector method findArchetype.
private Map.Entry<String, Archetype> findArchetype(Map<String, List<Archetype>> archetypes, String groupId, String artifactId) {
Archetype example = new Archetype();
example.setGroupId(groupId);
example.setArtifactId(artifactId);
for (Map.Entry<String, List<Archetype>> entry : archetypes.entrySet()) {
List<Archetype> catalog = entry.getValue();
if (catalog.contains(example)) {
Archetype archetype = catalog.get(catalog.indexOf(example));
return newMapEntry(entry.getKey(), archetype);
}
}
return null;
}
use of org.apache.maven.archetype.catalog.Archetype 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());
}
Aggregations