Search in sources :

Example 11 with MavenXpp3Reader

use of org.apache.maven.model.io.xpp3.MavenXpp3Reader in project ServiceStack.Java by ServiceStack.

the class EclipseMavenHelper method addMavenDependencyIfRequired.

public boolean addMavenDependencyIfRequired(IFile pomFile, String groupId, String packageId, String version) throws Exception {
    boolean dependencyAdded = false;
    MavenXpp3Reader reader = new MavenXpp3Reader();
    Model pomModel;
    try {
        pomModel = reader.read(new FileReader(new File(pomFile.getLocationURI())));
        final List<Dependency> dependencies = pomModel.getDependencies();
        boolean requiresPomDependency = true;
        for (Dependency dep : dependencies) {
            if (Objects.equals(dep.getGroupId(), groupId) && Objects.equals(dep.getArtifactId(), packageId)) {
                requiresPomDependency = false;
            }
        }
        String content;
        if (requiresPomDependency) {
            Dependency dependency = new Dependency();
            dependency.setGroupId(groupId);
            dependency.setArtifactId(packageId);
            dependency.setVersion(version);
            pomModel.addDependency(dependency);
            StringWriter strWriter = new StringWriter();
            new MavenXpp3Writer().write(strWriter, pomModel);
            content = strWriter.toString();
            InputStream stream = new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8));
            pomFile.setContents(stream, false, false, null);
            dependencyAdded = true;
        }
    } catch (IOException | XmlPullParserException e) {
        e.printStackTrace();
        throw new Exception("Unable to process pom.xml to add " + groupId + ":" + packageId + ":" + version);
    }
    return dependencyAdded;
}
Also used : MavenXpp3Reader(org.apache.maven.model.io.xpp3.MavenXpp3Reader) Dependency(org.apache.maven.model.Dependency) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) Model(org.apache.maven.model.Model) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) IFile(org.eclipse.core.resources.IFile) MavenXpp3Writer(org.apache.maven.model.io.xpp3.MavenXpp3Writer)

Example 12 with MavenXpp3Reader

use of org.apache.maven.model.io.xpp3.MavenXpp3Reader in project ceylon by eclipse.

the class AetherResolverImpl method getDependencies.

@Override
public DependencyDescriptor getDependencies(File pomXml, String name, String version) throws IOException {
    MavenXpp3Reader reader = new MavenXpp3Reader();
    Model model;
    try (FileReader fileReader = new FileReader(pomXml)) {
        model = reader.read(fileReader);
    } catch (XmlPullParserException e) {
        throw new IOException(e);
    }
    return new ModelDependencyDescriptor(model);
}
Also used : Model(org.eclipse.ceylon.aether.apache.maven.model.Model) MavenXpp3Reader(org.eclipse.ceylon.aether.apache.maven.model.io.xpp3.MavenXpp3Reader) FileReader(java.io.FileReader) XmlPullParserException(org.eclipse.ceylon.aether.codehaus.plexus.util.xml.pull.XmlPullParserException) IOException(java.io.IOException)

Example 13 with MavenXpp3Reader

use of org.apache.maven.model.io.xpp3.MavenXpp3Reader in project ceylon by eclipse.

the class AetherResolverImpl method getDependencies.

@Override
public DependencyDescriptor getDependencies(InputStream pomXml, String name, String version) throws IOException {
    MavenXpp3Reader reader = new MavenXpp3Reader();
    Model model;
    try {
        model = reader.read(pomXml);
    } catch (XmlPullParserException e) {
        throw new IOException(e);
    }
    return new ModelDependencyDescriptor(model);
}
Also used : Model(org.eclipse.ceylon.aether.apache.maven.model.Model) MavenXpp3Reader(org.eclipse.ceylon.aether.apache.maven.model.io.xpp3.MavenXpp3Reader) XmlPullParserException(org.eclipse.ceylon.aether.codehaus.plexus.util.xml.pull.XmlPullParserException) IOException(java.io.IOException)

Example 14 with MavenXpp3Reader

use of org.apache.maven.model.io.xpp3.MavenXpp3Reader in project mule by mulesoft.

the class MavenUtils method getPomModelFolder.

/**
 * Returns the {@link Model} from a given artifact folder
 *
 * @param artifactFolder folder containing the exploded artifact file.
 * @return the {@link Model} from the {@value ArtifactPluginDescriptor#MULE_PLUGIN_POM} file if available
 * @throws ArtifactDescriptorCreateException if the folder does not contain a {@value ArtifactPluginDescriptor#MULE_PLUGIN_POM}
 *         file or the file can' be loaded
 */
public static Model getPomModelFolder(File artifactFolder) {
    final File mulePluginPom = lookupPomFromMavenLocation(artifactFolder);
    MavenXpp3Reader reader = new MavenXpp3Reader();
    Model model;
    try (FileReader mulePluginPomFileReader = new FileReader(mulePluginPom)) {
        model = reader.read(mulePluginPomFileReader);
    } catch (IOException | XmlPullParserException e) {
        throw new ArtifactDescriptorCreateException(format("There was an issue reading '%s' for the plugin '%s'", mulePluginPom.getName(), artifactFolder.getAbsolutePath()), e);
    }
    return model;
}
Also used : Model(org.apache.maven.model.Model) ArtifactDescriptorCreateException(org.mule.runtime.module.artifact.api.descriptor.ArtifactDescriptorCreateException) MavenXpp3Reader(org.apache.maven.model.io.xpp3.MavenXpp3Reader) FileReader(java.io.FileReader) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) IOException(java.io.IOException) FileUtils.createFile(org.mule.runtime.core.api.util.FileUtils.createFile) File(java.io.File)

Example 15 with MavenXpp3Reader

use of org.apache.maven.model.io.xpp3.MavenXpp3Reader in project spoon by INRIA.

the class MavenLauncher method readPOM.

/**
 * Extract the information from the pom
 * @param path the path to the pom
 * @param parent the parent pom
 * @return the extracted model
 * @throws IOException when the file does not exist
 * @throws XmlPullParserException when the file is corrupted
 */
private InheritanceModel readPOM(String path, InheritanceModel parent) throws IOException, XmlPullParserException {
    if (!path.endsWith(".xml") && !path.endsWith(".pom")) {
        path = Paths.get(path, "pom.xml").toString();
    }
    File pomFile = new File(path);
    if (!pomFile.exists()) {
        return null;
    }
    MavenXpp3Reader pomReader = new MavenXpp3Reader();
    try (FileReader reader = new FileReader(pomFile)) {
        Model model = pomReader.read(reader);
        InheritanceModel inheritanceModel = new InheritanceModel(model, parent, pomFile.getParentFile());
        for (String module : model.getModules()) {
            inheritanceModel.addModule(readPOM(Paths.get(pomFile.getParent(), module).toString(), inheritanceModel));
        }
        return inheritanceModel;
    }
}
Also used : Model(org.apache.maven.model.Model) MavenXpp3Reader(org.apache.maven.model.io.xpp3.MavenXpp3Reader) FileReader(java.io.FileReader) 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