【问题标题】:OSGI transitive dependencies levelOSGI 传递依赖级别
【发布时间】:2012-10-09 13:21:52
【问题描述】:

我目前正在尝试捆绑一个非 OSGI maven 项目。现在,我有类似的东西,

 <?xml version="1.0" encoding="UTF-8"?>
 <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>

<!-- The Basics -->
<artifactId>Project1</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<dependencies>
    <dependency>
        <groupId>internal.commons</groupId>
        <artifactId>internalcommons</artifactId>
        <version>1.5</version>
    </dependency>
    <dependency>
        <groupId>jdom</groupId>
        <artifactId>jdom</artifactId>
        <version>1.0</version>
    </dependency>
</dependencies> 
<parent>
    <groupId>internal</groupId>
    <artifactId>jar-project</artifactId>
    <version>1.0</version>
</parent>
<name>Project 1</name>

<build> 
    <directory>${basedir}/bundles</directory>
    <plugins>
        <plugin>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
                </archive>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.felix</groupId>
            <artifactId>maven-bundle-plugin</artifactId>
            <version>2.3.7</version>
            <extensions>true</extensions>
            <configuration>
                <instructions>
                    <Bundle-SymbolicName>${project.groupId}.${project.artifactId}</Bundle-SymbolicName>
                    <Bundle-Name>${project.name}</Bundle-Name>
                    <Bundle-Version>1.0</Bundle-Version>
                    <Import-Package>*;resolution:=optional</Import-Package>
                    <Embed-Dependency>*</Embed-Dependency>
                    <Embed-Transitive>true</Embed-Transitive>
                </instructions>
            </configuration>
        </plugin>       
    </plugins>  
</build>

然而,我的 nonOSGI 包依赖于各种不同的 jar,其中一些 jar 也有依赖关系。当我捆绑 nonOSGI maven 项目时,我只能获取此捆绑包的直接依赖项,当我使用 bundleall 时,我收到与 xml-apis:xml-apis:jar:2.6.2 相关的错误。如果没有其他选项但依赖项太多,我可以创建所有依赖项的捆绑包。

任何帮助将不胜感激!

【问题讨论】:

  • 您有什么与 xml-apis 相关的错误?你能展示你的pom吗?
  • 添加了 pom,如果我运行当前使用 bundle:bundle 粘贴的 pom,我得到运行时错误原因:java.lang.ClassNotFoundException: com.sun.org.apache.xml.internal .serialize.OutputFormat 未找到。 Bundle:bundleall 导致无法在项目 Project1 上执行目标 org.apache.felix:maven-bundle-plugin:2.3.7:bundleall (default-cli):在 repoxml-apis:xml-apis:jar 中找不到工件:2.6.2:compile: 在 nexus 中找不到工件 xml-apis:xml-apis:jar:2.6.2
  • 如果可能的话,请用依赖项更新你的 pom,让我们进行试验。
  • 更新了 pom,另一个注意事项 internal.commons 本身有各种依赖项,我认为这就是问题所在。我正在进行运行时反射以使用捆绑包,有时依赖项是不存在。

标签: java apache maven osgi bundle


【解决方案1】:

嗯,

根据发布的堆栈跟踪,central repo 中没有 xml-apis:xml-apis:2.6.2 工件。我想应该是xerces:xmlParserAPIs:2.6.2。 这是更新的 pom,嵌入 deps 没有问题:

<?xml version="1.0" encoding="UTF-8"?>
<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>

    <!-- The Basics -->
    <groupId>internal</groupId>
    <artifactId>Project1</artifactId>
    <version>1.0</version>
    <packaging>bundle</packaging>

    <name>Project 1</name>

    <dependencies>
        <dependency>
            <groupId>jdom</groupId>
            <artifactId>jdom</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>xerces</groupId>
            <artifactId>xmlParserAPIs</artifactId>
            <version>2.6.2</version>
        </dependency>
    </dependencies> 

    <build> 
        <plugins>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <version>2.3.7</version>
                <extensions>true</extensions>
                <configuration>
                    <instructions>
                        <Bundle-SymbolicName>${project.groupId}.${project.artifactId}</Bundle-SymbolicName>
                        <Bundle-Name>${project.name}</Bundle-Name>
                        <Bundle-Version>1.0</Bundle-Version>
                        <Import-Package>*;resolution:=optional</Import-Package>
                        <Embed-Dependency>*</Embed-Dependency>
                        <Embed-Transitive>true</Embed-Transitive>
                    </instructions>
                </configuration>
            </plugin>       
        </plugins>  
    </build>

</project>

注意使用packaging=bundle。构建只需执行mvn package

虽然 maven-bundle-plugin 有助于嵌入依赖项,但如果可以实现项目的更高模块化,我强烈建议您不要这样做。有带有预构建 osgi 包的 repos,例如:apache servicemix bundlesspring enterprise bundle repository 等。

【讨论】:

  • 使用 *;resolution:=optional 是一种不好的做法,在应用程序开始运行很长时间后,它通常会在运行时出现类未找到异常!此外,通过盲目地嵌入所有内容,您将增加 Import-Package 路径中的包数量,这在编译时无法解决。所以使用 *true 也是一个坏主意......不幸的是,除了手工完成所有肮脏的工作之外,没有其他解决方案.
  • 为什么你只是没有尝试回答最初的问题,即 osgify 你所有的 hundereds 部门?这绝对是更好的解决方案,特别是如果您有足够的时间和金钱并且其他部门对 osgi 友好(没有不同的线程上下文类加载问题,总是通过显式提供的类加载器加载类,没有 spi 使用等)
猜你喜欢
  • 2014-12-20
  • 1970-01-01
  • 2017-07-27
  • 1970-01-01
  • 2017-03-11
  • 2019-11-04
  • 2017-05-02
  • 2018-07-06
相关资源
最近更新 更多