Search in sources :

Example 6 with MavenXpp3Reader

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

the class MavenProjectBuilder method initBundle.

public IStatus initBundle(IProject project, String bundleUrl, IProgressMonitor monitor) throws CoreException {
    if (bundleUrl != null) {
        File pomFile = FileUtil.getFile(project.getFile("pom.xml"));
        MavenXpp3Reader mavenReader = new MavenXpp3Reader();
        MavenXpp3Writer mavenWriter = new MavenXpp3Writer();
        try (FileReader reader = new FileReader(pomFile)) {
            Model model = mavenReader.read(reader);
            if (model != null) {
                Build build = model.getBuild();
                Plugin plugin = build.getPluginsAsMap().get("com.liferay:com.liferay.portal.tools.bundle.support");
                if (plugin != null) {
                    try (FileWriter fileWriter = new FileWriter(pomFile)) {
                        Xpp3Dom origin = (Xpp3Dom) plugin.getConfiguration();
                        Xpp3Dom newConfiguration = new Xpp3Dom("configuration");
                        Xpp3Dom url = new Xpp3Dom("url");
                        url.setValue(bundleUrl);
                        newConfiguration.addChild(url);
                        plugin.setConfiguration(Xpp3Dom.mergeXpp3Dom(newConfiguration, origin));
                        mavenWriter.write(fileWriter, model);
                    }
                }
            }
        } catch (Exception e) {
            LiferayMavenCore.logError("Could not write file in" + pomFile, e);
        }
    }
    IMavenProjectFacade facade = MavenUtil.getProjectFacade(project, monitor);
    if (_execMavenLaunch(project, MavenGoalUtil.getMavenInitBundleGoal(project), facade, monitor)) {
        return Status.OK_STATUS;
    }
    return LiferayMavenCore.createErrorStatus("run init-bundle error");
}
Also used : Xpp3Dom(org.codehaus.plexus.util.xml.Xpp3Dom) Build(org.apache.maven.model.Build) FileWriter(java.io.FileWriter) Model(org.apache.maven.model.Model) MavenXpp3Reader(org.apache.maven.model.io.xpp3.MavenXpp3Reader) FileReader(java.io.FileReader) IMavenProjectFacade(org.eclipse.m2e.core.project.IMavenProjectFacade) IFile(org.eclipse.core.resources.IFile) File(java.io.File) CoreException(org.eclipse.core.runtime.CoreException) MavenXpp3Writer(org.apache.maven.model.io.xpp3.MavenXpp3Writer) DebugPlugin(org.eclipse.debug.core.DebugPlugin) ResourcesPlugin(org.eclipse.core.resources.ResourcesPlugin) Plugin(org.apache.maven.model.Plugin) MavenPlugin(org.eclipse.m2e.core.MavenPlugin)

Example 7 with MavenXpp3Reader

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

the class PluginBundleManager method install.

public PluginBundle install(MavenPluginBundle mavenPluginBundle, List<SPluginInformation> plugins, boolean strictDependencyChecking) throws Exception {
    PluginBundleVersionIdentifier pluginBundleVersionIdentifier = mavenPluginBundle.getPluginVersionIdentifier();
    MavenXpp3Reader mavenreader = new MavenXpp3Reader();
    Model model = null;
    try (InputStream pomInputStream = mavenPluginBundle.getPomInputStream()) {
        model = mavenreader.read(pomInputStream);
    }
    if (plugins == null) {
        try (InputStream inputStream = mavenPluginBundle.getJarInputStream()) {
            try (JarInputStream jarInputStream = new JarInputStream(inputStream)) {
                JarEntry nextJarEntry = jarInputStream.getNextJarEntry();
                while (nextJarEntry != null) {
                    if (nextJarEntry.getName().equals("plugin/plugin.xml")) {
                        // Install all plugins
                        PluginDescriptor pluginDescriptor = pluginManager.getPluginDescriptor(new FakeClosingInputStream(jarInputStream));
                        plugins = new ArrayList<>();
                        pluginManager.processPluginDescriptor(pluginDescriptor, plugins);
                        for (SPluginInformation info : plugins) {
                            info.setInstallForAllUsers(true);
                            info.setInstallForNewUsers(true);
                        }
                        break;
                    }
                    nextJarEntry = jarInputStream.getNextJarEntry();
                }
            }
        }
    }
    DelegatingClassLoader delegatingClassLoader = new DelegatingClassLoader(getClass().getClassLoader());
    loadDependencies(mavenPluginBundle.getVersion(), strictDependencyChecking, model, delegatingClassLoader);
    Path target = pluginsDir.resolve(pluginBundleVersionIdentifier.getFileName());
    if (Files.exists(target)) {
        throw new PluginException("This plugin has already been installed " + target.getFileName().toString());
    }
    InputStream jarInputStream = mavenPluginBundle.getJarInputStream();
    try {
        Files.copy(jarInputStream, target);
    } finally {
        jarInputStream.close();
    }
    return loadPlugin(pluginBundleVersionIdentifier, target, mavenPluginBundle.getPluginBundle(), mavenPluginBundle.getPluginBundleVersion(), plugins, delegatingClassLoader);
}
Also used : Path(java.nio.file.Path) JarInputStream(java.util.jar.JarInputStream) FakeClosingInputStream(org.bimserver.utils.FakeClosingInputStream) JarInputStream(java.util.jar.JarInputStream) InputStream(java.io.InputStream) SPluginInformation(org.bimserver.interfaces.objects.SPluginInformation) PluginException(org.bimserver.shared.exceptions.PluginException) MavenXpp3Reader(org.apache.maven.model.io.xpp3.MavenXpp3Reader) JarEntry(java.util.jar.JarEntry) DelegatingClassLoader(org.bimserver.plugins.classloaders.DelegatingClassLoader) Model(org.apache.maven.model.Model) FakeClosingInputStream(org.bimserver.utils.FakeClosingInputStream)

Example 8 with MavenXpp3Reader

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

the class PluginBundleManager 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();
    Model model = null;
    try (JarFile jarFile = new JarFile(target.toFile())) {
        ZipEntry entry = jarFile.getEntry("META-INF/maven/" + pluginBundleVersion.getGroupId() + "/" + pluginBundleVersion.getArtifactId() + "/pom.xml");
        try (InputStream inputStream = jarFile.getInputStream(entry)) {
            model = mavenreader.read(inputStream);
        }
    }
    sPluginBundle.setOrganization(model.getOrganization().getName());
    sPluginBundle.setName(model.getName());
    DelegatingClassLoader delegatingClassLoader = new DelegatingClassLoader(getClass().getClassLoader());
    loadDependencies(model.getVersion(), strictDependencyChecking, model, delegatingClassLoader);
    for (org.apache.maven.model.Dependency dependency : model.getDependencies()) {
        if (isDependencyProvidedOrIfcPlugins(dependency)) {
        // 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 (isDependencyProvided(dependency)) {
                } else {
                    // this must be IfcPlugins then?
                    MavenPluginLocation mavenPluginLocation = mavenPluginRepository.getPluginLocation(dependency.getGroupId(), dependency.getArtifactId());
                    try {
                        Path depJarFile = mavenPluginLocation.getVersionJar(dependency.getVersion());
                        FileJarClassLoader jarClassLoader = new FileJarClassLoader(pluginManager, 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) FakeClosingInputStream(org.bimserver.utils.FakeClosingInputStream) JarInputStream(java.util.jar.JarInputStream) InputStream(java.io.InputStream) 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) PluginException(org.bimserver.shared.exceptions.PluginException) ArtifactDescriptorException(org.eclipse.aether.resolution.ArtifactDescriptorException) DependencyCollectionException(org.eclipse.aether.collection.DependencyCollectionException) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) JAXBException(javax.xml.bind.JAXBException) FileNotFoundException(java.io.FileNotFoundException) UserException(org.bimserver.shared.exceptions.UserException) InvalidVersionSpecificationException(org.apache.maven.artifact.versioning.InvalidVersionSpecificationException) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) DependencyResolutionException(org.eclipse.aether.resolution.DependencyResolutionException) IOException(java.io.IOException) 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 9 with MavenXpp3Reader

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

the class PluginBundleManager method loadJavaProject.

