`
nbaertuo
  • 浏览: 75296 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

felix学习二-发布classpath为bundle

阅读更多

    这次我们需要将classpath中的jar发布成bundle。

    我们在Main中找到main这个启动函数,同样可以发现loadSystemProperties这个配置加载函数,他负责加载config.properties中的felix一个配置属性。其中felix.auto.start.1是bundle物理路径的配置参数。如果我们想启动某一个bundle可以通过修改该属性值实现。现在我们要做的是自动让eclipse工程中classpath里面的jar发布为felix中的bundle。实现也是考虑修改felix.auto.start.1的值。所以我们在loadSystemProperties方法后面增加一些代码,如下:

 

// jar
  String jarBundles = (String) props.get("felix.auto.start.1");

  String[] classpathJars = System.getProperty("java.class.path").split(
    ";");
  for (String location : classpathJars) {

   if (!location.contains("test")) {
    String jarName = location
      .substring(location.lastIndexOf("\\") + 1);
    if (jarName.indexOf("jar") > 0) {
      if (jarName.contains("org.apache.felix.framework")) {
      continue;
     }
     if (!jarBundles.contains(jarName)) {
      location.replaceAll("/", "\\");
      jarBundles = jarBundles + " file:" + location;
     }
    } else {
     // 文件夹方式
     File manifestFile = new File(location+ "/META-INF/Manifest.mf");
     if (manifestFile.exists()) {
      jarBundles = jarBundles + " reference:file:" + location;
     }
    }

   }
  }

  props.setProperty("felix.auto.start.1", jarBundles);

 

现在我们可以将classpath中的jar和工程都发布为bundle啦。可以随便写一个测试用例。在main的pom.xml中添加一个依赖。启动完后ps一下你会发现这个符合bundle的依赖已经发布成bundle啦。

 

  附上修改后的Main.java。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics