简体   繁体   English

Maven OSGi项目可以编译,但无法运行

[英]Maven OSGi project compiles, but won't run

I have been trying to port Paul Bakker's ( @paul-bakker ) Making JavaFX better with OSGi : javafx-osgi-example to a maven OSGi project that uses the Apache Felix Maven Bundle Plugin (BND). 我一直在尝试将Paul Bakker的@ paul-bakker移植 到OSGi上使JavaFX更好: javafx-osgi-example到使用Apache Felix Maven Bundle插件(BND)的Maven OSGi项目。 So far it compiles without any errors, but I can't get it to run: 到目前为止,它可以编译,没有任何错误,但是我无法运行它:

Starting OSGi Framework
Found declarative services implementation: file:/C:/Users/Rev/.m2/repository/org/apache/felix/org.apache.felix.scr/1.6.2/org.apache.felix.scr-1.6.2.jar
INFO : org.apache.felix.scr (1):  Version = 1.6.2
Bundle: org.apache.felix.framework
    Registered service: [org.osgi.service.resolver.Resolver]
    Registered service: [org.osgi.service.packageadmin.PackageAdmin]
    Registered service: [org.osgi.service.startlevel.StartLevel]
Bundle: org.apache.felix.scr
    Registered service: [org.apache.felix.scr.ScrService]
    Registered service: [org.osgi.service.cm.ManagedService]
    Registered service: [org.apache.felix.scr.impl.ScrGogoCommand]
DEBUG: Starting ComponentActorThread
Bundle: null
Bundle: null
Bundle: null

As you can see, the bundles never get started. 如您所见,捆绑包永远不会开始。 No errors are thrown. 没有引发任何错误。 It just simply doesn't start. 只是根本没有开始。

Why won't the bundles start? 为什么不开始捆绑销售?

THE PROJECT SOURCES 项目资源

  1. Paul Bakker's (The original non-maven project) : javafx-osgi-example 保罗·巴克(Paul Bakker)(原始的非maven项目):javafx-osgi-example

  2. My maven implementation of Paul Bakker's javafx-osgi-example : JavaFX-Maven-Multi-Module-OSGi Paul Bakker的javafx-osgi-example的 maven实现:JavaFX-Maven-Multi-Module-OSGi

From the terminal (Windows), mvn clean install works perfectly. 从终端(Windows),mvn clean install可以完美运行。

UPDATE 更新

I've been trying to run it from Eclipse, but to no success: 我一直试图从Eclipse运行它,但没有成功:

Run -> Run as -> Java Application 运行->运行方式-> Java应用程序


Eclipse Java EE IDE for Web Developers. 用于Web开发人员的Eclipse Java EE IDE。

Version: Mars.2 Release (4.5.2) 版本:Mars.2版本(4.5.2)
Build id: 20160218-0600 Build ID:20160218-0600


UPDATE 更新

I have a class App under dist in the package rev.dist that does the launching. 我在rev.dist包中的dist下有一个类App ,它可以启动。 It goes loops throug all the directories under the rev and starts any jars whose names match the names under resources/plugins.txt . 它会遍历rev下的所有目录,并启动名称与resources/plugins.txt下的名称匹配的所有jar。

APP.java APP.java

package rev.dist;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.stream.Collectors;

import org.apache.commons.io.FilenameUtils;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleException;
import org.osgi.framework.Constants;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.launch.Framework;
import org.osgi.framework.launch.FrameworkFactory;

public class App {

    FrameworkFactory frameworkFactory;
    private Framework framework;

    private List<String> pluginsList = new ArrayList<>();

    private int addedPlugins;

    public static void main(String[] args) throws BundleException, URISyntaxException {
        App app = new App();
        app.initialize();
    }

    private void initialize() throws BundleException, URISyntaxException {
        this.plugins();

        Map<String, String> map = new HashMap<String, String>();

        // make sure the cache is cleaned
        map.put(Constants.FRAMEWORK_STORAGE_CLEAN, Constants.FRAMEWORK_STORAGE_CLEAN_ONFIRSTINIT);

        map.put("ds.showtrace", "true");
        map.put("ds.showerrors", "true");

        frameworkFactory = ServiceLoader.load(FrameworkFactory.class).iterator().next();
        framework = frameworkFactory.newFramework(map);

        System.out.println("Starting OSGi Framework");
        framework.init();

        loadScrBundle(framework);

        File baseDir = new File("../");
        String baseDirPath = baseDir.getAbsolutePath();

        File[] files = new File(baseDirPath).listFiles();

        this.showFiles(files);

        for (Bundle bundle : framework.getBundleContext().getBundles()) {
            bundle.start();
            System.out.println("Bundle: " + bundle.getSymbolicName());
            if (bundle.getRegisteredServices() != null) {
                for (ServiceReference<?> serviceReference : bundle.getRegisteredServices())
                    System.out.println("\tRegistered service: " + serviceReference);
            }
        }
    }

    public void showFiles(File[] files) throws BundleException {

        if (addedPlugins != pluginsList.size()) {
            System.out.println(":: " + pluginsList.size());
            addedPlugins--;
        }

        for (File file : files) {
            if (file.isDirectory()) {
                // System.out.println("Directory: " + file.getName());
                showFiles(file.listFiles()); // Calls same method again.
            } else {
                String[] bits = file.getName().split(".");
                if (bits.length > 0 && bits[bits.length - 1].equalsIgnoreCase("jar")) {
                    // framework.getBundleContext().installBundle(file.toURI().toString());
                }

                // String ext = FilenameUtils.getExtension(file.getAbsolutePath());

                String basename = FilenameUtils.getBaseName(file.getName());

                if (pluginsList.contains(basename)) {
                    framework.getBundleContext().installBundle(file.toURI().toString());
                    System.out.println("File: " + file.getName());

                    System.out.println("Base >>>>>>>>>>>>> : " + basename);

                    pluginsList.remove(basename);
                }
            }
        }
    }

    public void plugins() {
        File plugins = new File("src/main/resources/plugins.txt");
        String fileName = plugins.getAbsolutePath();

        try (BufferedReader br = Files.newBufferedReader(Paths.get(fileName))) {

            // br returns as stream and convert it into a List
            pluginsList = br.lines().collect(Collectors.toList());

        } catch (IOException e) {
            e.printStackTrace();
        }

        pluginsList.forEach(System.out::println);
        addedPlugins = pluginsList.size();
    }

    private void loadScrBundle(Framework framework) throws URISyntaxException, BundleException {
        URL url = getClass().getClassLoader().getResource("org/apache/felix/scr/ScrService.class");
        if (url == null)
            throw new RuntimeException("Could not find the class org.apache.felix.scr.ScrService");
        String jarPath = url.toURI().getSchemeSpecificPart().replaceAll("!.*", "");
        System.out.println("Found declarative services implementation: " + jarPath);
        framework.getBundleContext().installBundle(jarPath).start();
    }
}

I have released a couple of first Early Access versions of Drombler FX - the modular application framework for JavaFX. 我已经发布了Drombler FX的几个第一个Early Access版本-JavaFX的模块化应用程序框架。

It's not based on Paul Bakker's work, but it's based on OSGi and Maven (POM-first), as well. 它不是基于Paul Bakker的工作,而是基于OSGi和Maven(POM优先)。 Maybe you find it useful. 也许您觉得它有用。 The application framework is Open Source. 该应用程序框架是开源的。

There is also a tutorial with a Getting Started page. 还有一个带有入门页面的教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM