Search in sources :

Example 6 with ManipulationException

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

the class PomIO method peekAtPomHierarchy.

private List<PomPeek> peekAtPomHierarchy(final File topPom) throws ManipulationException {
    final List<PomPeek> peeked = new ArrayList<>();
    try {
        final LinkedList<File> pendingPoms = new LinkedList<>();
        pendingPoms.add(topPom.getCanonicalFile());
        final String topDir = topPom.getAbsoluteFile().getParentFile().getCanonicalPath();
        final Set<File> seen = new HashSet<>();
        File topLevelParent = topPom;
        while (!pendingPoms.isEmpty()) {
            final File pom = pendingPoms.removeFirst();
            seen.add(pom);
            logger.debug("PEEK: " + pom);
            final PomPeek peek = new PomPeek(pom);
            final ProjectVersionRef key = peek.getKey();
            if (key != null) {
                peeked.add(peek);
                final File dir = pom.getParentFile();
                final String relPath = peek.getParentRelativePath();
                if (relPath != null) {
                    logger.debug("Found parent relativePath: " + relPath + " in pom: " + pom);
                    File parent = new File(dir, relPath);
                    if (parent.isDirectory()) {
                        parent = new File(parent, "pom.xml");
                    }
                    parent = parent.getCanonicalFile();
                    if (parent.getParentFile().getCanonicalPath().startsWith(topDir) && parent.exists() && !seen.contains(parent) && !pendingPoms.contains(parent)) {
                        topLevelParent = parent;
                        logger.debug("Possible top level parent " + parent);
                        pendingPoms.add(parent);
                    } else {
                        logger.debug("Skipping reference to non-existent parent relativePath: '" + relPath + "' in: " + pom);
                    }
                }
                final Set<String> modules = peek.getModules();
                if (modules != null && !modules.isEmpty()) {
                    for (final String module : modules) {
                        logger.debug("Found module: " + module + " in pom: " + pom);
                        File modPom = new File(dir, module);
                        if (modPom.isDirectory()) {
                            modPom = new File(modPom, "pom.xml");
                        }
                        if (modPom.exists() && !seen.contains(modPom) && !pendingPoms.contains(modPom)) {
                            pendingPoms.addLast(modPom);
                        } else {
                            logger.debug("Skipping reference to non-existent module: '" + module + "' in: " + pom);
                        }
                    }
                }
            } else {
                logger.debug("Skipping " + pom + " as its a template file.");
            }
        }
        final HashSet<ProjectVersionRef> projectrefs = new HashSet<>();
        for (final PomPeek p : peeked) {
            projectrefs.add(p.getKey());
            if (p.getPom().equals(topLevelParent)) {
                logger.debug("Setting top level parent to " + p.getPom() + " :: " + p.getKey());
                p.setInheritanceRoot(true);
            }
        }
        for (final PomPeek p : peeked) {
            if (p.getParentKey() == null || !seenThisParent(projectrefs, p.getParentKey())) {
                logger.debug("Found a standalone pom " + p.getPom() + " :: " + p.getKey());
                p.setInheritanceRoot(true);
            }
        }
    } catch (final IOException e) {
        throw new ManipulationException("Problem peeking at POMs.", e);
    }
    return peeked;
}
Also used : ArrayList(java.util.ArrayList) PomPeek(org.commonjava.maven.galley.maven.parse.PomPeek) IOException(java.io.IOException) LinkedList(java.util.LinkedList) ProjectVersionRef(org.commonjava.maven.atlas.ident.ref.ProjectVersionRef) ManipulationException(org.commonjava.maven.ext.common.ManipulationException) File(java.io.File) HashSet(java.util.HashSet)

Example 7 with ManipulationException

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

the class PomIO method determineEOL.

private static LineSeparator determineEOL(File pom) throws ManipulationException {
    try (BufferedInputStream bufferIn = new BufferedInputStream(new FileInputStream(pom))) {
        int prev = -1;
        int ch;
        while ((ch = bufferIn.read()) != -1) {
            if (ch == '\n') {
                if (prev == '\r') {
                    return LineSeparator.CRNL;
                } else {
                    return LineSeparator.NL;
                }
            } else if (prev == '\r') {
                return LineSeparator.CR;
            }
            prev = ch;
        }
        throw new ManipulationException("Could not determine end-of-line marker mode");
    } catch (IOException ioe) {
        throw new ManipulationException("Could not determine end-of-line marker mode", ioe);
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) ManipulationException(org.commonjava.maven.ext.common.ManipulationException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 8 with ManipulationException

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

the class XMLIO method writeXML.

public void writeXML(File target, Document contents) throws ManipulationException {
    try {
        // TODO: https://stackoverflow.com/questions/24551962/adding-linebreak-in-xml-file-before-root-node
        // TODO: Can't get a clean round trip due to newline differences.
        // StreamResult result = new StreamResult( new FileWriter( target));
        // transformer.transform( new DOMSource( contents ), result);
        String result = convert(contents);
        // Adjust for comment before root node and possibly insert a newline.
        result = result.replaceFirst("(?s)(<!--.*-->)<", "$1\n<");
        FileUtils.writeStringToFile(target, result);
    } catch (IOException e) {
        logger.error("XML transformer failure", e);
        throw new ManipulationException("XML transformer failure", e);
    }
}
Also used : ManipulationException(org.commonjava.maven.ext.common.ManipulationException) IOException(java.io.IOException)

Example 9 with ManipulationException

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

the class JSONIOTest method countMatches.

@Test
public void countMatches() throws ManipulationException, IOException {
    String pluginsPath = "$..plugins";
    String reposURLPath = "$.repository.url";
    try {
        DocumentContext doc = jsonIO.parseJSON(pluginFile);
        List o = doc.read(pluginsPath);
        assertTrue(o.size() == 1);
        o = doc.read(reposURLPath);
        assertTrue(o.size() == 1);
    } catch (JsonPathException e) {
        throw new ManipulationException("Caught JsonPath", e);
    }
}
Also used : JsonPathException(com.jayway.jsonpath.JsonPathException) ManipulationException(org.commonjava.maven.ext.common.ManipulationException) List(java.util.List) DocumentContext(com.jayway.jsonpath.DocumentContext) Test(org.junit.Test)

Example 10 with ManipulationException

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

the class ManipulatingLifeCycleParticipant method afterProjectsRead.

@Override
public void afterProjectsRead(final MavenSession mavenSession) throws MavenExecutionException {
    final ManipulationException error = session.getError();
    if (error != null) {
        throw new MavenExecutionException("POM Manipulation failed: " + error.getMessage(), error);
    }
    super.afterProjectsRead(mavenSession);
}
Also used : MavenExecutionException(org.apache.maven.MavenExecutionException) ManipulationException(org.commonjava.maven.ext.common.ManipulationException)

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