将SIGAR .so和.dll文件嵌入到OSGI包

时间:2016-07-18 18:14:37

标签: maven osgi karaf sigar

我想将.so文件和.dll文件嵌入到OSGI包中。我试过这个: 我将所有文件复制到src\main\resources\lib

然后我添加了这个依赖:

        <dependency>
            <groupId>org.hyperic</groupId>
            <artifactId>sigar</artifactId>
            <version>1.6.5.132-6</version>
            <type>jar</type>
        </dependency>

的pom.xml

<plugin>
    <groupId>org.apache.felix</groupId>
    <artifactId>maven-bundle-plugin</artifactId>
    <version>3.0.0</version>
    <extensions>true</extensions>
    <configuration>
        <instructions>
            <Embed-Dependency>sigar;scope=compile|runtime</Embed-Dependency>
            <Embed-Dependency>sigar;scope=compile|runtime</Embed-Dependency>
                            <Include-Resource>
                                ${basedir}/src/main/resources/lib/libsigar-amd64-linux.so,
                                ${basedir}/src/main/resources/lib/libsigar-x86-linux.so,
                                ${basedir}/src/main/resources/lib/libsigar-amd64-solaris.so,
                                ${basedir}/src/main/resources/lib/libsigar-universal64-macosx.dylib,
                                ${basedir}/src/main/resources/lib/sigar-amd64-winnt.dll
                            </Include-Resource>
                            <Bundle-NativeCode>
                                /libsigar-amd64-linux.so;
                                processor=x86_64; osname=Linux,
                                /libsigar-x86-linux.so;
                                processor=x86;
                                osname=Linux,
                                /libsigar-amd64-solaris.so;
                                processor=x86_64; osname=Solaris,
                                /libsigar-universal64-macosx.dylib;
                                osname=macosx; processor=x86;
                                processor=x86_64,
                                /sigar-amd64-winnt.dll;
                                processor=x86_64; osname=Windows
                            </Bundle-NativeCode>
            <Bundle-Activator>org.test.Activator</Bundle-Activator>
            <Bundle-Version>${project.version}</Bundle-Version>
            <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
        </instructions>
    </configuration>
</plugin>

当我将捆绑包运行到OSGI框架中时,我得到了:

no sigar-amd64-winnt.dll in java.library.path
org.hyperic.sigar.SigarException: no sigar-amd64-winnt.dll in java.library.path
        at org.hyperic.sigar.Sigar.loadLibrary(Sigar.java:172)

你能给我一些如何嵌入它们的建议吗?

更新: 我也试过了:

<plugin>
    <groupId>org.apache.felix</groupId>
    <artifactId>maven-bundle-plugin</artifactId>
    <version>3.0.0</version>
    <extensions>true</extensions>
    <configuration>
        <instructions>
            <Embed-Dependency>sigar;scope=compile|runtime</Embed-Dependency>
            <Include-Resource>
                ${maven-resources},
                lib=src\main\resources\lib
            </Include-Resource>
            <Bundle-NativeCode>
                lib/sigar-amd64-winnt.dll;
                processor=x86_64; osname=win32
            </Bundle-NativeCode>
            <Import-Package>
                org.osgi.framework;version="[1.7,2)",
                org.osgi.util.tracker;version="[1.5,2)"
            </Import-Package>
            <Bundle-Activator>org.test.Activator</Bundle-Activator>
            <Bundle-Version>${project.version}</Bundle-Version>
            <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
         </instructions>
    </configuration>
</plugin>

我得到了这个部分结果:

org.osgi.framework.BundleException: Unable to resolve io.lambdacube.osgi.sigar [31](R 31.0): missing requirement [io.lambdacube.osgi.sigar [31](R 31.0)] osgi.native; (|(&(osgi.native.osname~=linux)(osgi.native.processor~=x86_64))(&(osgi.native.osname~=linux)(osgi.native.processor~=x86))(&(osgi.native.osname~=solaris)(osgi.native.processor~=x86_64))(&(osgi.native.osname~=macosx)(|(osgi.native.processor~=x86)(osgi.native.processor~=x86_64)))) Unresolved requirements: [[io.lambdacube.osgi.sigar [31](R 31.0)] osgi.native; (|(&(osgi.native.osname~=linux)(osgi.native.processor~=x86_64))(&(osgi.native.osname~=linux)(osgi.native.processor~=x86))(&(osgi.native.osname~=solaris)(osgi.native.processor~=x86_64))(&(osgi.native.osname~=macosx)(|(osgi.native.processor~=x86)(osgi.native.processor~=x86_64))))]
    at org.apache.felix.framework.Felix.resolveBundleRevision(Felix.java:4111)[org.apache.felix.framework-5.4.0.jar:]
    at org.apache.felix.framework.Felix.startBundle(Felix.java:2117)[org.apache.felix.framework-5.4.0.jar:]
    at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:998)[org.apache.felix.framework-5.4.0.jar:]

有任何解决方案吗?

1 个答案:

答案 0 :(得分:2)

您需要使用标头Bundle-NativeCode来映射捆绑包中的本机库。

此外,已经有几个项目将sigar打包为osgi捆绑包。您可以看到例如this onehere