In order to follow this how-to you'll need the following on your computer:
The Sling Feature Model provides a robust approach for configuring and assembling OSGi-based applications. Here are some of its high-level capabilities:
A Feature Model project is a standard Maven project with the following additional features:
Let's start by creating a simple Sling project using the traditional Maven Sling Bundle Archetype.
$ mvn -X archetype:generate -DarchetypeGroupId=org.apache.sling -DarchetypeArtifactId=sling-bundle-archetype \
-DgroupId=org.apache.sling.example \
-DartifactId=feature-model-sample \
-Dversion=1.0.0-SNAPSHOT \
-Dpackage=org.apache.sling.example
$ cd feature-model-sample
Note: There are plans to introduce a Maven archetype that will create a Maven project with support for Feature Models.
Now, let's update the POM and add a few elements to layer on support for Feature Models.
1. Add the following to the <properties>
element.
<slingfeature-maven-plugin.version>1.3.4</slingfeature-maven-plugin.version>
<sling-kickstart-maven-plugin.version>0.0.2</sling-kickstart-maven-plugin.version>
<oak.version>1.26.0</oak.version>
2. Add the Sling Feature Maven Plugin under <build>
-> <plugins>
.
<plugin>
<groupId>org.apache.sling</groupId>
<artifactId>slingfeature-maven-plugin</artifactId>
<version>${slingfeature-maven-plugin.version}</version>
<extensions>true</extensions>
<executions>
<execution>
<id>create-fm</id>
<phase>package</phase>
<goals>
<goal>include-artifact</goal>
</goals>
</execution>
<execution>
<id>aggregate-configuration</id>
<phase>package</phase>
<goals>
<goal>aggregate-features</goal>
</goals>
<configuration>
<aggregates>
<aggregate>
<classifier>feature_model_sample</classifier>
<filesInclude>**/*.json</filesInclude>
<title>Sling Sample Feature Model</title>
</aggregate>
</aggregates>
</configuration>
</execution>
<execution>
<id>install-fm</id>
<phase>package</phase>
<goals>
<goal>attach-features</goal>
</goals>
</execution>
</executions>
</plugin>
The Sling Feature Maven Plugin is responsible for creating a Feature Model JSON file for your project. The only pieces that change from project to project are the <classifier>
and <title>
element values. Set these values to something descriptive for your project. The classifier value will be used as part of the Feature Model JSON filename: target/slingfeature-tmp/feature-
classifier.json
Make a note of target/slingfeature-tmp/feature-feature_model_sample.json
as this path will be used in the next section.
3. Add a Maven profile called launch
under <project>
-> <profiles>
.
<profile>
<id>launch</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.sling</groupId>
<artifactId>sling-kickstart-maven-plugin</artifactId>
<version>${sling-kickstart-maven-plugin.version}</version>
<extensions>true</extensions>
<executions>
<execution>
<id>start-sling</id>
<phase>install</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
</executions>
<configuration>
<launchpadDependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.kickstart</artifactId>
<version>0.0.3-SNAPSHOT</version>
</launchpadDependency>
<parallelExecution>false</parallelExecution>
<keepLaunchpadRunning>true</keepLaunchpadRunning>
<servers>
<server>
<port>8080</port>
<controlPort>8081</controlPort>
<additionalFeatureFile>
target/slingfeature-tmp/feature-feature_model_sample.json
</additionalFeatureFile>
<debug>true</debug>
</server>
</servers>
</configuration>
</plugin>
</plugins>
</build>
</profile>
This profile is responsible for starting your application using the Kickstarter. Simply, set the <additionalFeatureFile>
to the file in your target directory that contains your application's Feature Model JSON.
$ mvn clean install -Plaunch
Now, log into Sling and visit the System Console. You should see your bundle (feature-model-sample
) listed.
Well, that was fun. We'll revisit this sample project soon, But first, let's see how the Feature Model can help us handle complex application configurations. In the next section, we'll take a look at the Composite NodeStore and how to configure it with the Feature Model.