Anderson
Anderson

Reputation: 1200

Not generating OSGI-INF folder

I am new to OSGI.I have been having trouble to get OSGI-INF folder in generated jar file. I need to have folder structure like as below

I am using Eclipse and m2e plugin. When I run my project I am getting BUILD SUCCESS. And I am getting the below folder structure in that generated jar file.

Here is my 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>

Why OSGI-INF folder is not in the .jar file? I need to set some information in OSGO-INF folder since I have to register my component as a OSGI service. Please guide me to get it done.

Upvotes: 1

Views: 5701

Answers (2)

Koldar
Koldar

Reputation: 1407

Although being pretty late, I'll post my 2 cents about this issue for future reference.

As already pointed out, you can have the "packaging" of the bundle set to "jar" if you follow the instructions given in Maven bundle plugin documentation.

There is just a little gotcha: with that configuration, you need to explicitly add <exportScr>true</exportScr> inside the plugin configuration in order to properly create the SCR xml file (also remember to adjust manifest location, since in the documentation that piece is absent!).

You can see an example here (that's totally different from yours, but I assume you can easily reduce it on your code, if you're still interested):

<?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>

Upvotes: 3

Sheena Artrip
Sheena Artrip

Reputation: 2010

Your pom.xml needs to have a packaging type of "bundle" rather than "jar". If you want the packaging type to be "jar", use this:

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

EDIT: Oh! That's only problem one. The other problem is that I don't think you can generate OSGI-INF with the maven-bundle-plugin. You need to create the OSGI-INF folder yourself within src/main/resources or use a plugin that generates OSGI-INF.

The maven-scr-plugin can generate OSGI-INF, but it's only useful if you are using SCR. Maven SCR Plugin - Not generating OSGI-INF folder

Upvotes: 0

Related Questions