Search in sources :

Example 41 with ManipulationException

use of org.commonjava.maven.ext.common.ManipulationException in project pom-manipulation-ext by release-engineering.

the class VersionCalculator method getMetadataVersions.

/**
 * Accumulate all available versions for a given GAV from all available repositories.
 * @param groupId the groupId to search for
 * @param artifactId the artifactId to search for
 * @return Collection of versions for the specified group:artifact
 * @throws ManipulationException if an error occurs.
 */
private Set<String> getMetadataVersions(final String groupId, final String artifactId) throws ManipulationException {
    logger.debug("Reading available versions from repository metadata for: " + groupId + ":" + artifactId);
    try {
        final MavenMetadataView metadataView = readerWrapper.readMetadataView(new SimpleProjectRef(groupId, artifactId));
        final List<String> versions = metadataView.resolveXPathToAggregatedStringList("/metadata/versioning/versions/version", true, -1);
        return new HashSet<>(versions);
    } catch (final GalleyMavenException e) {
        throw new ManipulationException("Failed to resolve metadata for: %s:%s.", e, groupId, artifactId);
    }
}
Also used : MavenMetadataView(org.commonjava.maven.galley.maven.model.view.meta.MavenMetadataView) SimpleProjectRef(org.commonjava.maven.atlas.ident.ref.SimpleProjectRef) GalleyMavenException(org.commonjava.maven.galley.maven.GalleyMavenException) ManipulationException(org.commonjava.maven.ext.common.ManipulationException) HashSet(java.util.HashSet)

Example 42 with ManipulationException

use of org.commonjava.maven.ext.common.ManipulationException in project pom-manipulation-ext by release-engineering.

the class XMLManipulator method internalApplyChanges.

void internalApplyChanges(Project project, XMLState.XMLOperation operation) throws ManipulationException {
    File target = new File(project.getPom().getParentFile(), operation.getFile());
    logger.info("Attempting to start XML update to file {} with xpath {} and replacement {}", target, operation.getXPath(), operation.getUpdate());
    Document doc = xmlIO.parseXML(target);
    try {
        NodeList nodeList = (NodeList) xPath.evaluate(operation.getXPath(), doc, XPathConstants.NODESET);
        if (nodeList.getLength() == 0) {
            if (project.isIncrementalPME()) {
                logger.warn("Did not locate XML using XPath " + operation.getXPath());
                return;
            } else {
                logger.error("XPath {} did not find any expressions within {} ", operation.getXPath(), operation.getFile());
                throw new ManipulationException("Did not locate XML using XPath " + operation.getXPath());
            }
        }
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            if (isEmpty(operation.getUpdate())) {
                // Delete
                node.getParentNode().removeChild(node);
            } else {
                // Update
                node.setTextContent(operation.getUpdate());
            }
        }
        xmlIO.writeXML(target, doc);
    } catch (XPathExpressionException e) {
        logger.error("Caught XML exception processing file {}, document context {} ", target, doc, e);
        throw new ManipulationException("Caught XML exception processing file", e);
    }
}
Also used : XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ManipulationException(org.commonjava.maven.ext.common.ManipulationException) Document(org.w3c.dom.Document) File(java.io.File)

Aggregations

ManipulationException (org.commonjava.maven.ext.common.ManipulationException)42 IOException (java.io.IOException)13 File (java.io.File)11 HashMap (java.util.HashMap)7 HashSet (java.util.HashSet)7 LinkedHashMap (java.util.LinkedHashMap)7 Project (org.commonjava.maven.ext.common.model.Project)7 ArrayList (java.util.ArrayList)6 Map (java.util.Map)6 ArtifactRef (org.commonjava.maven.atlas.ident.ref.ArtifactRef)6 Properties (java.util.Properties)5 SimpleArtifactRef (org.commonjava.maven.atlas.ident.ref.SimpleArtifactRef)5 JsonPathException (com.jayway.jsonpath.JsonPathException)4 ProjectVersionRef (org.commonjava.maven.atlas.ident.ref.ProjectVersionRef)4 SimpleProjectRef (org.commonjava.maven.atlas.ident.ref.SimpleProjectRef)4 CommonState (org.commonjava.maven.ext.core.state.CommonState)4 Test (org.junit.Test)4 DocumentContext (com.jayway.jsonpath.DocumentContext)3 FileInputStream (java.io.FileInputStream)3 Dependency (org.apache.maven.model.Dependency)3