Search in sources :

Example 36 with MavenXpp3Reader

use of org.apache.maven.model.io.xpp3.MavenXpp3Reader in project maven-dependency-plugin by apache.

the class AnalyzeDuplicateMojo method execute.

/**
 * {@inheritDoc}
 */
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    if (skip) {
        getLog().info("Skipping plugin execution");
        return;
    }
    MavenXpp3Reader pomReader = new MavenXpp3Reader();
    Model model = null;
    Reader reader = null;
    try {
        reader = ReaderFactory.newXmlReader(project.getFile());
        model = pomReader.read(reader);
        reader.close();
        reader = null;
    } catch (Exception e) {
        throw new MojoExecutionException("IOException: " + e.getMessage(), e);
    } finally {
        IOUtil.close(reader);
    }
    Set<String> duplicateDependencies = Collections.emptySet();
    if (model.getDependencies() != null) {
        duplicateDependencies = findDuplicateDependencies(model.getDependencies());
    }
    Set<String> duplicateDependenciesManagement = Collections.emptySet();
    if (model.getDependencyManagement() != null && model.getDependencyManagement().getDependencies() != null) {
        duplicateDependenciesManagement = findDuplicateDependencies(model.getDependencyManagement().getDependencies());
    }
    if (getLog().isInfoEnabled()) {
        StringBuilder sb = new StringBuilder();
        if (!duplicateDependencies.isEmpty()) {
            sb.append("List of duplicate dependencies defined in <dependencies/> in your pom.xml:\n");
            for (Iterator<String> it = duplicateDependencies.iterator(); it.hasNext(); ) {
                String dup = it.next();
                sb.append("\to ").append(dup);
                if (it.hasNext()) {
                    sb.append("\n");
                }
            }
        }
        if (!duplicateDependenciesManagement.isEmpty()) {
            if (sb.length() > 0) {
                sb.append("\n");
            }
            sb.append("List of duplicate dependencies defined in <dependencyManagement/> in your pom.xml:\n");
            for (Iterator<String> it = duplicateDependenciesManagement.iterator(); it.hasNext(); ) {
                String dup = it.next();
                sb.append("\to ").append(dup);
                if (it.hasNext()) {
                    sb.append("\n");
                }
            }
        }
        if (sb.length() > 0) {
            getLog().info(sb.toString());
        } else {
            getLog().info("No duplicate dependencies found in <dependencies/> or in <dependencyManagement/>");
        }
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Model(org.apache.maven.model.Model) Reader(java.io.Reader) MavenXpp3Reader(org.apache.maven.model.io.xpp3.MavenXpp3Reader) MavenXpp3Reader(org.apache.maven.model.io.xpp3.MavenXpp3Reader) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException)

Example 37 with MavenXpp3Reader

use of org.apache.maven.model.io.xpp3.MavenXpp3Reader 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 38 with MavenXpp3Reader

use of org.apache.maven.model.io.xpp3.MavenXpp3Reader 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)

Example 39 with MavenXpp3Reader

use of org.apache.maven.model.io.xpp3.MavenXpp3Reader in project fabric8 by jboss-fuse.

the class PomDetails method getModel.

public Model getModel() throws IOException {
    try {
        Model model = new MavenXpp3Reader().read(new FileInputStream(file));
        model.setGroupId(properties.getProperty("groupId", model.getGroupId()));
        model.setArtifactId(properties.getProperty("artifactId", model.getArtifactId()));
        model.setVersion(properties.getProperty("version", model.getVersion()));
        return model;
    } catch (XmlPullParserException e) {
        throw new IOException("Error parsing maven pom " + file, e);
    }
}
Also used : Model(org.apache.maven.model.Model) MavenXpp3Reader(org.apache.maven.model.io.xpp3.MavenXpp3Reader) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 40 with MavenXpp3Reader

use of org.apache.maven.model.io.xpp3.MavenXpp3Reader in project liferay-ide by liferay.

the class LiferayMavenLegacyProjectUpdater method _hasDependency.

private boolean _hasDependency(IProject project, String groupId, String artifactId) {
    boolean retVal = false;
    IFile iFile = project.getFile("pom.xml");
    File pomFile = iFile.getLocation().toFile();
    MavenXpp3Reader mavenReader = new MavenXpp3Reader();
    try (FileReader reader = new FileReader(pomFile.getAbsolutePath())) {
        Model model = mavenReader.read(reader);
        List<Dependency> dependencies = model.getDependencies();
        for (Dependency dependency : dependencies) {
            if (groupId.equals(dependency.getGroupId()) && artifactId.equals(dependency.getArtifactId())) {
                retVal = true;
                break;
            }
        }
    } catch (FileNotFoundException fnfe) {
    } catch (IOException ioe) {
    } catch (XmlPullParserException xppe) {
    }
    return retVal;
}
Also used : IFile(org.eclipse.core.resources.IFile) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) Model(org.apache.maven.model.Model) FileNotFoundException(java.io.FileNotFoundException) MavenXpp3Reader(org.apache.maven.model.io.xpp3.MavenXpp3Reader) FileReader(java.io.FileReader) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) Dependency(org.apache.maven.model.Dependency) IOException(java.io.IOException) IFile(org.eclipse.core.resources.IFile) File(java.io.File)

Aggregations

MavenXpp3Reader (org.apache.maven.model.io.xpp3.MavenXpp3Reader)55 Model (org.apache.maven.model.Model)50 IOException (java.io.IOException)35 XmlPullParserException (org.codehaus.plexus.util.xml.pull.XmlPullParserException)30 FileReader (java.io.FileReader)23 File (java.io.File)15 Reader (java.io.Reader)15 FileNotFoundException (java.io.FileNotFoundException)14 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)10 Path (java.nio.file.Path)7 ArtifactResolutionException (org.eclipse.aether.resolution.ArtifactResolutionException)7 MavenXpp3Writer (org.apache.maven.model.io.xpp3.MavenXpp3Writer)6 PluginException (org.bimserver.shared.exceptions.PluginException)6 ArtifactDescriptorException (org.eclipse.aether.resolution.ArtifactDescriptorException)6 InputStream (java.io.InputStream)5 StringReader (java.io.StringReader)5 SPluginBundle (org.bimserver.interfaces.objects.SPluginBundle)5 SPluginBundleVersion (org.bimserver.interfaces.objects.SPluginBundleVersion)5 FileInputStream (java.io.FileInputStream)4 DelegatingClassLoader (org.bimserver.plugins.classloaders.DelegatingClassLoader)4