Search in sources :

Example 31 with XmlPullParserException

use of org.codehaus.plexus.util.xml.pull.XmlPullParserException in project CzechIdMng by bcvsolutions.

the class MavenManager method getModel.

/**
 * Resolve maven model from given library. Pom or jar library is supported only.
 * Jar library has to contain standard maven pom.xml descriptor.
 *
 * @param extractedLibrary extracted library (~module) folder
 * @return maven model
 * @since 10.7.0
 */
public Model getModel(File extractedLibrary) {
    Assert.notNull(extractedLibrary, "Library (jar / pom) is required.");
    File pomFile = null;
    // pom / jar library is supported
    if (extractedLibrary.getName().equals(MAVEN_PROJECT_FILE_NAME)) {
        pomFile = extractedLibrary;
    } else {
        File moduleMavenResources = new File(String.format("%s/META-INF/maven", extractedLibrary.getPath()));
        if (!moduleMavenResources.exists()) {
            LOG.info("Library [{}] cannot be resolved as maven dependency for backend build, " + "required maven descriptor [{}] not found.", extractedLibrary.getName(), MAVEN_PROJECT_FILE_NAME);
            return null;
        }
        // 
        for (File file : FileUtils.listFiles(moduleMavenResources, null, true)) {
            if (file.getName().equals(MAVEN_PROJECT_FILE_NAME)) {
                // find the first file, more projects are not supported now
                pomFile = file;
                break;
            }
        }
    }
    // pom file not resolved
    if (pomFile == null) {
        LOG.info("Library [{}] cannot be resolved as maven dependency for backend build, " + "required maven descriptor [{}] not found.", extractedLibrary.getName(), MAVEN_PROJECT_FILE_NAME);
        return null;
    }
    // read model from pom file
    try (InputStream is = new FileInputStream(pomFile)) {
        Model model = mavenReader.read(is);
        model.setPomFile(pomFile);
        return model;
    } catch (IOException | XmlPullParserException ex) {
        LOG.warn("Library [{}] cannot be resolved as maven dependency for backend build, " + "required maven descriptor [{}] is not readable.", extractedLibrary.getName(), MAVEN_PROJECT_FILE_NAME, ex);
        return null;
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Model(org.apache.maven.model.Model) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 32 with XmlPullParserException

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

the class ValidatingMavenPublisher method parsePomFileIntoMavenModel.

private Model parsePomFileIntoMavenModel(MavenNormalizedPublication publication) {
    File pomFile = publication.getPomArtifact().getFile();
    try {
        Model model = readModelFromPom(pomFile);
        model.setPomFile(pomFile);
        return model;
    } catch (XmlPullParserException parseException) {
        throw new InvalidMavenPublicationException(publication.getName(), "POM file is invalid. Check any modifications you have made to the POM file.", parseException);
    } catch (IOException ex) {
        throw new UncheckedIOException(ex);
    }
}
Also used : InvalidMavenPublicationException(org.gradle.api.publish.maven.InvalidMavenPublicationException) Model(org.apache.maven.model.Model) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) UncheckedIOException(org.gradle.api.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(org.gradle.api.UncheckedIOException) File(java.io.File)

Example 33 with XmlPullParserException

use of org.codehaus.plexus.util.xml.pull.XmlPullParserException in project kie-wb-common by kiegroup.

the class PomEditorServiceImpl method checkRepositories.

private void checkRepositories(final Path pomPath, final String pomXml) {
    // Check is the POM's GAV has been changed.
    final KieModule module = moduleService.resolveModule(pomPath);
    POM pom = new POM(GAV_UNDETERMINED);
    try {
        pom = pomContentHandler.toModel(pomXml);
        if (pom.getGav().equals(module.getPom().getGav())) {
            return;
        }
        // If GAV is snapshot we can freely override the module
        if (pom.getGav().isSnapshot()) {
            return;
        }
    } catch (final XmlPullParserException e) {
        throw new InvalidPomException(e.getLineNumber(), e.getColumnNumber());
    } catch (final IOException e) {
        logger.warn("Unable to load pom.xml. It is therefore impossible to ascertain GAV.", e);
    }
    // Check is the POM's GAV resolves to any pre-existing artifacts.
    // Filter resolved Repositories by those enabled for the Module.
    final ModuleRepositories moduleRepositories = moduleRepositoriesService.load(module.getRepositoriesPath());
    final Set<MavenRepositoryMetadata> repositories = repositoryResolver.getRepositoriesResolvingArtifact(pomXml, moduleRepositories.filterByIncluded());
    if (repositories.size() > 0) {
        throw new GAVAlreadyExistsException(pom.getGav(), repositories);
    }
}
Also used : ModuleRepositories(org.guvnor.common.services.project.model.ModuleRepositories) MavenRepositoryMetadata(org.guvnor.common.services.project.model.MavenRepositoryMetadata) GAVAlreadyExistsException(org.guvnor.common.services.project.service.GAVAlreadyExistsException) InvalidPomException(org.kie.workbench.common.screens.projecteditor.model.InvalidPomException) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) IOException(java.io.IOException) KieModule(org.kie.workbench.common.services.shared.project.KieModule) POM(org.guvnor.common.services.project.model.POM)

Example 34 with XmlPullParserException

use of org.codehaus.plexus.util.xml.pull.XmlPullParserException in project intellij-community by JetBrains.

the class MavenEmbedder method buildSettings.

public static Settings buildSettings(PlexusContainer container, MavenEmbedderSettings embedderSettings) {
    File file = embedderSettings.getGlobalSettingsFile();
    if (file != null) {
        System.setProperty(MavenSettingsBuilder.ALT_GLOBAL_SETTINGS_XML_LOCATION, file.getPath());
    }
    Settings settings = null;
    try {
        MavenSettingsBuilder builder = (MavenSettingsBuilder) container.lookup(MavenSettingsBuilder.ROLE);
        File userSettingsFile = embedderSettings.getUserSettingsFile();
        if (userSettingsFile != null && userSettingsFile.exists() && !userSettingsFile.isDirectory()) {
            settings = builder.buildSettings(userSettingsFile, false);
        }
        if (settings == null) {
            settings = builder.buildSettings();
        }
    } catch (ComponentLookupException e) {
        MavenEmbedderLog.LOG.error(e);
    } catch (IOException e) {
        MavenEmbedderLog.LOG.warn(e);
    } catch (XmlPullParserException e) {
        MavenEmbedderLog.LOG.warn(e);
    }
    if (settings == null) {
        settings = new Settings();
    }
    if (embedderSettings.getLocalRepository() != null) {
        settings.setLocalRepository(embedderSettings.getLocalRepository().getPath());
    }
    if (settings.getLocalRepository() == null) {
        settings.setLocalRepository(System.getProperty("user.home") + "/.m2/repository");
    }
    settings.setOffline(embedderSettings.isWorkOffline());
    settings.setInteractiveMode(false);
    settings.setUsePluginRegistry(embedderSettings.isUsePluginRegistry());
    RuntimeInfo runtimeInfo = new RuntimeInfo(settings);
    runtimeInfo.setPluginUpdateOverride(embedderSettings.getPluginUpdatePolicy() == MavenEmbedderSettings.UpdatePolicy.ALWAYS_UPDATE);
    settings.setRuntimeInfo(runtimeInfo);
    return settings;
}
Also used : ComponentLookupException(org.codehaus.plexus.component.repository.exception.ComponentLookupException) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) IOException(java.io.IOException) File(java.io.File)

Example 35 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)

Aggregations

XmlPullParserException (org.codehaus.plexus.util.xml.pull.XmlPullParserException)79 IOException (java.io.IOException)73 File (java.io.File)37 Model (org.apache.maven.model.Model)32 MavenXpp3Reader (org.apache.maven.model.io.xpp3.MavenXpp3Reader)30 FileNotFoundException (java.io.FileNotFoundException)20 Reader (java.io.Reader)20 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)19 FileReader (java.io.FileReader)15 FileInputStream (java.io.FileInputStream)12 ArrayList (java.util.ArrayList)12 InputStream (java.io.InputStream)11 StringReader (java.io.StringReader)10 ArtifactResolutionException (org.eclipse.aether.resolution.ArtifactResolutionException)10 Path (java.nio.file.Path)7 Artifact (org.eclipse.aether.artifact.Artifact)7 DefaultArtifact (org.eclipse.aether.artifact.DefaultArtifact)7 PluginException (org.bimserver.shared.exceptions.PluginException)6 ArtifactDescriptorException (org.eclipse.aether.resolution.ArtifactDescriptorException)6 HashMap (java.util.HashMap)5