【问题标题】:Retrieving every dependencies with Karaf Maven Plugin and create Karaf Archive使用 Karaf Maven 插件检索每个依赖项并创建 Karaf 存档
【发布时间】:2023-03-19 06:10:02
【问题描述】:

我正在使用 ServiceMix 6.1.0(包含 Karaf 3.0.5)并且我有一个 Maven 项目。我想使用karaf-maven-plugin 创建一个 Karaf 存档 (.kar)。

我了解使用 Karaf 存档的目标:检索每个依赖项,以便 .kar 可以部署在离线环境中。嗯...我以为我已经理解了...在创建 .kar 并部署它之后,我收到一个错误:

Error executing command: Can't install feature karafMavenPlugin/0.0.0:
Could not start bundle mvn:org.testng/testng/6.8.8 in feature(s) karafMavenPlugin-0.0.1-SNAPSHOT: 
Unresolved constraint in bundle org.testng [371]: 
Unable to resolve 371.0: 
missing requirement [371.0] osgi.wiring.package; (osgi.wiring.package=org.junit)

这里是pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>test</groupId>
  <artifactId>karafMavenPlugin</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>kar</packaging>
  <name>TestKarafMavenPlugin</name>

    <build>
        <plugins>
            <!-- Plugin to create .kar -->
            <plugin>
                <groupId>org.apache.karaf.tooling</groupId>
                <artifactId>karaf-maven-plugin</artifactId>
                <version>3.0.5</version>
                <extensions>true</extensions>
                <configuration>
                    <aggregateFeatures>true</aggregateFeatures>
                    <includeTransitiveDependency>true</includeTransitiveDependency>
                </configuration>
            </plugin>
        </plugins>
        <pluginManagement>
            <plugins>
                <!--This plugin's configuration is used to store Eclipse m2e settings 
                    only. It has no influence on the Maven build itself. -->
                <plugin>
                    <groupId>org.eclipse.m2e</groupId>
                    <artifactId>lifecycle-mapping</artifactId>
                    <version>1.0.0</version>
                    <configuration>
                        <lifecycleMappingMetadata>
                            <pluginExecutions>
                                <pluginExecution>
                                    <pluginExecutionFilter>
                                        <groupId>
                                            org.apache.karaf.tooling
                                        </groupId>
                                        <artifactId>
                                            karaf-maven-plugin
                                        </artifactId>
                                        <versionRange>
                                            [3.0.5,)
                                        </versionRange>
                                        <goals>
                                            <goal>
                                                features-generate-descriptor
                                            </goal>
                                        </goals>
                                    </pluginExecutionFilter>
                                    <action>
                                        <ignore></ignore>
                                    </action>
                                </pluginExecution>
                            </pluginExecutions>
                        </lifecycleMappingMetadata>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.8.8</version>
        </dependency>
    </dependencies>
</project>

这是生成的feature.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<features xmlns="http://karaf.apache.org/xmlns/features/v1.2.1" name="karafMavenPlugin">
    <feature name="karafMavenPlugin" version="0.0.1-SNAPSHOT" description="TestKarafMavenPlugin">
        <bundle>mvn:org.testng/testng/6.8.8</bundle>
        <bundle>wrap:mvn:org.beanshell/bsh/2.0b4</bundle>
        <bundle>mvn:com.beust/jcommander/1.27</bundle>
    </feature>
</features>

为什么没有检索到 JUnit 依赖项?

我的猜测是karaf-maven-plugin 不会检索pom.xml 中的依赖关系,它们是&lt;optional&gt;&lt;provided&gt;。实际上,JUnit 是 TestNG 的依赖项,这里是 TestNG 的 pom.xml(仅依赖项):

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

  <modelVersion>4.0.0</modelVersion>
  <groupId>org.testng</groupId>
  <artifactId>testng</artifactId>
  <packaging>jar</packaging>
  <name>TestNG</name>
  <version>6.8.8</version>
  <description>TestNG is a testing framework.</description>
  <url>http://testng.org</url>

  <dependencies>
    <dependency>
      <groupId>org.apache.ant</groupId>
      <artifactId>ant</artifactId>
      <version>1.7.0</version>
      <optional>true</optional>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.10</version>
      <optional>true</optional>
    </dependency>

    <dependency>
      <groupId>org.beanshell</groupId>
      <artifactId>bsh</artifactId>
      <version>2.0b4</version>
<!--
      <scope>provided</scope>
-->
    </dependency>

    <dependency>
      <groupId>com.google.inject</groupId>
      <artifactId>guice</artifactId>
      <version>2.0</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>com.beust</groupId>
      <artifactId>jcommander</artifactId>
      <version>1.27</version>
    </dependency>
      <dependency>
        <groupId>org.yaml</groupId>
        <artifactId>snakeyaml</artifactId>
        <version>1.12</version>
      <optional>true</optional>
      </dependency>
  </dependencies>    
</project>

我们可以看到,不是&lt;optional&gt;也不是&lt;provided&gt;的每一个依赖都被karaf-maven-plugin检索到了。

所以...我错了吗?这是预期的行为吗?如果是这种情况,有没有办法向karaf-maven-plugin指示下载每个依赖项?

【问题讨论】:

    标签: osgi dependency-management apache-karaf karaf apache-servicemix


    【解决方案1】:

    如 maven 文档中关于 scope 的描述:

    提供

    这很像编译,但表示您希望 JDK 或容器在运行时提供依赖项。例如, 在为 Java Enterprise Edition 构建 Web 应用程序时,您 将设置对 Servlet API 和相关 Java EE API 的依赖关系 到提供的范围,因为 Web 容器提供了这些类。 此范围仅在编译和测试类路径上可用, 并且不传递。

    'provided' 不可传递:通过 api 请求 maven 依赖项时,提供的依赖项不会被检索。

    因此,为了使用 karaf-maven-plugin,您必须在 pom 中显式添加此依赖项。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-16
      • 1970-01-01
      • 2015-06-21
      • 2018-02-28
      • 1970-01-01
      • 2016-04-13
      • 2019-03-29
      • 1970-01-01
      相关资源
      最近更新 更多