【问题标题】:Not generating OSGI-INF folder不生成 OSGI-INF 文件夹
【发布时间】:2013-06-29 07:16:16
【问题描述】:

我是 OSGI 新手。在生成的 jar 文件中获取 OSGI-INF 文件夹时遇到了麻烦。 我需要有如下文件夹结构

  • 元信息
  • OSGI-INF
  • com.mine.cq

我正在使用 Eclipse 和 m2e 插件。当我运行我的项目时,我得到了 BUILD SUCCESS。我在生成的 jar 文件中得到了以下文件夹结构。

  • 元信息
  • com.mine.cq

这是我的 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>com.mine.cq</groupId>
  <artifactId>mineCore</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>mineCore</name>
  <url>http://maven.apache.org</url>

  <properties>
        <file.encoding>utf-8</file.encoding>
    </properties>
   <build>
        <defaultGoal>install</defaultGoal>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-enforcer-plugin</artifactId>
                <version>1.0-alpha-3</version>
                <executions>
                    <execution>
                        <id>enforce-java</id>
                        <goals>
                            <goal>enforce</goal>
                        </goals>
                        <configuration>
                            <rules>
                                <requireMavenVersion>
                                    <message>Project must be built with Maven 2.0.7 or higher</message>
                                    <version>2.0.7</version>
                                </requireMavenVersion>
                                <requireJavaVersion>
                                    <message>Project must be compiled with Java 5 or higher</message>
                                    <version>1.5.0</version>
                                </requireJavaVersion>
                            </rules>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <version>1.4.3</version>
                <extensions>true</extensions>
                <configuration>
                    <instructions>
                        <Export-Package>
                            com.mine.cq.mineCore.*
                        </Export-Package>
                        <Import-Package>
                            *;resolution:=optional,
                            javax.servlet;version=2.4,
                            javax.servlet.http;version=2.4
                        </Import-Package>
                        <Embed-Dependency>   
                        </Embed-Dependency>
                        <Embed-Transitive>true</Embed-Transitive>
                        <Include-Resource>{maven-resources}</Include-Resource>
                        <Sling-Bundle-Resources>/var/classes</Sling-Bundle-Resources>
                    </instructions>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-scm-plugin</artifactId>
                <version>1.0</version>
                <configuration>
                    <goals>install</goals>
                </configuration>
            </plugin>
        </plugins>
    </build>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

为什么 OSGI-INF 文件夹不在 .jar 文件中?我需要在 OSGO-INF 文件夹中设置一些信息,因为我必须将我的组件注册为 OSGI 服务。 请指导我完成它。

【问题讨论】:

  • 在导出 JAR 文件时,是否选择了 OSGI-INF 文件夹。默认不选中。
  • 生成的 jar 没有 OSGI-INF 文件夹。如何选择 osgi-inf 文件夹?我需要在 pom.xml 中添加任何插件来生成 OSGI-INF 文件夹吗?

标签: java maven-2 apache-felix osgi-bundle


【解决方案1】:

虽然已经很晚了,但我会发布关于这个问题的 2 美分以供将来参考。

正如已经指出的,如果您按照Maven bundle plugin documentation 中的说明进行操作,您可以将捆绑包的“打包”设置为“jar”。

有一个小问题:使用该配置,您需要在插件配置中显式添加 &lt;exportScr&gt;true&lt;/exportScr&gt; 才能正确创建 SCR xml 文件(还要记住调整清单位置,因为在文档中该部分是缺席!)。

您可以在这里看到一个示例(这与您的完全不同,但我假设您可以轻松地在您的代码中减少它,如果您仍然感兴趣的话):

<?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>
    <groupId>com.massimobono.karaf.examples</groupId>
    <artifactId>user-example</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <packaging>jar</packaging>    
    <build>
        <plugins>
            <plugin>
                <!-- Here you specifiy that you want to use the manifest file generated by maven bundle plugin -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.0.2</version>
                <configuration>
                    <archive>
                        <manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
                    </archive>
                </configuration>
            </plugin>
            <!-- Here you generate the whole MANIFEST by using maven-bundle-plugin -->
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <version>3.2.0</version>
                <extensions>true</extensions> <!-- make sure this is present -->
                <executions>
                    <execution>
                        <id>bundle-manifest</id>
                        <phase>process-classes</phase>
                        <goals>
                            <goal>manifest</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <manifestLocation>${project.build.outputDirectory}/META-INF/</manifestLocation> <!-- make sure this is present! in the example of maven bundle plugin documentation, this piece is NOT present -->
                    <exportScr>true</exportScr> <!-- be sure to add this line as well -->
                    <supportedProjectTypes>
                        <supportedProjectType>jar</supportedProjectType>
                        <supportedProjectType>bundle</supportedProjectType>
                        <supportedProjectType>war</supportedProjectType>
                    </supportedProjectTypes>
                    <instructions>
                        <Bundle-SymbolicName>${project.groupId}.${project.artifactId}</Bundle-SymbolicName>
                        <_dsannotations>*</_dsannotations>
                    </instructions>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.osgi</groupId>
            <artifactId>org.osgi.core</artifactId>
            <version>6.0.0</version>
            <scope>provided</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.osgi/org.osgi.service.component.annotations -->
        <dependency>
            <groupId>org.osgi</groupId>
            <artifactId>org.osgi.service.component.annotations</artifactId>
            <version>1.3.0</version>
        </dependency>


    </dependencies>

</project>

【讨论】:

    【解决方案2】:

    您的 pom.xml 需要具有“bundle”而不是“jar”的包装类型。如果您希望包装类型为“jar”,请使用:

    http://felix.apache.org/site/apache-felix-maven-bundle-plugin-bnd.html#ApacheFelixMavenBundlePlugin%28BND%29-AddingOSGimetadatatoexistingprojectswithoutchangingthepackagingtype

    编辑:哦!那只是问题之一。另一个问题是我认为你不能生成 带有 maven-bundle-plugin 的 OSGI-INF。您需要自己在 src/main/resources 中创建 OSGI-INF 文件夹或使用生成 OSGI-INF 的插件。

    maven-scr-plugin 可以生成 OSGI-INF,但只有在使用 SCR 时才有用。 Maven SCR Plugin - Not generating OSGI-INF folder

    【讨论】:

    • 当我更改为捆绑包而不是 jar 时,我收到“项目构建错误未知打包捆绑包”错误。
    • stackoverflow.com/questions/9732051/… 另外,您可以使用其他方法,让您使用 'jar' 包装类型。
    • 包装元素应该是“bundle”,因为它是 osgi bundle。我在 pom.xml 中没有收到任何错误。在 mvn install 命令之后,我收到错误“无法在项目 cqcore 上执行目标 org.apache.maven.plugins:maven-surefire-plugin:2.15:test (default-test):执行目标 org.apache 的默认测试。 maven.plugins:maven-surefire-plugin:2.15:test failed: A required class is missing while execution org.apache.maven.plugins:maven-surefire-plugin:2.15:test: org/apache/commons/lang3/StringUtils"您能告诉我如何解决这个问题吗?
    猜你喜欢
    • 2013-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-29
    • 1970-01-01
    • 2021-11-24
    • 2015-09-29
    • 2014-05-22
    相关资源
    最近更新 更多