简体   繁体   English

将外部库集成到OSGI

[英]Integrating external library into OSGI

I have been trying to fix this issue for several days now, but i seem stuck somehow. 我已经尝试解决此问题已有好几天了,但是我似乎还是被卡住了。 I want to develop a OSGi bundle, and my implementation requires an external library (that has some more dependencies). 我想开发一个OSGi包,而我的实现需要一个外部库(具有更多的依赖项)。 The situation is as follows: My project is using gradle and i got a simple code snippet to compile, but it crashes at runtime when the bundle is launched ( i am using apache felix), giving me an exception complaining about unmet dependencies. 情况如下:我的项目正在使用gradle,并且我得到了一个简单的代码片段进行编译,但是当捆绑包启动时,它在运行时崩溃(我使用的是apache felix),这使我有一个抱怨未满足的依赖项的异常。 But all libraries are present in the jar bundle, and i added them into the classpath. 但是所有库都存在于jar捆绑包中,我将它们添加到了classpath中。 The missing package org.ethereum.facade is listed within Private-Packages. 缺少的包org.ethereum.facade列在Private-Packages中。 I read a lot of questions with similar problems, but none of the silutions proposed seems to solve my problem. 我读了很多类似问题的问题,但是提出的所有解决方案似乎都无法解决我的问题。

I am posting my gradle.build files 我正在发布gradle.build文件

// ETHEREUM NODE
configurations.create('embed')
repositories {
  mavenLocal()
    mavenCentral()
    maven {
        url "http://dl.bintray.com/ethereum/maven"
     }
}
dependencies {
 // This will compile the openMuc Framework and place the result into the build classpath:
 compile project(':openmuc-core-api')
// This will place the etherum libraries into the classpath
    compile ("org.ethereum:ethereumj-core:latest.release")
    embed ("org.ethereum:ethereumj-core:latest.release")
}
jar {
    manifest {
        name = 'OpenMUC App - EthereumNode'
         instruction 'Bundle-ClassPath', 'lib/ethereumj-core-1.4.2-RELEASE.jar,.'
}
 into('lib') {
    from configurations.embed
  }
}

The resulting jar: 产生的罐子:

[MANIFEST openmuc-app-ethereumNode-0.16.0.jar]
Bnd-LastModified                         1490003658351                           
Bundle-ClassPath                         lib/ethereumj-core-1.4.2-RELEASE.jar,.  
Bundle-ManifestVersion                   2                                       
Bundle-Name                              OpenMUC App - EthereumNode              
Bundle-SymbolicName                      org.openmuc.framework.openmuc-app-ethereumNode
Bundle-Version                           0.16.0                                  
Created-By                               1.8.0_25 (Oracle Corporation)           
Export-Package                           org.openmuc.framework.app.ethereumNode;version="0.16.0";uses:="javax.xml.bind,org.osgi.service.component"
Import-Package                           javax.xml.bind,org.osgi.service.component;version="[1.2,2)",org.slf4j;version="[1.7,2)",org.ethereum.facade
Manifest-Version                         1.0                                     
Private-Package                          genesis,org.ethereum,org.ethereum.cli,org.ethereum.config,org.ethereum.config.blockchain,org.ethereum.config.net,org.ethereum.core,org.ethereum.core.genesis,org.ethereum.crypto,org.ethereum.crypto.cryptohash,org.ethereum.crypto.jce,org.ethereum.datasource,org.ethereum.datasource.inmem,org.ethereum.datasource.leveldb,org.ethereum.datasource.mapdb,org.ethereum.db,org.ethereum.db.index,org.ethereum.facade,org.ethereum.json,org.ethereum.jsonrpc,org.ethereum.listener,org.ethereum.manager,org.ethereum.mine,org.ethereum.net,org.ethereum.net.client,org.ethereum.net.dht,org.ethereum.net.eth,org.ethereum.net.eth.handler,org.ethereum.net.eth.message,org.ethereum.net.message,org.ethereum.net.p2p,org.ethereum.net.rlpx,org.ethereum.net.rlpx.discover,org.ethereum.net.rlpx.discover.table,org.ethereum.net.server,org.ethereum.net.shh,org.ethereum.net.submit,org.ethereum.net.swarm,org.ethereum.net.swarm.bzz,org.ethereum.samples,org.ethereum.solidity,org.ethereum.solidity.compiler,org.ethereum.sync,org.ethereum.trie,org.ethereum.util,org.ethereum.util.blockchain,org.ethereum.validator,org.ethereum.vm,org.ethereum.vm.program,org.ethereum.vm.program.invoke,org.ethereum.vm.program.listener,org.ethereum.vm.trace,lib
Require-Capability                       osgi.ee;filter:="(&(osgi.ee=JavaSE)(version=1.7))"
Service-Component                        OSGI-INF/org.openmuc.framework.app.ethereumNode.EthereumNode.xml
Tool                                     Bnd-3.0.0.201509101326                  

