Thursday, 8 November 2012

How to use a Maven project to create a content package for CQ

-->

How to use a Maven project to create a content package for CQ (or more specifically Sling)


This articles describes how to get hold of an example Maven project for creating a content package; how to use it to install a content package; and, a little bit of explanation of the POM provided.

Here are some important summary items.


Downloading the sample Maven project


$ mvn archetype:generate

...
301: remote -> org.apache.sling:sling-bundle-archetype (-)
302: remote -> org.apache.sling:sling-initial-content-archetype (Maven archetype for initial content)
303: remote -> org.apache.sling:sling-jcrinstall-bundle-archetype (-)
304: remote -> org.apache.sling:sling-launchpad-standalone-archetype (-)
305: remote -> org.apache.sling:sling-launchpad-webapp-archetype (-)
306: remote -> org.apache.sling:sling-servlet-archetype (Maven archetype for Sling Servlets)
….

Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 225: 302
Downloading: http://repo1.maven.org/maven2/org/apache/sling/sling-initial-content-archetype/1.0.0/sling-initial-content-archetype-1.0.0.jar
Downloaded: http://repo1.maven.org/maven2/org/apache/sling/sling-initial-content-archetype/1.0.0/sling-initial-content-archetype-1.0.0.jar (10 KB at 104.4 KB/sec)
Downloading: http://repo1.maven.org/maven2/org/apache/sling/sling-initial-content-archetype/1.0.0/sling-initial-content-archetype-1.0.0.pom
Downloaded: http://repo1.maven.org/maven2/org/apache/sling/sling-initial-content-archetype/1.0.0/sling-initial-content-archetype-1.0.0.pom (4 KB at 7.4 KB/sec)
Define value for property 'groupId': : com.mkalugin.cq5
Define value for property 'artifactId': : test-wibble
Define value for property 'version':  1.0-SNAPSHOT: :
Define value for property 'package':  com.mkalugin.cq5: :
Confirm properties configuration:
groupId: com.mkalugin.cq5
artifactId: test-wibble
version: 1.0-SNAPSHOT
package: com.mkalugin.cq5
 Y: :
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Archetype: sling-initial-content-archetype:1.0.0
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.mkalugin.cq5
[INFO] Parameter: artifactId, Value: test-wibble
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: package, Value: com.mkalugin.cq5
[INFO] Parameter: packageInPathFormat, Value: com/mkalugin/cq5
[INFO] Parameter: package, Value: com.mkalugin.cq5
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: groupId, Value: com.mkalugin.cq5
[INFO] Parameter: artifactId, Value: test-wibble
[INFO] project created from Archetype in dir: /cq/mkalugin-cq/projectX/proj-cq5-core/tmp/test-wibble
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 19.944s
[INFO] Finished at: Tue Oct 30 14:56:39 GMT 2012
[INFO] Final Memory: 7M/81M
[INFO] ------------------------------------------------------------------------
$ ls
test-wibble/

$ cd test-wibble
$ mvn -PautoInstallBundle install
[INFO] Scanning for projects...
[INFO]                                                                        
[INFO] ------------------------------------------------------------------------
[INFO] Building test-wibble 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] Installing Bundle com.mkalugin.cq5.test-wibble(/cq/mkalugin-cq/projectX/proj-cq5-core/tmp/test-wibble/target/test-wibble-1.0-SNAPSHOT.jar) to http://localhost:4502/system/console via POST
[INFO] Bundle installed
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

Anatomy of the POM

The above section showed how to get the example maven project for a CQ (more specifically a Sling) content package.
Now follows an analysis of the produced POM.

Standard first 3 lines :-

<?xml version="1.0" encoding="ISO-8859-1"?>
<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

Now, our project specific information (NB, this appears in Felix bundles).

    <groupId>com.mkalugin.cq5</groupId>
    <artifactId>test-wibble</artifactId>

NB, Packaging is specified as "bunlde", this will be an OSGi bundle (not a CRX package).

    <packaging>bundle</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>test-wibble</name>
    <description>com.mkalugin.cq5 - test-wibble</description>

Now comes the maven-bundle-plugin definition - this does the work of creating an OSGi Sling bundle with instructions of how to install (or uninstall if you with) the content nodes.

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <extensions>true</extensions>
                <version>2.3.7</version>

Now comes the details of the content nodes and what we want to do with them.

                <configuration>
                    <instructions>
                        <Sling-Nodetypes>
                            SLING-INF/nodetypes/nodetypes.cnd
                        </Sling-Nodetypes>
                        <Sling-Initial-Content>
                            SLING-INF/scripts;overwrite:=true;uninstall:=true;path:=/apps/my/node,
                            SLING-INF/content;overwrite:=true;uninstall:=true;path:=/content
                        </Sling-Initial-Content>
                    </instructions>
                </configuration>
            </plugin>
        </plugins>
    </build>

An autoInstallBundle profile is given by default showing how the produced bundle can be loaded & installed in to Felix.
This is documented at : http://sling.apache.org/site/content-loading-jcrcontentloader.html

    <profiles>
        <profile>
            <id>autoInstallBundle</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.sling</groupId>
                        <artifactId>maven-sling-plugin</artifactId>
                        <version>2.1.0</version>
                        <executions>
                            <execution>
                                <id>install-bundle</id>
                                <goals>
                                    <goal>install</goal>
                                </goals>
                                <configuration>
                                    <slingUrl>http://localhost:4502/system/console</slingUrl>
                                    <user>admin</user>
                                    <password>admin</password>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>

Further reading : http://sling.apache.org/site/content-loading-jcrcontentloader.html

The end.