如何在 Scala 中编码我的包,然后将其部署到 OSGI 容器中?
我应该先将其编译为“java”,还是可以将 scala 直接部署到 OSGI 中并使用某种 bundle 来识别它?
任何指示都会很棒。 目前,我使用 Apache Felix 作为我的 osgi 容器,但只需对通用概念进行简单的解释就足以让我开始。
最佳答案
感谢大家的回答,你们引导我找到了解决方案!我将在这里用更简单的术语为更广泛的受众描述它。
目标:用 scala 编写代码,部署到 OSGi。
使用的工具:
- Equinox OSGi 实现
- Eclipse Helios 3.6,
- 斯卡拉2.9
程序
- 安装Scala IDE对于 Eclipse。查找适用于 Scala 2.9 和 Eclipse 3.6 的版本
- 在 Eclipse 中创建新的
Scala 项目
。 右键单击项目并选择:
配置 -> 转换为插件项目...,将项目转换为 OSGi 包
现在,下一部分是我陷入困境的地方。您看,现在我们需要将此 bundle (我们的项目)部署到 OSGi 环境。然而,我们缺少必须位于 OSGi 容器中的 Scala 类(或包含这些类的包)才能提供我们在包中使用的所有 Scala 包 API。不幸的是,找到“Scala 包”并不那么容易。之后looking around事实证明,由于某种原因,Scala 包实际上位于 Sonatype Maven Repository 中.
从 Sonatype Maven Repository 中的适当位置下载
scala-library-2.9.1.jar
,并将其部署(通过对您来说最方便的方式)到您的 OSGi 容器。调整你的 list 文件以需要 Scala 包(我很确定这是一个包依赖项(即 Require-Bundle)实际上非常安全的地方 - 毕竟,你永远不会运行你的没有 Scala 的 Scala 代码!):
Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: Scala Hello Bundle-SymbolicName: com.test.scala.hello Bundle-Version: 1.0.0.qualifier Bundle-Vendor: drozzy Import-Package: org.osgi.framework;version="1.5.0" Bundle-Activator: com.test.scala.hello.Activator Require-Bundle: scala-library;bundle-version="2.9.1"
现在,您可以在 Scala 中编写捆绑激活器(哇哦!):
//Activator.scala package com.test.scala.hello import java.lang.System import org.osgi.framework.BundleActivator import org.osgi.framework.BundleContext class Activator extends BundleActivator { def start(context: BundleContext) { System.out.println("Hello world from scala!"); } def stop(context: BundleContext){} }
将您的项目作为 bundle 部署到 OSGi 容器,并留意“Hello world from scala!”消息。
关于OSGI 容器中的 Scala?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5052706/