java - 我可以将第三方 jar 放在 karaf(任何特定文件夹)中以解决传递依赖关系吗?

标签 java maven packaging apache-karaf karaf

我有各种具有第三方库依赖项的自制项目。 我将它们捆绑到 OSGI 容器中,但无法解决我项目中的深度依赖关系。 现在我正在寻找 karaf 文件夹,我可以在其中放置我的库,以便 bundle 可以直接访问它们而不是安装它们。

此外,我也在使用 maven。

编辑: 在遵循你的“功能”解决方案之后,我能够生成包含传递依赖项的 list ,但问题是现在它也在寻找非常常见的 java 文件:例如:下面是相当大的依赖项列表:

bsh -- Cannot be resolved
com.fasterxml.jackson.annotation -- Cannot be resolved
com.fasterxml.jackson.core -- Cannot be resolved
com.fasterxml.jackson.databind -- Cannot be resolved
com.fasterxml.jackson.databind.annotation -- Cannot be resolved
com.fasterxml.jackson.databind.module -- Cannot be resolved
com.gargoylesoftware.htmlunit -- Cannot be resolved
com.gargoylesoftware.htmlunit.util -- Cannot be resolved
com.google.protobuf -- Cannot be resolved
com.ibm.uvm.tools -- Cannot be resolved
com.ibm.websphere.uow -- Cannot be resolved
com.ibm.wsspi.uow -- Cannot be resolved
com.jamonapi -- Cannot be resolved
com.jamonapi.utils -- Cannot be resolved
com.jayway.jsonpath -- Cannot be resolved
com.jcraft.jzlib -- Cannot be resolved
com.mysema.query.types -- Cannot be resolved
com.sun.javadoc -- Cannot be resolved and overwritten by Boot Delegation
com.sun.jdmk.comm -- Cannot be resolved and overwritten by Boot Delegation
com.sun.net.httpserver -- Cannot be resolved and overwritten by Boot Delegation
com.sun.tools.javadoc -- Cannot be resolved and overwritten by Boot Delegation
com.sun.xml.fastinfoset.sax -- Cannot be resolved and overwritten by Boot Delegation
com.sun.xml.fastinfoset.stax -- Cannot be resolved and overwritten by Boot Delegation
com.typesafe.config -- Cannot be resolved
groovy.lang -- Cannot be resolved
groovy.xml -- Cannot be resolved
javassist -- Cannot be resolved
javax.activation from org.apache.felix.framework (0)
javax.annotation from org.apache.felix.framework (0)
javax.crypto from org.apache.felix.framework (0)
javax.crypto.spec from org.apache.felix.framework (0)
javax.ejb -- Cannot be resolved
javax.el -- Cannot be resolved
javax.enterprise.concurrent -- Cannot be resolved
javax.enterprise.context -- Cannot be resolved
javax.enterprise.context.spi -- Cannot be resolved
javax.enterprise.event -- Cannot be resolved
javax.enterprise.inject -- Cannot be resolved
javax.enterprise.inject.spi -- Cannot be resolved
javax.enterprise.util -- Cannot be resolved

最佳答案

回答您的问题:您可以将所有依赖包放入 $KARAF_HOME/deploy 文件夹中,Karaf 会为您部署它们。

但是,这不是很方便,因为它是一个手动过程,不是由 Maven 驱动的。相反,看看 Karaf 的概念 feature repositories .您可以使用 Karaf maven plugin为您的包及其(传递)依赖项创建一个功能存储库。如果您需要将您的应用程序作为单个 Artifact 发布,您可以使用相同的插件创建一个 KAR 存档,这是一个 zip 文件,在一个独立的部署单元中包含功能存储库和所需的依赖项。

首先将模板 feature.xml 文件放入 src/main/feature:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<features xmlns="http://karaf.apache.org/xmlns/features/v1.3.0" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://karaf.apache.org/xmlns/features/v1.3.0 http://karaf.apache.org/xmlns/features/v1.3.0"
        name="My feature">
    <feature name="${project.artifactId}" version="${project.version}" description="Describe feature here">
         <!-- Add "self" to the list of dependencies -->
         <bundle>mvn:${project.groupId}/${project.artifactId}/${project.version}</bundle>
    </feature>
</features>

然后设置插件以根据您的 Maven 依赖项填充功能模板:

<plugin>
    <groupId>org.apache.karaf.tooling</groupId>
    <artifactId>karaf-maven-plugin</artifactId>
    <configuration>
        <includeTransitiveDependency>true</includeTransitiveDependency>
    </configuration>
    <executions>
        <execution>
            <id>generate-features-descriptor</id>
            <goals>
                <goal>features-generate-descriptor</goal>
            </goals>
        </execution>
    </executions>
</plugin>

构建您的项目将在本地 Maven 存储库中创建一个额外的 Maven Artifact 以及您的 bundle jar:xxx-features.xml 您可以使用 feature:repo-add 命令让本地 Karaf 知道您的功能存储库。然后使用 feature:install 命令添加您的功能。这将启动您的包及其所有声明的(传递的)Maven 依赖项。

编辑:

正如您在评论中提到的,您的某些(全部?)依赖项是普通 JAR,而不是 OSGi 包,可能您最好 embedding使用 maven-bundle-plugin 将那些非 OSGi 依赖项放入您自己的包中。但这可能非常乏味。大多数非 OSGi JAR 都有包导入,这些导入要么在运行时根本不使用,要么在您的特定使用场景中不使用。为了避免将 OSGi 依赖项列表扩展到传递性 Maven 依赖项列表之外,您必须防止将那些“继承的”包添加到您自己的包的 MANIFEST 中的包导入列表中。例如,我有一个使用 httl 模板库的包,它又依赖于 Javassist。 OSGi 包也不是。因此,我同时嵌入并禁止导入在 httl 或 javassist 代码中声明但在运行时不需要的包:

<plugin>
    <groupId>org.apache.felix</groupId>
    <artifactId>maven-bundle-plugin</artifactId>
    <extensions>true</extensions>
    <configuration>
        <instructions>
            <Embed-Dependency>*;scope=compile|runtime;inline=false;artifactId=httl|javassist</Embed-Dependency>
            <Embed-Transitive>false</Embed-Transitive>
            <Import-Package>
                !com.alibaba.*,
                !com.thoughtworks.*,
                !javassist.*,
                !net.htmlparser.*,
                !org.apache.commons.logging.*,
                !org.codehaus.jackson.*,
                !org.joda.convert.*,
                !com.sun.jdi.*,
                !javax.servlet.*,
                *
            </Import-Package>
        </instructions>
    </configuration>
</plugin>

关于java - 我可以将第三方 jar 放在 karaf(任何特定文件夹)中以解决传递依赖关系吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34634109/

相关文章:

java - DataOutputStream 给我一个 'java.io.IOException: unexpected end of stream' ?

ubuntu - 如何在 PPA 中发布 i386 和 amd64 中的闭源应用程序?

java - 添加 gwteventservice 作为 Maven 依赖项会破坏项目

java - 如何将外部文件夹添加到类路径?

java - Maven war 插件问题

mobile - 使用 JAD 文件为不同操作系统版本打包 Blackberry 应用程序

c# - 我有一个需要打包的 C# WPF 应用程序。有什么建议么?

java - 将新值附加到 javax.json.JsonObject

java - 为什么jsp在将Jsp转换为Servlet时创建PageContext和JspWriter的两个引用

java - 在 Jython 中,我可以创建一个实现 Java 接口(interface)的内联匿名类吗?