Search in sources :

Example 16 with ArchetypeCatalog

use of org.apache.maven.archetype.catalog.ArchetypeCatalog in project maven-archetype by apache.

the class LocalCatalogArchetypeDataSource method getArchetypeCatalog.

public ArchetypeCatalog getArchetypeCatalog(ProjectBuildingRequest buildingRequest) throws ArchetypeDataSourceException {
    File localRepo = repositoryManager.getLocalRepositoryBasedir(buildingRequest);
    File catalogFile = new File(localRepo, ARCHETYPE_CATALOG_FILENAME);
    if (catalogFile.exists() && catalogFile.isDirectory()) {
        catalogFile = new File(catalogFile, ARCHETYPE_CATALOG_FILENAME);
    }
    getLogger().debug("Using catalog " + catalogFile);
    if (catalogFile.exists()) {
        try {
            return readCatalog(ReaderFactory.newXmlReader(catalogFile));
        } catch (FileNotFoundException e) {
            throw new ArchetypeDataSourceException("The specific archetype catalog does not exist.", e);
        } catch (IOException e) {
            throw new ArchetypeDataSourceException("Error reading archetype catalog.", e);
        }
    } else {
        return new ArchetypeCatalog();
    }
}
Also used : FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) ArchetypeCatalog(org.apache.maven.archetype.catalog.ArchetypeCatalog) File(java.io.File)

Example 17 with ArchetypeCatalog

use of org.apache.maven.archetype.catalog.ArchetypeCatalog 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;
}
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

ArchetypeCatalog (org.apache.maven.archetype.catalog.ArchetypeCatalog)17 File (java.io.File)12 Archetype (org.apache.maven.archetype.catalog.Archetype)12 ArchetypeManager (org.apache.maven.archetype.ArchetypeManager)7 DefaultProjectBuildingRequest (org.apache.maven.project.DefaultProjectBuildingRequest)7 ProjectBuildingRequest (org.apache.maven.project.ProjectBuildingRequest)7 ArchetypeCatalogXpp3Writer (org.apache.maven.archetype.catalog.io.xpp3.ArchetypeCatalogXpp3Writer)6 MavenArtifactRepository (org.apache.maven.artifact.repository.MavenArtifactRepository)6 MavenRepositorySystemSession (org.apache.maven.repository.internal.MavenRepositorySystemSession)6 FileWriter (java.io.FileWriter)5 Writer (java.io.Writer)5 ArchetypeGenerationRequest (org.apache.maven.archetype.ArchetypeGenerationRequest)5 ArchetypeGenerationResult (org.apache.maven.archetype.ArchetypeGenerationResult)5 ArtifactRepository (org.apache.maven.artifact.repository.ArtifactRepository)5 SimpleLocalRepositoryManager (org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager)5 IOException (java.io.IOException)4 Properties (java.util.Properties)4 Model (org.apache.maven.model.Model)4 ArchetypeCreationRequest (org.apache.maven.archetype.ArchetypeCreationRequest)3 ArchetypeCreationResult (org.apache.maven.archetype.ArchetypeCreationResult)3