My Wiki!

Karaf Web

Example: pax-whiteboard

1. Generate bundle project with Web Bundle Archetype

mvn archetype:generate -DarchetypeGroupId=org.ops4j.pax.web.archetypes -DarchetypeArtifactId=wab-archetype

2. Add dependencies

                <dependencies>
			<dependency>
				<groupId>org.ops4j.pax.web</groupId>
				<artifactId>pax-web-extender-whiteboard</artifactId>
                                <!-- version: check karaf pax-http-whiteboard version -->
			</dependency>
		</dependencies>

Parent pom.xml

        <properties>
		<maven-bundle-plugin.version>2.3.7</maven-bundle-plugin.version>
		<servlet-api.version>2.5</servlet-api.version>
		<org.ops4j.pax.web.version>6.0.3</org.ops4j.pax.web.version>
	</properties>
 
	<dependencyManagement>
		<dependencies>
 
			<dependency>
				<groupId>org.ops4j.pax.web</groupId>
				<artifactId>pax-web-extender-whiteboard</artifactId>
				<version>${org.ops4j.pax.web.version}</version>
			</dependency>
		</dependencies>
	</dependencyManagement>

3. Blueprint

We move html to src/main/webapp. The folder content will be configured to be placed in bundle root (build config). So we also put blueprint.xml file there.

mkdir -p src/main/webapp/OSGI-INF/blueprint
vim src/main/webapp/OSGI-INF/blueprint/blueprint.xml
cat src/main/webapp/OSGI-INF/blueprint/blueprint.xml 
<blueprint default-activation="eager"
    xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 
    <bean id="resourceMapping"
        class="org.ops4j.pax.web.extender.whiteboard.runtime.DefaultResourceMapping">
        <property name="alias" value="/" />
        <property name="path" value="" />
    </bean>
 
    <bean id="welcomeFile"
        class="org.ops4j.pax.web.extender.whiteboard.runtime.DefaultWelcomeFileMapping">
        <property name="redirect" value="true" />
        <property name="welcomeFiles">
            <array>
                <value>index.html</value>
            </array>
        </property>
    </bean>
 
    <service id="resources" ref="resourceMapping"
        interface="org.ops4j.pax.web.extender.whiteboard.ResourceMapping" />
 
    <service id="welcomeFileService" ref="welcomeFile"
        interface="org.ops4j.pax.web.extender.whiteboard.WelcomeFileMapping" />
</blueprint>

4. mvn build configration

Configure bundle root

  <_wab>src/main/webapp/</_wab>

This means everything in src/main/java is compiled and put in WEB-INF/classes. Everything in src/main/resources is put in WEB-INF/classes. This is pretty much what you would expect because these locations are used to put files that are loadable via the classloader. The content in src/main/webapp is then placed in the root. In fact in this scenario you put the web.xml in the src/main/webapp/WEB-INF directory, and if your WAB contains blueprint your blueprint would go in src/main/webapp/OSGI-INF/blueprint.

		<build>
			<plugins>
				<plugin>
					<groupId>org.apache.felix</groupId>
					<artifactId>maven-bundle-plugin</artifactId>
					<version>${maven-bundle-plugin.version}</version>
					<extensions>true</extensions>
					<configuration>
						<instructions>
							<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
							<Bundle-Version>${project.version}</Bundle-Version>
							<Import-Package>*</Import-Package>
							<Web-ContextPath>/services</Web-ContextPath>
							<!-- Sets Bundle-Classpath to WEB-INF/classes -->
							<_wab>src/main/webapp/</_wab>
					</instructions>
					<supportedProjectTypes>
						<supportedProjectType>jar</supportedProjectType>
						<supportedProjectType>bundle</supportedProjectType>
						<supportedProjectType>war</supportedProjectType>
					</supportedProjectTypes>
				</configuration>
				<executions>
					<execution>
						<id>generate-manifest</id>
						<phase>process-classes</phase>
						<goals>
							<goal>manifest</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
			<plugin>
				<artifactId>maven-war-plugin</artifactId>
				<configuration>
					<archive>
						<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
					</archive>
				</configuration>
			</plugin>
		</plugins>
	</build>

The resulting jar:

jar tvf target/sd-web-webapp-1.0.jar 
   698 Fri Apr 21 19:20:58 CEST 2017 META-INF/MANIFEST.MF
     0 Fri Apr 21 19:20:58 CEST 2017 META-INF/
     0 Fri Apr 21 19:20:58 CEST 2017 META-INF/maven/
     0 Fri Apr 21 19:20:58 CEST 2017 META-INF/maven/com.gtarc.servicedirectory/
     0 Fri Apr 21 19:20:58 CEST 2017 META-INF/maven/com.gtarc.servicedirectory/sd-web-webapp/
   147 Fri Apr 21 19:20:58 CEST 2017 META-INF/maven/com.gtarc.servicedirectory/sd-web-webapp/pom.properties
  2742 Fri Apr 21 19:20:10 CEST 2017 META-INF/maven/com.gtarc.servicedirectory/sd-web-webapp/pom.xml
     0 Fri Apr 21 19:20:58 CEST 2017 OSGI-INF/
     0 Fri Apr 21 19:20:58 CEST 2017 OSGI-INF/blueprint/
   974 Fri Apr 21 18:15:48 CEST 2017 OSGI-INF/blueprint/blueprint.xml
   328 Fri Apr 21 18:10:58 CEST 2017 index.html

5. Load & run

  karaf@root()> install -s mvn:com.gtarc.servicedirectory/sd-web-webapp/1.0
  feature:repo-add mvn:com.gtarc.servicedirectory/sd-feature/1.0/xml/features    

Navigation