Search in sources :

Example 16 with XmlPullParserException

use of org.codehaus.plexus.util.xml.pull.XmlPullParserException 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)

Example 17 with XmlPullParserException

use of org.codehaus.plexus.util.xml.pull.XmlPullParserException in project indy by Commonjava.

the class MavenMetadataMerger method merge.

@Override
public byte[] merge(final Collection<Transfer> sources, final Group group, final String path) {
    Logger logger = LoggerFactory.getLogger(getClass());
    logger.debug("Generating merged metadata in: {}:{}", group.getKey(), path);
    final Metadata master = new Metadata();
    master.setVersioning(new Versioning());
    final MetadataXpp3Reader reader = new MetadataXpp3Reader();
    final FileReader fr = null;
    InputStream stream = null;
    boolean merged = false;
    Transfer snapshotProvider = null;
    for (final Transfer src : sources) {
        if (!src.exists()) {
            continue;
        }
        try {
            stream = src.openInputStream();
            String content = IOUtils.toString(stream);
            logger.debug("Adding in metadata content from: {}\n\n{}\n\n", src, content);
            // there is a lot of junk in here to make up for Metadata's anemic merge() method.
            final Metadata md = reader.read(new StringReader(content), false);
            if (md.getGroupId() != null) {
                master.setGroupId(md.getGroupId());
            }
            if (md.getArtifactId() != null) {
                master.setArtifactId(md.getArtifactId());
            }
            if (md.getVersion() != null) {
                master.setVersion(md.getVersion());
            }
            master.merge(md);
            Versioning versioning = master.getVersioning();
            Versioning mdVersioning = md.getVersioning();
            // FIXME: Should we try to merge snapshot lists instead of using the first one we encounter??
            if (versioning.getSnapshot() == null && mdVersioning != null) {
                logger.info("INCLUDING snapshot information from: {} in: {}:{}", src, group.getKey(), path);
                snapshotProvider = src;
                versioning.setSnapshot(mdVersioning.getSnapshot());
                final List<SnapshotVersion> snapshotVersions = versioning.getSnapshotVersions();
                boolean added = false;
                for (final SnapshotVersion snap : mdVersioning.getSnapshotVersions()) {
                    if (!snapshotVersions.contains(snap)) {
                        snapshotVersions.add(snap);
                        added = true;
                    }
                }
                if (added) {
                    Collections.sort(snapshotVersions, new SnapshotVersionComparator());
                }
            } else {
                logger.warn("SKIPPING snapshot information from: {} in: {}:{} (obscured by: {})", src, group.getKey(), path, snapshotProvider);
            }
            merged = true;
        } catch (final IOException e) {
            final StoreKey key = getKey(src);
            logger.error(String.format("Cannot read metadata: %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 metadata: %s from artifact-store: %s. Reason: %s", src.getPath(), key, e.getMessage()), e);
        } finally {
            closeQuietly(fr);
            closeQuietly(stream);
        }
    }
    Versioning versioning = master.getVersioning();
    if (versioning != null && versioning.getVersions() != null) {
        if (metadataProviders != null) {
            for (MavenMetadataProvider provider : metadataProviders) {
                try {
                    Metadata toMerge = provider.getMetadata(group.getKey(), path);
                    if (toMerge != null) {
                        merged = master.merge(toMerge) || merged;
                    }
                } catch (IndyWorkflowException e) {
                    logger.error(String.format("Cannot read metadata: %s from metadata provider: %s. Reason: %s", path, provider.getClass().getSimpleName(), e.getMessage()), e);
                }
            }
        }
        List<SingleVersion> versionObjects = versioning.getVersions().stream().map(VersionUtils::createSingleVersion).collect(Collectors.toList());
        Collections.sort(versionObjects);
        versioning.setVersions(versionObjects.stream().map(SingleVersion::renderStandard).collect(Collectors.toList()));
        if (versionObjects.size() > 0) {
            String latest = versionObjects.get(versionObjects.size() - 1).renderStandard();
            versioning.setLatest(latest);
            versioning.setRelease(latest);
        }
    }
    if (merged) {
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            new MetadataXpp3Writer().write(baos, master);
            return baos.toByteArray();
        } catch (final IOException e) {
            logger.error(String.format("Cannot write consolidated metadata: %s to: %s. Reason: %s", path, group.getKey(), e.getMessage()), e);
        }
    }
    return null;
}
Also used : MetadataXpp3Reader(org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader) InputStream(java.io.InputStream) Metadata(org.apache.maven.artifact.repository.metadata.Metadata) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Logger(org.slf4j.Logger) StoreKey(org.commonjava.indy.model.core.StoreKey) Versioning(org.apache.maven.artifact.repository.metadata.Versioning) MetadataXpp3Writer(org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Writer) SnapshotVersion(org.apache.maven.artifact.repository.metadata.SnapshotVersion) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException) Transfer(org.commonjava.maven.galley.model.Transfer) StringReader(java.io.StringReader) FileReader(java.io.FileReader) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) SingleVersion(org.commonjava.maven.atlas.ident.version.SingleVersion)

Example 18 with XmlPullParserException

use of org.codehaus.plexus.util.xml.pull.XmlPullParserException in project asterixdb by apache.

the class SupplementalModelHelper method getSupplement.