[IMPEXP]
Import-Package
  javax.xml.bind                         
  org.ethereum.facade                    
  org.osgi.service.component             {version=[1.2,2)}
  org.slf4j                              {version=[1.7,2)}
Export-Package
  org.openmuc.framework.app.ethereumNode {version=0.16.0}

[USES]
org.openmuc.framework.app.ethereumNode   java.lang
                                         javax.xml.bind
                                         org.ethereum.facade
                                         org.osgi.service.component
                                         org.slf4j

[USEDBY]
java.lang                                org.openmuc.framework.app.ethereumNode
javax.xml.bind                           org.openmuc.framework.app.ethereumNode
org.ethereum.facade                      org.openmuc.framework.app.ethereumNode
org.osgi.service.component               org.openmuc.framework.app.ethereumNode
org.slf4j                                org.openmuc.framework.app.ethereumNode

[COMPONENTS]
OSGI-INF/org.openmuc.framework.app.ethereumNode.EthereumNode.xml
<?xml version="1.0" encoding="UTF-8"?>
<component name="org.openmuc.framework.app.ethereumNode.EthereumNode">
  <implementation class="org.openmuc.framework.app.ethereumNode.EthereumNode"/>
</component>


[LIST]
META-INF
  MANIFEST.MF extra='\uFFFD\uFFFD\u0000\u0000'
OSGI-INF
  org.openmuc.framework.app.ethereumNode.EthereumNode.xml
lib
  aopalliance-1.0.jar
  commons-codec-1.10.jar
  commons-collections4-4.0.jar
  commons-lang3-3.4.jar
  commons-logging-1.2.jar
  config-1.2.1.jar
  core-1.53.0.0.jar
  ethereumj-core-1.4.2-RELEASE.jar
  guava-16.0.1.jar
  jackson-annotations-2.5.0.jar
  jackson-core-2.5.1.jar
  jackson-core-asl-1.9.13.jar
  jackson-databind-2.5.1.jar
  jackson-mapper-asl-1.9.13.jar
  java-util-1.8.0.jar
  javassist-3.15.0-GA.jar
  json-io-2.4.1.jar
  json-simple-1.1.1.jar
  jsr305-3.0.0.jar
  leveldb-0.7.jar
  leveldb-api-0.7.jar
  leveldbjni-all-1.18.3.jar
  logback-classic-1.1.7.jar
  logback-core-1.1.7.jar
  mapdb-2.0-beta12.jar
  netty-all-4.0.30.Final.jar
  prov-1.53.0.0.jar
  slf4j-api-1.7.20.jar
  solcJ-all-0.4.8.jar
  spring-aop-4.2.0.RELEASE.jar
  spring-beans-4.2.0.RELEASE.jar
  spring-context-4.2.0.RELEASE.jar
  spring-core-4.2.0.RELEASE.jar
  spring-expression-4.2.0.RELEASE.jar
  spring-jdbc-4.2.0.RELEASE.jar
  spring-orm-4.2.0.RELEASE.jar
  spring-tx-4.2.0.RELEASE.jar
org
org/openmuc
org/openmuc/framework
org/openmuc/framework/app
org/openmuc/framework/app/ethereumNode
  EthereumNode.class

The exception i encounter when the bundle is loaded: 加载捆绑包时遇到的异常:

ERROR: Bundle org.openmuc.framework.openmuc-app-ethereumNode [3] Error starting file:/Volumes/UserData/Documents/FH/MasterThesis_MatthiasPosch/java/openmuc/framework/bundle/openmuc-app-ethereumNode-0.16.0.jar (org.osgi.framework.BundleException: Unable to resolve org.openmuc.framework.openmuc-app-ethereumNode [3](R 3.0): missing requirement [org.openmuc.framework.openmuc-app-ethereumNode [3](R 3.0)] osgi.wiring.package; (osgi.wiring.package=org.ethereum.facade) Unresolved requirements: [[org.openmuc.framework.openmuc-app-ethereumNode [3](R 3.0)] osgi.wiring.package; (osgi.wiring.package=org.ethereum.facade)])
org.osgi.framework.BundleException: Unable to resolve org.openmuc.framework.openmuc-app-ethereumNode [3](R 3.0): missing requirement [org.openmuc.framework.openmuc-app-ethereumNode [3](R 3.0)] osgi.wiring.package; (osgi.wiring.package=org.ethereum.facade) Unresolved requirements: [[org.openmuc.framework.openmuc-app-ethereumNode [3](R 3.0)] osgi.wiring.package; (osgi.wiring.package=org.ethereum.facade)]
    at org.apache.felix.framework.Felix.resolveBundleRevision(Felix.java:4112)
    at org.apache.felix.framework.Felix.startBundle(Felix.java:2118)
    at org.apache.felix.framework.Felix.setActiveStartLevel(Felix.java:1372)
    at org.apache.felix.framework.FrameworkStartLevelImpl.run(FrameworkStartLevelImpl.java:308)
    at java.lang.Thread.run(Thread.java:745)

So it seems all libraries are in place in the lib folder, but still the bundle does not work. 因此,似乎所有库都在lib文件夹中,但该捆绑包仍然无法使用。 I tried to play with the code, but any reference to the included libraries (eg. spring) will lead to a similar exception. 我尝试使用该代码,但是对所包含库的任何引用(例如spring)都会导致类似的异常。 Why? 为什么?

My solution was to use the osgi-run plugin for gradle instead of the original osgi plugin. 我的解决方案是将osgi-run插件用于gradle,而不是原始的osgi插件。 Declaring my dependencies as SystemLibrary systemLib ("org.ethereum:ethereumj-core:1.2.0-RELEASE") resolved the issues and i am able to use the library without exceptions and errors. 将我的依赖项声明为SystemLibrary systemLib ("org.ethereum:ethereumj-core:1.2.0-RELEASE")解决了问题,并且我能够无例外地使用库。

In your Manifest you import the package org.ethereum.facade . 在清单中,导入包org.ethereum.facade This can only work if there is also a bundle that exports this package. 仅当还有捆绑包可以导出此程序包时,此方法才有效。 If the package is purely internal then you should set it as private package and not import it. 如果软件包是纯内部软件包,则应将其设置为私有软件包,而不要导入。

If the package might be needed from the outside then make sure you have an import and an export for it in your bundle. 如果可能需要从外部使用该包装,请确保您的包装中有该包装的进口和出口。 If you have not configured the import manually then for some reason the build seems to think you need it and it is not present locally in your bundle. 如果您尚未手动配置导入,则出于某种原因,该构建似乎会认为您需要它,并且它不在本地包中。

How do you create the Manifest? 您如何创建清单? Do you use bnd for this? 您为此使用bnd吗? If yes then maybe the manual change in bundle classpath is not recognized by it. 如果是,则可能无法识别包类路径中的手动更改。

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

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