Search in sources :

Example 1 with MavenXpp3Reader

use of org.apache.maven.model.io.xpp3.MavenXpp3Reader in project BIMserver by opensourceBIM.

the class PluginManager method loadFromPluginDir.

public PluginBundle loadFromPluginDir(PluginBundleVersionIdentifier pluginBundleVersionIdentifier, SPluginBundleVersion pluginBundleVersion, List<SPluginInformation> plugins, boolean strictDependencyChecking) throws Exception {
    Path target = pluginsDir.resolve(pluginBundleVersionIdentifier.getFileName());
    if (!Files.exists(target)) {
        throw new PluginException(target.toString() + " not found");
    }
    SPluginBundle sPluginBundle = new SPluginBundle();
    MavenXpp3Reader mavenreader = new MavenXpp3Reader();
    try (JarFile jarFile = new JarFile(target.toFile())) {
        ZipEntry entry = jarFile.getEntry("META-INF/maven/" + pluginBundleVersion.getGroupId() + "/" + pluginBundleVersion.getArtifactId() + "/pom.xml");
        Model model = mavenreader.read(jarFile.getInputStream(entry));
        sPluginBundle.setOrganization(model.getOrganization().getName());
        sPluginBundle.setName(model.getName());
        DelegatingClassLoader delegatingClassLoader = new DelegatingClassLoader(getClass().getClassLoader());
        for (org.apache.maven.model.Dependency dependency : model.getDependencies()) {
            if (dependency.getGroupId().equals("org.opensourcebim") && (dependency.getArtifactId().equals("shared") || dependency.getArtifactId().equals("pluginbase"))) {
            // TODO Skip, we should also check the version though
            } else {
                PluginBundleIdentifier pluginBundleIdentifier = new PluginBundleIdentifier(dependency.getGroupId(), dependency.getArtifactId());
                if (pluginBundleIdentifierToPluginBundle.containsKey(pluginBundleIdentifier)) {
                    if (strictDependencyChecking) {
                        VersionRange versionRange = VersionRange.createFromVersion(dependency.getVersion());
                        String version = pluginBundleIdentifierToPluginBundle.get(pluginBundleIdentifier).getPluginBundleVersion().getVersion();
                        ArtifactVersion artifactVersion = new DefaultArtifactVersion(version);
                        if (versionRange.containsVersion(artifactVersion)) {
                        // OK
                        } else {
                            throw new Exception("Required dependency " + pluginBundleIdentifier + " is installed, but it's version (" + version + ") does not comply to the required version (" + dependency.getVersion() + ")");
                        }
                    } else {
                        LOGGER.info("Skipping strict dependency checking for dependency " + dependency.getArtifactId());
                    }
                } else {
                    if (dependency.getGroupId().equals("org.opensourcebim") && (dependency.getArtifactId().equals("shared") || dependency.getArtifactId().equals("pluginbase"))) {
                    } else {
                        MavenPluginLocation mavenPluginLocation = mavenPluginRepository.getPluginLocation(dependency.getGroupId(), dependency.getArtifactId());
                        try {
                            Path depJarFile = mavenPluginLocation.getVersionJar(dependency.getVersion());
                            FileJarClassLoader jarClassLoader = new FileJarClassLoader(this, delegatingClassLoader, depJarFile);
                            jarClassLoaders.add(jarClassLoader);
                            delegatingClassLoader.add(jarClassLoader);
                        } catch (Exception e) {
                        }
                    }
                }
            }
        }
        return loadPlugin(pluginBundleVersionIdentifier, target, sPluginBundle, pluginBundleVersion, plugins, delegatingClassLoader);
    }
}
Also used : Path(java.nio.file.Path) PluginException(org.bimserver.shared.exceptions.PluginException) ZipEntry(java.util.zip.ZipEntry) SPluginBundle(org.bimserver.interfaces.objects.SPluginBundle) DefaultArtifactVersion(org.apache.maven.artifact.versioning.DefaultArtifactVersion) MavenXpp3Reader(org.apache.maven.model.io.xpp3.MavenXpp3Reader) VersionRange(org.apache.maven.artifact.versioning.VersionRange) JarFile(java.util.jar.JarFile) DelegatingClassLoader(org.bimserver.plugins.classloaders.DelegatingClassLoader) ObjectIDMException(org.bimserver.plugins.objectidms.ObjectIDMException) ArtifactDescriptorException(org.eclipse.aether.resolution.ArtifactDescriptorException) DependencyCollectionException(org.eclipse.aether.collection.DependencyCollectionException) ServiceException(org.bimserver.shared.exceptions.ServiceException) IOException(java.io.IOException) PluginException(org.bimserver.shared.exceptions.PluginException) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) JAXBException(javax.xml.bind.JAXBException) FileNotFoundException(java.io.FileNotFoundException) UserException(org.bimserver.shared.exceptions.UserException) ChannelConnectionException(org.bimserver.shared.ChannelConnectionException) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) DependencyResolutionException(org.eclipse.aether.resolution.DependencyResolutionException) FileSystemNotFoundException(java.nio.file.FileSystemNotFoundException) DeserializeException(org.bimserver.plugins.deserializers.DeserializeException) DefaultArtifactVersion(org.apache.maven.artifact.versioning.DefaultArtifactVersion) ArtifactVersion(org.apache.maven.artifact.versioning.ArtifactVersion) FileJarClassLoader(org.bimserver.plugins.classloaders.FileJarClassLoader) Model(org.apache.maven.model.Model)

Example 2 with MavenXpp3Reader

use of org.apache.maven.model.io.xpp3.MavenXpp3Reader in project BIMserver by opensourceBIM.

the class PluginManager method update.

public PluginBundle update(PluginBundleVersionIdentifier pluginBundleVersionIdentifier, SPluginBundle sPluginBundle, SPluginBundleVersion pluginBundleVersion, Path jarFile, Path pomFile, List<SPluginInformation> plugins) throws Exception {
    PluginBundle existingPluginBundle = pluginBundleIdentifierToPluginBundle.get(pluginBundleVersionIdentifier.getPluginBundleIdentifier());
    if (existingPluginBundle == null) {
        throw new UserException("No previous version of plugin bundle " + pluginBundleVersionIdentifier.getPluginBundleIdentifier() + " found");
    }
    try {
        existingPluginBundle.close();
        if (pluginBundleIdentifierToPluginBundle.remove(pluginBundleVersionIdentifier.getPluginBundleIdentifier()) == null) {
            LOGGER.warn("Previous version of " + pluginBundleVersionIdentifier.getPluginBundleIdentifier() + " not found");
        }
        PluginBundleVersionIdentifier currentVersion = pluginBundleIdentifierToCurrentPluginBundleVersionIdentifier.get(pluginBundleVersionIdentifier.getPluginBundleIdentifier());
        if (pluginBundleIdentifierToCurrentPluginBundleVersionIdentifier.remove(pluginBundleVersionIdentifier.getPluginBundleIdentifier()) == null) {
            LOGGER.warn("Previous version of " + pluginBundleVersionIdentifier.getPluginBundleIdentifier() + " not found");
        }
        if (pluginBundleVersionIdentifierToPluginBundle.remove(currentVersion) == null) {
            LOGGER.warn("Previous version (" + currentVersion + ") of " + pluginBundleVersionIdentifier.getPluginBundleIdentifier() + " not found");
        }
        for (PluginContext pluginContext : existingPluginBundle) {
            Set<PluginContext> set = implementations.get(pluginContext.getPluginInterface());
            set.remove(pluginContext);
        }
        if (existingPluginBundle.getPluginBundle().getInstalledVersion().getType() == SPluginBundleType.MAVEN) {
            Path target = pluginsDir.resolve(currentVersion.getFileName());
            Files.delete(target);
        }
    // for (PluginContext pluginContext : existingPluginBundle) {
    // pluginChangeListener.pluginUninstalled(pluginContext);
    // }
    } catch (IOException e) {
        LOGGER.error("", e);
    }
    Path target = pluginsDir.resolve(pluginBundleVersionIdentifier.getFileName());
    if (Files.exists(target)) {
        throw new PluginException("This plugin has already been installed " + target.getFileName().toString());
    }
    Files.copy(jarFile, target);
    MavenXpp3Reader mavenreader = new MavenXpp3Reader();
    Model model = null;
    try (FileReader fileReader = new FileReader(pomFile.toFile())) {
        model = mavenreader.read(fileReader);
    }
    DelegatingClassLoader delegatingClassLoader = new DelegatingClassLoader(getClass().getClassLoader());
    for (org.apache.maven.model.Dependency dependency : model.getDependencies()) {
        if (dependency.getGroupId().equals("org.opensourcebim") && (dependency.getArtifactId().equals("shared") || dependency.getArtifactId().equals("pluginbase"))) {
        // TODO Skip, we should also check the version though
        } else {
            PluginBundleIdentifier pluginBundleIdentifier = new PluginBundleIdentifier(dependency.getGroupId(), dependency.getArtifactId());
            if (pluginBundleIdentifierToPluginBundle.containsKey(pluginBundleIdentifier)) {
                // if (false) {
                // VersionRange versionRange =
                // VersionRange.createFromVersion(dependency.getVersion());
                // String version =
                // pluginBundleIdentifierToPluginBundle.get(pluginBundleIdentifier).getPluginBundleVersion().getVersion();
                // ArtifactVersion artifactVersion = new
                // DefaultArtifactVersion(version);
                // if (versionRange.containsVersion(artifactVersion)) {
                // // OK
                // } else {
                // throw new Exception("Required dependency " +
                // pluginBundleIdentifier + " is installed, but it's version
                // (" + version + ") does not comply to the required version
                // (" + dependency.getVersion() + ")");
                // }
                // } else {
                LOGGER.info("Skipping strict dependency checking for dependency " + dependency.getArtifactId());
            // }
            } else {
                if (dependency.getGroupId().equals("org.opensourcebim") && (dependency.getArtifactId().equals("shared") || dependency.getArtifactId().equals("pluginbase"))) {
                    throw new Exception("Required dependency " + pluginBundleIdentifier + " is not installed");
                } else {
                    MavenPluginLocation mavenPluginLocation = mavenPluginRepository.getPluginLocation(model.getRepositories().get(0).getUrl(), dependency.getGroupId(), dependency.getArtifactId());
                    try {
                        Path depJarFile = mavenPluginLocation.getVersionJar(dependency.getVersion());
                        FileJarClassLoader jarClassLoader = new FileJarClassLoader(this, delegatingClassLoader, depJarFile);
                        jarClassLoaders.add(jarClassLoader);
                        delegatingClassLoader.add(jarClassLoader);
                    } catch (Exception e) {
                    }
                }
            }
        }
    }
    PluginBundle pluginBundle = null;
    // Stage 1, load all plugins from the JAR file and initialize them
    try {
        pluginBundle = loadPluginsFromJar(pluginBundleVersionIdentifier, target, sPluginBundle, pluginBundleVersion, delegatingClassLoader);
        for (SPluginInformation sPluginInformation : plugins) {
            if (sPluginInformation.isEnabled()) {
                PluginContext pluginContext = pluginBundle.getPluginContext(sPluginInformation.getIdentifier());
                pluginContext.getPlugin().init(pluginContext);
            }
        }
    } catch (Exception e) {
        Files.delete(target);
        LOGGER.error("", e);
        throw e;
    }
    // uninstalled
    try {
        long pluginBundleVersionId = pluginChangeListener.pluginBundleUpdated(pluginBundle);
        for (SPluginInformation sPluginInformation : plugins) {
            if (sPluginInformation.isEnabled()) {
                PluginContext pluginContext = pluginBundle.getPluginContext(sPluginInformation.getIdentifier());
                pluginChangeListener.pluginUpdated(pluginBundleVersionId, pluginContext, sPluginInformation);
            }
        }
        return pluginBundle;
    } catch (Exception e) {
        uninstall(pluginBundleVersionIdentifier);
        LOGGER.error("", e);
        throw e;
    }
}
Also used : Path(java.nio.file.Path) PluginException(org.bimserver.shared.exceptions.PluginException) SPluginInformation(org.bimserver.interfaces.objects.SPluginInformation) MavenXpp3Reader(org.apache.maven.model.io.xpp3.MavenXpp3Reader) IOException(java.io.IOException) DelegatingClassLoader(org.bimserver.plugins.classloaders.DelegatingClassLoader) ObjectIDMException(org.bimserver.plugins.objectidms.ObjectIDMException) ArtifactDescriptorException(org.eclipse.aether.resolution.ArtifactDescriptorException) DependencyCollectionException(org.eclipse.aether.collection.DependencyCollectionException) ServiceException(org.bimserver.shared.exceptions.ServiceException) IOException(java.io.IOException) PluginException(org.bimserver.shared.exceptions.PluginException) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) JAXBException(javax.xml.bind.JAXBException) FileNotFoundException(java.io.FileNotFoundException) UserException(org.bimserver.shared.exceptions.UserException) ChannelConnectionException(org.bimserver.shared.ChannelConnectionException) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) DependencyResolutionException(org.eclipse.aether.resolution.DependencyResolutionException) FileSystemNotFoundException(java.nio.file.FileSystemNotFoundException) DeserializeException(org.bimserver.plugins.deserializers.DeserializeException) SPluginBundle(org.bimserver.interfaces.objects.SPluginBundle) FileJarClassLoader(org.bimserver.plugins.classloaders.FileJarClassLoader) Model(org.apache.maven.model.Model) FileReader(java.io.FileReader) UserException(org.bimserver.shared.exceptions.UserException)

Example 3 with MavenXpp3Reader

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

the class PomHelper method readPom.

public static MavenProject readPom(File pomFile) throws MojoExecutionException {
    Reader reader = null;
    try {
        reader = new FileReader(pomFile);
        MavenXpp3Reader modelReader = new MavenXpp3Reader();
        return new MavenProject(modelReader.read(reader));
    } catch (FileNotFoundException e) {
        throw new MojoExecutionException("Error reading specified POM file: " + e.getMessage(), e);
    } catch (IOException e) {
        throw new MojoExecutionException("Error reading specified POM file: " + e.getMessage(), e);
    } catch (XmlPullParserException e) {
        throw new MojoExecutionException("Error reading specified POM file: " + e.getMessage(), e);
    } finally {
        IOUtil.close(reader);
    }
}
Also used : MavenProject(org.apache.maven.project.MavenProject) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) FileNotFoundException(java.io.FileNotFoundException) FileReader(java.io.FileReader) Reader(java.io.Reader) MavenXpp3Reader(org.apache.maven.model.io.xpp3.MavenXpp3Reader) FileReader(java.io.FileReader) MavenXpp3Reader(org.apache.maven.model.io.xpp3.MavenXpp3Reader) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) IOException(java.io.IOException)

Example 4 with MavenXpp3Reader

use of org.apache.maven.model.io.xpp3.MavenXpp3Reader in project kie-wb-common by kiegroup.

the class MavenUtils method resolveDependenciesFromMultimodulePrj.

public static List<Artifact> resolveDependenciesFromMultimodulePrj(List<String> pomsPaths) {
    MavenXpp3Reader reader = new MavenXpp3Reader();
    List<Artifact> deps = new ArrayList<>();
    try {
        for (String pomx : pomsPaths) {
            Path pom = Paths.get(pomx);
            Model model = reader.read(new ByteArrayInputStream(Files.readAllBytes(pom)));
            if (model.getDependencyManagement() != null && model.getDependencyManagement().getDependencies() != null) {
                createArtifacts(model.getDependencyManagement().getDependencies(), deps);
            }
            if (model.getDependencies() != null) {
                createArtifacts(model.getDependencies(), deps);
            }
        }
    } catch (Exception ex) {
        logger.error(ex.getMessage());
        return Collections.emptyList();
    }
    return deps;
}
Also used : Path(org.uberfire.java.nio.file.Path) ByteArrayInputStream(java.io.ByteArrayInputStream) ArrayList(java.util.ArrayList) Model(org.apache.maven.model.Model) MavenXpp3Reader(org.apache.maven.model.io.xpp3.MavenXpp3Reader) DefaultArtifact(org.apache.maven.artifact.DefaultArtifact) Artifact(org.apache.maven.artifact.Artifact)

Example 5 with MavenXpp3Reader

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

the class MavenUtil method hasDependency.

public static boolean hasDependency(IProject mavenProject, String groupId, String artifactId) {
    MavenXpp3Reader mavenReader = new MavenXpp3Reader();
    IFile pomFile = mavenProject.getFile("pom.xml");
    if (FileUtil.notExists(pomFile)) {
        return false;
    }
    try (FileReader reader = new FileReader(pomFile.getLocation().toFile())) {
        Model model = mavenReader.read(reader);
        if (model != null) {
            List<Dependency> dependencies = model.getDependencies();
            for (Dependency dependency : dependencies) {
                String tempgroutId = dependency.getGroupId();
                String tempartifactId = dependency.getArtifactId();
                if (groupId.equals(tempgroutId) && artifactId.equals(tempartifactId)) {
                    return true;
                }
            }
        }
    } catch (Exception e) {
    }
    return false;
}
Also used : IFile(org.eclipse.core.resources.IFile) Model(org.apache.maven.model.Model) MavenXpp3Reader(org.apache.maven.model.io.xpp3.MavenXpp3Reader) FileReader(java.io.FileReader) Dependency(org.apache.maven.model.Dependency) URISyntaxException(java.net.URISyntaxException) CoreException(org.eclipse.core.runtime.CoreException)

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