1

我是 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 服务。请指导我完成它。

4

2 回答 2

2

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

正如已经指出的那样,如果您按照Maven 捆绑插件文档中给出的说明进行操作,您可以将捆绑包的“打包”设置为“jar” 。

有一个小问题:使用该配置,您需要<exportScr>true</exportScr>在插件配置中显式添加才能正确创建 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>
于 2016-11-22T10:28:25.990 回答
0

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

http://felix.apache.org/site/apache-felix-maven-bundle-plugin-bnd.html#ApacheFelixMavenBundlePlugin%28BND%29-AddingOSGimetadatatoexistingprojectswithout更改包装类型

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

maven-scr-plugin 可以生成 OSGI-INF,但只有在使用 SCR 时才有用。Maven SCR 插件 - 不生成 OSGI-INF 文件夹

于 2013-06-30T04:06:19.250 回答