Search in sources :

Example 26 with ManipulationException

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

the class Project method modelKey.

public static ProjectVersionRef modelKey(final Model model) throws ManipulationException {
    String g = model.getGroupId();
    String v = model.getVersion();
    if (g == null || v == null) {
        final Parent p = model.getParent();
        if (p == null) {
            throw new ManipulationException("Invalid model: " + model + " Cannot find groupId and/or version!");
        }
        if (g == null) {
            g = p.getGroupId();
        }
        if (v == null) {
            v = p.getVersion();
        }
    }
    final String a = model.getArtifactId();
    return new SimpleProjectVersionRef(g, a, v);
}
Also used : Parent(org.apache.maven.model.Parent) ManipulationException(org.commonjava.maven.ext.common.ManipulationException) SimpleProjectVersionRef(org.commonjava.maven.atlas.ident.ref.SimpleProjectVersionRef)

Example 27 with ManipulationException

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

the class ManifestUtils method getManifestInformation.

/**
 * Retrieves the SHA this was built with.
 *
 * @return the GIT sha of this codebase.
 * @throws ManipulationException if an error occurs.
 */
public static String getManifestInformation() throws ManipulationException {
    String result = "";
    try {
        final Enumeration<URL> resources = ManifestUtils.class.getClassLoader().getResources("META-INF/MANIFEST.MF");
        while (resources.hasMoreElements()) {
            final URL jarUrl = resources.nextElement();
            if (jarUrl.getFile().contains("pom-manipulation-")) {
                final Manifest manifest = new Manifest(jarUrl.openStream());
                result = manifest.getMainAttributes().getValue("Implementation-Version");
                result += " ( SHA: " + manifest.getMainAttributes().getValue("Scm-Revision") + " ) ";
                break;
            }
        }
    } catch (final IOException e) {
        throw new ManipulationException("Error retrieving information from manifest", e);
    }
    return result;
}
Also used : ManipulationException(org.commonjava.maven.ext.common.ManipulationException) IOException(java.io.IOException) Manifest(java.util.jar.Manifest) URL(java.net.URL)

Example 28 with ManipulationException

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

the class PomIO method readModelsForManipulation.

/**
 * Read {@link Model} instances by parsing the POM directly. This is useful to escape some post-processing that happens when the
 * {@link MavenProject#getOriginalModel()} instance is set.
 *
 * @param executionRoot the top level pom file.
 * @param peeked a collection of poms resolved from the top level file.
 * @return a collection of Projects
 * @throws ManipulationException if an error occurs.
 */
private List<Project> readModelsForManipulation(File executionRoot, final List<PomPeek> peeked) throws ManipulationException {
    final List<Project> projects = new ArrayList<>();
    final HashMap<Project, ProjectVersionRef> projectToParent = new HashMap<>();
    for (final PomPeek peek : peeked) {
        final File pom = peek.getPom();
        // Sucks, but we have to brute-force reading in the raw model.
        // The effective-model building, below, has a tantalizing getRawModel()
        // method on the result, BUT this seems to return models that have
        // the plugin versions set inside profiles...so they're not entirely
        // raw.
        Model raw = null;
        InputStream in = null;
        try {
            in = new FileInputStream(pom);
            raw = new MavenXpp3Reader().read(in);
        } catch (final IOException | XmlPullParserException e) {
            throw new ManipulationException("Failed to build model for POM: %s.\n--> %s", e, pom, e.getMessage());
        } finally {
            closeQuietly(in);
        }
        if (raw == null) {
            continue;
        }
        final Project project = new Project(pom, raw);
        projectToParent.put(project, peek.getParentKey());
        project.setInheritanceRoot(peek.isInheritanceRoot());
        if (executionRoot.equals(pom)) {
            logger.debug("Setting execution root to {} with file {}" + (project.isInheritanceRoot() ? " and is the inheritance root. " : ""), project, pom);
            project.setExecutionRoot();
            try {
                if (FileUtils.readFileToString(pom).contains(MODIFIED_BY)) {
                    project.setIncrementalPME(true);
                }
            } catch (final IOException e) {
                throw new ManipulationException("Failed to read POM: %s", e, pom);
            }
        }
        projects.add(project);
    }
    // Fill out inheritance info for every project we have created.
    for (Project p : projects) {
        ProjectVersionRef pvr = projectToParent.get(p);
        p.setProjectParent(getParent(projects, pvr));
    }
    return projects;
}
Also used : HashMap(java.util.HashMap) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) MavenXpp3Reader(org.apache.maven.model.io.xpp3.MavenXpp3Reader) PomPeek(org.commonjava.maven.galley.maven.parse.PomPeek) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) MavenProject(org.apache.maven.project.MavenProject) Project(org.commonjava.maven.ext.common.model.Project) ProjectVersionRef(org.commonjava.maven.atlas.ident.ref.ProjectVersionRef) Model(org.apache.maven.model.Model) ManipulationException(org.commonjava.maven.ext.common.ManipulationException) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) File(java.io.File)

Example 29 with ManipulationException

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

the class XMLIO method convert.

public String convert(Document contents) throws ManipulationException {
    StringWriter outWriter = new StringWriter();
    try {
        StreamResult streamResult = new StreamResult(outWriter);
        transformer.transform(new DOMSource(contents), streamResult);
    } catch (TransformerException e) {
        logger.error("XML transformer failure", e);
        throw new ManipulationException("XML transformer failure", e);
    }
    return outWriter.toString();
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) ManipulationException(org.commonjava.maven.ext.common.ManipulationException) TransformerException(javax.xml.transform.TransformerException)

Example 30 with ManipulationException

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

the class ModelIO method resolveRawModel.

/**
 * Read the raw model (equivalent to the pom file on disk) from a given GAV.
 *
 * @param ref the ProjectVersion to read.
 * @return the Maven Model for the GAV
 * @throws ManipulationException if an error occurs.
 */
public Model resolveRawModel(final ProjectVersionRef ref) throws ManipulationException {
    Transfer transfer;
    try {
        transfer = galleyWrapper.resolveArtifact(ref.asPomArtifact());
    } catch (final TransferException e) {
        throw new ManipulationException("Failed to resolve POM: %s.\n--> %s", e, ref, e.getMessage());
    }
    if (transfer == null) {
        throw new ManipulationException("Failed to resolve POM: " + ref.asPomArtifact());
    }
    InputStream in = null;
    try {
        in = transfer.openInputStream();
        return new MavenXpp3Reader().read(in);
    } catch (final IOException | XmlPullParserException e) {
        throw new ManipulationException("Failed to build model for POM: %s.\n--> %s", e, ref, e.getMessage());
    } finally {
        closeQuietly(in);
    }
}
Also used : TransferException(org.commonjava.maven.galley.TransferException) InputStream(java.io.InputStream) Transfer(org.commonjava.maven.galley.model.Transfer) ManipulationException(org.commonjava.maven.ext.common.ManipulationException) MavenXpp3Reader(org.apache.maven.model.io.xpp3.MavenXpp3Reader) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) IOException(java.io.IOException)

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