Search in sources :

Example 1 with ArchetypeCatalogXpp3Reader

use of org.apache.maven.archetype.catalog.io.xpp3.ArchetypeCatalogXpp3Reader in project indy by Commonjava.

the class ArchetypeCatalogMerger method merge.

@Override
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;
    InputStream stream = null;
    boolean merged = false;
    final Set<String> seen = new HashSet<String>();
    for (final Transfer src : sources) {
        try {
            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;
}
Also used : Archetype(org.apache.maven.archetype.catalog.Archetype) InputStream(java.io.InputStream) ArchetypeCatalogXpp3Writer(org.apache.maven.archetype.catalog.io.xpp3.ArchetypeCatalogXpp3Writer) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) StoreKey(org.commonjava.indy.model.core.StoreKey) ArchetypeCatalogXpp3Reader(org.apache.maven.archetype.catalog.io.xpp3.ArchetypeCatalogXpp3Reader) Transfer(org.commonjava.maven.galley.model.Transfer) FileReader(java.io.FileReader) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) ArchetypeCatalog(org.apache.maven.archetype.catalog.ArchetypeCatalog) HashSet(java.util.HashSet)

Aggregations

ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileReader (java.io.FileReader)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 HashSet (java.util.HashSet)1 Archetype (org.apache.maven.archetype.catalog.Archetype)1 ArchetypeCatalog (org.apache.maven.archetype.catalog.ArchetypeCatalog)1 ArchetypeCatalogXpp3Reader (org.apache.maven.archetype.catalog.io.xpp3.ArchetypeCatalogXpp3Reader)1 ArchetypeCatalogXpp3Writer (org.apache.maven.archetype.catalog.io.xpp3.ArchetypeCatalogXpp3Writer)1 XmlPullParserException (org.codehaus.plexus.util.xml.pull.XmlPullParserException)1 StoreKey (org.commonjava.indy.model.core.StoreKey)1 Transfer (org.commonjava.maven.galley.model.Transfer)1