我已经创建了我的第一个 OSGi 服务,并试图将它部署在 Apache Felix 上。之后查看系统控制台时,发现该服务未激活,并且出现了一些问题:
org.springframework.ws.client.core,version=[2.1,3) -- Cannot be resolved
org.springframework.ws.soap,version=[2.1,3) -- Cannot be resolved
org.springframework.ws.soap.axiom,version=[2.1,3) -- Cannot be resolved
org.springframework.ws.soap.saaj,version=[2.1,3) -- Cannot be resolved
所以我查看了哪个 jar 包含这些包,它来自 spring-ws-core-2.1.2.RELEASE.jar,它也是一个 OSGi 包。我也部署了那个,但是又出现了以下错误消息:
org.springframework.web.servlet,version=[3.1.0, 4.0.0) -- Cannot be resolved
再次依赖,这次是 spring-webmvc-3.2.17.RELEASE.jar。然而问题是这个不是 OSGi 包,那么我该如何解决这个问题呢?由于它是第三方库,所以我能想到的不多。
那么如何在 OSGi 容器中使用非捆绑 jar 包呢?
以及如何自动解决依赖关系树而不必手动解决所有问题?
最佳答案
我创建了 osgi-run项目来解决使用 Gradle 支持的标准 Maven 依赖解析(不幸的是,OBR 没有得到广泛支持)来解决捆绑依赖的这个问题。
然而,自从几年前 Spring 项目放弃对 OSGi 的支持以来,Spring jar 是一个可怕的噩梦。
理论上,使用 osgi-run,您应该能够使用以下 gradle 文件创建包含 spring-ws-core 包的 OSGi 环境:
plugins {
id "com.athaydes.osgi-run" version "1.5.1"
}
repositories {
mavenLocal()
jcenter()
}
dependencies {
osgiRuntime 'org.springframework.ws:spring-ws-core:2.1.1.RELEASE'
}
这依赖于 poms 中的信息是否一致。如果找到任何非捆绑包,它会自动转换为 OSGi 捆绑包(参见 wrapping jars)。
但是,这不起作用...... Gradle 可以打印 spring-ws-core jar 的依赖层次结构,这是我使用它时得到的:
+--- org.springframework.ws:spring-ws-core:2.1.1.RELEASE
| +--- org.springframework.ws:spring-xml:2.1.1.RELEASE
| | +--- org.springframework:spring-context:3.1.2.RELEASE
| | | +--- org.springframework:spring-aop:3.1.2.RELEASE
| | | | +--- aopalliance:aopalliance:1.0
| | | | +--- org.springframework:spring-asm:3.1.2.RELEASE
| | | | +--- org.springframework:spring-beans:3.1.2.RELEASE
| | | | | \--- org.springframework:spring-core:3.1.2.RELEASE
| | | | | +--- org.springframework:spring-asm:3.1.2.RELEASE
| | | | | \--- commons-logging:commons-logging:1.1.1
| | | | \--- org.springframework:spring-core:3.1.2.RELEASE (*)
| | | +--- org.springframework:spring-beans:3.1.2.RELEASE (*)
| | | +--- org.springframework:spring-core:3.1.2.RELEASE (*)
| | | +--- org.springframework:spring-expression:3.1.2.RELEASE
| | | | \--- org.springframework:spring-core:3.1.2.RELEASE (*)
| | | \--- org.springframework:spring-asm:3.1.2.RELEASE
| | +--- commons-logging:commons-logging:1.1.1
| | +--- org.springframework:spring-core:3.1.2.RELEASE (*)
| | \--- org.springframework:spring-beans:3.1.2.RELEASE (*)
| +--- org.springframework:spring-context:3.1.2.RELEASE (*)
| +--- org.springframework:spring-aop:3.1.2.RELEASE (*)
| +--- org.springframework:spring-oxm:3.1.2.RELEASE
| | +--- commons-lang:commons-lang:2.5
| | +--- org.springframework:spring-beans:3.1.2.RELEASE (*)
| | +--- org.springframework:spring-context:3.1.2.RELEASE (*)
| | \--- org.springframework:spring-core:3.1.2.RELEASE (*)
| +--- org.springframework:spring-web:3.1.2.RELEASE
| | +--- aopalliance:aopalliance:1.0
| | +--- org.springframework:spring-beans:3.1.2.RELEASE (*)
| | +--- org.springframework:spring-context:3.1.2.RELEASE (*)
| | \--- org.springframework:spring-core:3.1.2.RELEASE (*)
| +--- org.springframework:spring-webmvc:3.1.2.RELEASE
| | +--- org.springframework:spring-asm:3.1.2.RELEASE
| | +--- org.springframework:spring-beans:3.1.2.RELEASE (*)
| | +--- org.springframework:spring-context:3.1.2.RELEASE (*)
| | +--- org.springframework:spring-context-support:3.1.2.RELEASE
| | | +--- org.springframework:spring-beans:3.1.2.RELEASE (*)
| | | +--- org.springframework:spring-context:3.1.2.RELEASE (*)
| | | \--- org.springframework:spring-core:3.1.2.RELEASE (*)
| | +--- org.springframework:spring-core:3.1.2.RELEASE (*)
| | +--- org.springframework:spring-expression:3.1.2.RELEASE (*)
| | \--- org.springframework:spring-web:3.1.2.RELEASE (*)
| +--- wsdl4j:wsdl4j:1.6.1
| +--- commons-logging:commons-logging:1.1.1
| +--- org.springframework:spring-core:3.1.2.RELEASE (*)
| \--- org.springframework:spring-beans:3.1.2.RELEASE (*)
我认为依赖解析中可能存在一些错误,因为根据上面解析的依赖关系图,似乎 Spring 2 jar 与 Spring 3 jar 混合在一起!但是不...它是exactly right .
但无论如何,经过一番调查,我得到了这个工作......
调查未满足的捆绑需求
首先,我注意到 Spring AOP 没有解析,因为它的
org.aopalliance.aop
要求。显然,这应该来自 aopalliance jar(它不是一个捆绑包,至少不是 Maven Central/JCenter 中的那个)。
我在
runOsgi
中添加了这条指令 block 在 Gradle 文件中,以便我可以看到 osgi-run
把 jar 包成一个包:wrapInstructions {
printManifests = true
}
运行
gradle clean createOsgi
再次, list 被打印出来......它看起来像这样:--------------------------------- Manifest for aopalliance-1.0.jar ---------------------------------
Manifest-Version: 1.0
Bundle-SymbolicName: aopalliance
Bundle-ManifestVersion: 2
Bnd-LastModified: 1474120107912
Import-Package: org.aopalliance.aop
Require-Capability: osgi.ee;filter:="(&(osgi.ee=JavaSE)(version=1.3))"
Tool: Bnd-3.1.0.201512181341
Ant-Version: Apache Ant 1.5.4
Originally-Created-By: 1.4.2_01-b06 (Sun Microsystems Inc.)
Export-Package: org.aopalliance.aop,org.aopalliance.intercept;uses:="o
rg.aopalliance.aop"
Bundle-Version: 1.0.0
Bundle-Name: aopalliance
Created-By: 1.8.0_60 (Oracle Corporation)
----------------------------------------------------------------------------------------------------
请注意,有一个正确生成的
Bundle-Version
,但是这些包不是用那个版本导出的……通过添加下面的指令,我们可以强制用一个版本导出包:wrapInstructions {
printManifests = true
manifest( 'aopalliance.*' ) {
instruction 'Export-Package', '*;version=1.0'
}
}
现在,
Export-Package
list 中的说明是正确的:Export-Package: org.aopalliance.aop;version="1.0",org.aopalliance.inte
rcept;version="1.0";uses:="org.aopalliance.aop"
运行 OSGi 容器,我们看到 Spring AOP 包仍然没有解决,但现在唯一的问题是因为它的
(&(osgi.wiring.package=org.apache.commons.logging)(version>=1.1.1)(!(version>=2.0.0)))
不满足要求。commons.logging
jar 有一个已知问题(documented 在 osgi-run
README 页面中)...它声明了可选依赖项,使其难以自动包装。但此外,
commons.logging
jar 中有一个不正确的 Specification-Version
在 list 中。它说 1.0
而不是 1.1.1
,这就是 osgi-run
用于捆绑版本,所以 Bundle-Version
得到不正确的值。通过也强制
osgi-run
要导出具有正确版本的包,包装工作正常并且 Spring AOP 正确启动:manifest( /commons-logging.*/ ) {
instruction 'Import-Package', '!javax.servlet,!org.apache.*,*'
instruction 'Export-Package', '*;version=1.1.1'
}
现在,转到下一个问题,我们注意到
org.springframework.web
由于它对 (&(osgi.wiring.package=javax.servlet)(version>=2.4.0)(!(version>=4.0.0)))
的要求而无法解决.这很简单,将 servlet-api 包(由 Felix 提供)添加到 OSGi 运行时,它应该可以工作......只需在 Gradle 文件中添加这个依赖项:
osgiRuntime 'org.apache.felix:org.apache.felix.http.servlet-api:1.1.2'
现在,OSGi 环境启动,没有任何故障!
最终解决方案
这是安装的全套捆绑包:
ID|State |Level|Name
0|Active | 0|System Bundle (5.4.0)|5.4.0
1|Active | 1|aopalliance (1.0.0)|1.0.0
2|Active | 1|Commons Lang (2.5.0)|2.5.0
3|Active | 1|Jakarta Commons Logging (1.0.0)|1.0.0
4|Active | 1|Apache Felix Gogo Command (0.16.0)|0.16.0
5|Active | 1|Apache Felix Gogo Runtime (0.16.2)|0.16.2
6|Active | 1|Apache Felix Gogo Shell (0.12.0)|0.12.0
7|Active | 1|Apache Felix Servlet API (1.1.2)|1.1.2
8|Active | 1|Spring AOP (3.1.2.RELEASE)|3.1.2.RELEASE
9|Active | 1|Spring ASM (3.1.2.RELEASE)|3.1.2.RELEASE
10|Active | 1|Spring Beans (3.1.2.RELEASE)|3.1.2.RELEASE
11|Active | 1|Spring Context (3.1.2.RELEASE)|3.1.2.RELEASE
12|Active | 1|Spring Context Support (3.1.2.RELEASE)|3.1.2.RELEASE
13|Active | 1|Spring Core (3.1.2.RELEASE)|3.1.2.RELEASE
14|Active | 1|Spring Expression Language (3.1.2.RELEASE)|3.1.2.RELEASE
15|Active | 1|Spring Object/XML Mapping (3.1.2.RELEASE)|3.1.2.RELEASE
16|Active | 1|Spring Web (3.1.2.RELEASE)|3.1.2.RELEASE
17|Active | 1|Spring Web Servlet (3.1.2.RELEASE)|3.1.2.RELEASE
18|Active | 1|Spring Web Services Core (2.1.1.RELEASE)|2.1.1.RELEASE
19|Active | 1|Spring XML (2.1.1.RELEASE)|2.1.1.RELEASE
20|Active | 1|tomcat-servlet-api (8.0.0)|8.0.0
21|Active | 1|JWSDL (1.2.0)|1.2.0
如果您想尝试
osgi-run
,这是我使用的 Gradle 文件:plugins {
id "com.athaydes.osgi-run" version "1.5.1"
}
repositories {
mavenLocal()
jcenter()
}
dependencies {
osgiRuntime 'org.springframework.ws:spring-ws-core:2.1.1.RELEASE'
osgiRuntime 'org.apache.felix:org.apache.felix.http.servlet-api:1.1.2'
}
runOsgi {
wrapInstructions {
printManifests = true
manifest( 'aopalliance.*' ) {
instruction 'Export-Package', '*;version=1.0'
}
manifest( /commons-logging.*/ ) {
instruction 'Import-Package', '!javax.servlet,!org.apache.*,*'
instruction 'Export-Package', '*;version=1.1.1'
}
}
}
只需将其保存在
build.gradle
文件,然后运行 gradle createOsgi
在 build/osgi/run.sh
获取您的启动脚本.我将在
osgi-run
上工作尝试使此类问题自动解决,希望在下一个版本中,上面显示的第一个构建文件将毫不费力地工作。
关于java - Apache Felix 上的 OSGi 部署,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39405616/