public PluginBundle loadJavaProject(Path projectRoot, Path pomFile, Path pluginFolder, PluginDescriptor pluginDescriptor, boolean resolveRemoteDependencies) throws PluginException, FileNotFoundException, IOException, XmlPullParserException {
    MavenXpp3Reader mavenreader = new MavenXpp3Reader();
    Model model = null;
    try (FileReader reader = new FileReader(pomFile.toFile())) {
        model = mavenreader.read(reader);
    }
    PluginBundleVersionIdentifier pluginBundleVersionIdentifier = new PluginBundleVersionIdentifier(model.getGroupId(), model.getArtifactId(), model.getVersion());
    if (pluginBundleIdentifierToPluginBundle.containsKey(pluginBundleVersionIdentifier.getPluginBundleIdentifier())) {
        throw new PluginException("Plugin " + pluginBundleVersionIdentifier.getPluginBundleIdentifier().getHumanReadable() + " already loaded (version " + pluginBundleIdentifierToPluginBundle.get(pluginBundleVersionIdentifier.getPluginBundleIdentifier()).getPluginBundleVersion().getVersion() + ")");
    }
    DelegatingClassLoader delegatingClassLoader = new DelegatingClassLoader(getClass().getClassLoader());
    PublicFindClassClassLoader previous = new PublicFindClassClassLoader(getClass().getClassLoader()) {

        @Override
        public Class<?> findClass(String name) throws ClassNotFoundException {
            return null;
        }

        @Override
        public URL findResource(String name) {
            return null;
        }

        @Override
        public void dumpStructure(int indent) {
        }
    };
    Set<org.bimserver.plugins.Dependency> bimServerDependencies = new HashSet<>();
    pluginBundleVersionIdentifier = new PluginBundleVersionIdentifier(new PluginBundleIdentifier(model.getGroupId(), model.getArtifactId()), model.getVersion());
    previous = loadDependencies(bimServerDependencies, model, previous, resolveRemoteDependencies);
    delegatingClassLoader.add(previous);
    // Path libFolder = projectRoot.resolve("lib");
    // loadDependencies(libFolder, delegatingClassLoader);
    EclipsePluginClassloader pluginClassloader = new EclipsePluginClassloader(delegatingClassLoader, projectRoot);
    // pluginClassloader.dumpStructure(0);
    ResourceLoader resourceLoader = new ResourceLoader() {

        @Override
        public InputStream load(String name) {
            try {
                return Files.newInputStream(pluginFolder.resolve(name));
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    };
    SPluginBundle sPluginBundle = new SPluginBundle();
    if (model.getOrganization() == null) {
        throw new PluginException("Plugis are required to have an organization in the pom.xml file");
    }
    sPluginBundle.setOrganization(model.getOrganization().getName());
    sPluginBundle.setName(model.getName());
    SPluginBundleVersion sPluginBundleVersion = createPluginBundleVersionFromMavenModel(model, true);
    Path icon = projectRoot.resolve("icon.png");
    if (Files.exists(icon)) {
        byte[] iconBytes = Files.readAllBytes(icon);
        sPluginBundleVersion.setIcon(iconBytes);
    }
    sPluginBundle.setInstalledVersion(sPluginBundleVersion);
    return loadPlugins(pluginBundleVersionIdentifier, resourceLoader, pluginClassloader, projectRoot.toUri(), projectRoot.resolve("target/classes").toString(), pluginDescriptor, PluginSourceType.ECLIPSE_PROJECT, bimServerDependencies, sPluginBundle, sPluginBundleVersion);
}
Also used : SPluginBundleVersion(org.bimserver.interfaces.objects.SPluginBundleVersion) Path(java.nio.file.Path) PluginException(org.bimserver.shared.exceptions.PluginException) PublicFindClassClassLoader(org.bimserver.plugins.classloaders.PublicFindClassClassLoader) SPluginBundle(org.bimserver.interfaces.objects.SPluginBundle) MavenXpp3Reader(org.apache.maven.model.io.xpp3.MavenXpp3Reader) Dependency(org.eclipse.aether.graph.Dependency) IOException(java.io.IOException) DelegatingClassLoader(org.bimserver.plugins.classloaders.DelegatingClassLoader) Model(org.apache.maven.model.Model) FileReader(java.io.FileReader) EclipsePluginClassloader(org.bimserver.plugins.classloaders.EclipsePluginClassloader)

Example 10 with MavenXpp3Reader

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

the class MavenPluginLocation method createMavenVersion.

private MavenPluginVersion createMavenVersion(Version version) throws ArtifactDescriptorException, FileNotFoundException, IOException, ArtifactResolutionException, XmlPullParserException {
    ArtifactDescriptorRequest descriptorRequest = new ArtifactDescriptorRequest();
    Artifact versionArtifact = new DefaultArtifact(groupId, artifactId, "pom", version.toString());
    descriptorRequest.setArtifact(versionArtifact);
    descriptorRequest.setRepositories(mavenPluginRepository.getRepositoriesAsList());
    MavenPluginVersion mavenPluginVersion = new MavenPluginVersion(versionArtifact, version);
    ArtifactDescriptorResult descriptorResult;
    descriptorResult = mavenPluginRepository.getSystem().readArtifactDescriptor(mavenPluginRepository.getSession(), descriptorRequest);
    try {
        ArtifactRequest request = new ArtifactRequest();
        request.setArtifact(descriptorResult.getArtifact());
        request.setRepositories(mavenPluginRepository.getRepositoriesAsList());
        ArtifactResult resolveArtifact = mavenPluginRepository.getSystem().resolveArtifact(mavenPluginRepository.getSession(), request);
        File pomFile = resolveArtifact.getArtifact().getFile();
        MavenXpp3Reader mavenreader = new MavenXpp3Reader();
        try (FileReader fileReader = new FileReader(pomFile)) {
            Model model = mavenreader.read(fileReader);
            mavenPluginVersion.setModel(model);
        }
    } catch (Exception e) {
        LOGGER.error(e.getMessage());
    }
    for (org.eclipse.aether.graph.Dependency dependency : descriptorResult.getDependencies()) {
        DefaultArtifactVersion artifactVersion = new DefaultArtifactVersion(dependency.getArtifact().getVersion());
        mavenPluginVersion.addDependency(new MavenDependency(dependency.getArtifact(), artifactVersion));
    }
    return mavenPluginVersion;
}
Also used : DefaultArtifactVersion(org.apache.maven.artifact.versioning.DefaultArtifactVersion) MavenXpp3Reader(org.apache.maven.model.io.xpp3.MavenXpp3Reader) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Artifact(org.eclipse.aether.artifact.Artifact) ArtifactDescriptorException(org.eclipse.aether.resolution.ArtifactDescriptorException) VersionRangeResolutionException(org.eclipse.aether.resolution.VersionRangeResolutionException) ParseException(java.text.ParseException) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ArtifactResult(org.eclipse.aether.resolution.ArtifactResult) ArtifactRequest(org.eclipse.aether.resolution.ArtifactRequest) Model(org.apache.maven.model.Model) FileReader(java.io.FileReader) ArtifactDescriptorResult(org.eclipse.aether.resolution.ArtifactDescriptorResult) File(java.io.File) ArtifactDescriptorRequest(org.eclipse.aether.resolution.ArtifactDescriptorRequest) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact)

Aggregations

MavenXpp3Reader (org.apache.maven.model.io.xpp3.MavenXpp3Reader)65 Model (org.apache.maven.model.Model)59 IOException (java.io.IOException)39 XmlPullParserException (org.codehaus.plexus.util.xml.pull.XmlPullParserException)33 FileReader (java.io.FileReader)27 File (java.io.File)18 FileNotFoundException (java.io.FileNotFoundException)17 Reader (java.io.Reader)15 Path (java.nio.file.Path)11 PluginException (org.bimserver.shared.exceptions.PluginException)11 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)10 ArtifactResolutionException (org.eclipse.aether.resolution.ArtifactResolutionException)10 SPluginBundle (org.bimserver.interfaces.objects.SPluginBundle)9 ArtifactDescriptorException (org.eclipse.aether.resolution.ArtifactDescriptorException)9 DelegatingClassLoader (org.bimserver.plugins.classloaders.DelegatingClassLoader)8 InputStream (java.io.InputStream)7 FileInputStream (java.io.FileInputStream)6 DefaultArtifactVersion (org.apache.maven.artifact.versioning.DefaultArtifactVersion)6 ArrayList (java.util.ArrayList)5 JarFile (java.util.jar.JarFile)5