protected static Model getSupplement(Log log, Xpp3Dom supplementModelXml) throws MojoExecutionException {
    MavenXpp3Reader modelReader = new MavenXpp3Reader();
    Model model = null;
    try {
        model = modelReader.read(new StringReader(supplementModelXml.toString()));
        String groupId = model.getGroupId();
        String artifactId = model.getArtifactId();
        if (groupId == null || "".equals(groupId.trim())) {
            throw new MojoExecutionException("Supplemental project XML requires that a <groupId> element be present.");
        }
        if (artifactId == null || "".equals(artifactId.trim())) {
            throw new MojoExecutionException("Supplemental project XML requires that a <artifactId> element be present.");
        }
    } catch (IOException e) {
        log.warn("Unable to read supplemental XML: " + e.getMessage(), e);
    } catch (XmlPullParserException e) {
        log.warn("Unable to parse supplemental XML: " + e.getMessage(), e);
    }
    return model;
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) SupplementalDataModel(org.apache.maven.plugin.resources.remote.SupplementalDataModel) Model(org.apache.maven.model.Model) StringReader(java.io.StringReader) MavenXpp3Reader(org.apache.maven.model.io.xpp3.MavenXpp3Reader) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) IOException(java.io.IOException)

Example 19 with XmlPullParserException

use of org.codehaus.plexus.util.xml.pull.XmlPullParserException in project maven-plugins by apache.

the class ResourceResolver method resolveBundleFromProject.

private static List<JavadocBundle> resolveBundleFromProject(SourceResolverConfig config, MavenProject project, Artifact artifact) throws IOException {
    List<JavadocBundle> bundles = new ArrayList<JavadocBundle>();
    List<String> classifiers = new ArrayList<String>();
    if (config.includeCompileSources()) {
        classifiers.add(AbstractJavadocMojo.JAVADOC_RESOURCES_ATTACHMENT_CLASSIFIER);
    }
    if (config.includeTestSources()) {
        classifiers.add(AbstractJavadocMojo.TEST_JAVADOC_RESOURCES_ATTACHMENT_CLASSIFIER);
    }
    for (String classifier : classifiers) {
        File optionsFile = new File(project.getBuild().getDirectory(), "javadoc-bundle-options/javadoc-options-" + classifier + ".xml");
        if (!optionsFile.exists()) {
            continue;
        }
        FileInputStream stream = null;
        try {
            stream = new FileInputStream(optionsFile);
            JavadocOptions options = new JavadocOptionsXpp3Reader().read(stream);
            stream.close();
            stream = null;
            bundles.add(new JavadocBundle(options, new File(project.getBasedir(), options.getJavadocResourcesDirectory())));
        } catch (XmlPullParserException e) {
            IOException error = new IOException("Failed to read javadoc options from: " + optionsFile + "\nReason: " + e.getMessage());
            error.initCause(e);
            throw error;
        } finally {
            close(stream);
        }
    }
    return bundles;
}
Also used : JavadocOptionsXpp3Reader(org.apache.maven.plugin.javadoc.options.io.xpp3.JavadocOptionsXpp3Reader) JavadocOptions(org.apache.maven.plugin.javadoc.options.JavadocOptions) ArrayList(java.util.ArrayList) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 20 with XmlPullParserException

use of org.codehaus.plexus.util.xml.pull.XmlPullParserException in project maven-plugins by apache.

the class InstallFileMojo method readModel.

/**
     * Parses a POM.
     * 
     * @param pomFile The path of the POM file to parse, must not be <code>null</code>.
     * @return The model from the POM file, never <code>null</code>.
     * @throws MojoExecutionException If the POM could not be parsed.
     */
private Model readModel(File pomFile) throws MojoExecutionException {
    Reader reader = null;
    try {
        reader = ReaderFactory.newXmlReader(pomFile);
        final Model model = new MavenXpp3Reader().read(reader);
        reader.close();
        reader = null;
        return model;
    } catch (FileNotFoundException e) {
        throw new MojoExecutionException("File not found " + pomFile, e);
    } catch (IOException e) {
        throw new MojoExecutionException("Error reading POM " + pomFile, e);
    } catch (XmlPullParserException e) {
        throw new MojoExecutionException("Error parsing POM " + pomFile, e);
    } finally {
        IOUtil.close(reader);
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Model(org.apache.maven.model.Model) FileNotFoundException(java.io.FileNotFoundException) Reader(java.io.Reader) MavenXpp3Reader(org.apache.maven.model.io.xpp3.MavenXpp3Reader) MavenXpp3Reader(org.apache.maven.model.io.xpp3.MavenXpp3Reader) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)28 XmlPullParserException (org.codehaus.plexus.util.xml.pull.XmlPullParserException)28 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)13 Reader (java.io.Reader)11 File (java.io.File)10 Model (org.apache.maven.model.Model)8 MavenXpp3Reader (org.apache.maven.model.io.xpp3.MavenXpp3Reader)8 StringReader (java.io.StringReader)7 FileInputStream (java.io.FileInputStream)4 FileNotFoundException (java.io.FileNotFoundException)4 InputStream (java.io.InputStream)4 ArrayList (java.util.ArrayList)4 MojoFailureException (org.apache.maven.plugin.MojoFailureException)4 FileReader (java.io.FileReader)3 FileWriter (java.io.FileWriter)3 InputStreamReader (java.io.InputStreamReader)3 BufferedReader (java.io.BufferedReader)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 FileOutputStream (java.io.FileOutputStream)2 OutputStream (java.io.OutputStream)2