At times, setting up the initial state of AEM repository may appear cumbersome, especially when we have to setup multiple things beforehand such as:
1. Creating service users
2. Creating user groups
3. Setting up ACLs/permissions for these users
4. Base content structure inside /conf or /content
That said, it’s just not about the content, it could be configurations as well. Creating and setting up these configurations manually may lead to mistakes. Also, this doesn’t just apply to remote environments; it is much needed for a developer’s local instance as well. To overcome all these issues, Apache Sling Repository Initialization (otherwise known as Repo Init) comes very handy.
How Repo Init works?
Repo Init is a set of instructions/scripts which help us in creating JCR structures, service users and groups. There are no explicit permissions needed in order to run these scripts. These scripts are executed early in the deployment process so that all required configuration/content exist in the system before the code is executed.
This article is based and tested on AEM 188.8.131.52 with below bundle versions:
org.apache.sling.jcr.repoinit – 1.1.8
org.apache.sling.repoinit.parser – 1.2.2
Benefits of Repo Init:
- Keep all environments in-sync in terms of initial/basic configuration.
- Speeds up local environment setup for developers.
- No lag between the code deployment and basic setup of AEM repository. By the time code is deployed, Repo Init instructions are already executed.
Configuring Repo Init:
These scripts are stored as a factory OSGi configuration of type “org.apache.sling.jcr.repoinit.RepositoryInitializer”. Being an OSGi configuration, it is easy to have different set of scripts for author and publish run-modes along with different environments such as Stage and Production. For e.g. config.author, config.publish, config.author.prod, config.publish.stage etc.
Below is a sample Repo Init OSGi configuration with jcr:primaryType “sling:OsgiConfig”:
scripts = “[
create service user demo-service-user,
create path /conf/demo(nt:folder)/token(sling:Folder),
set ACL for demo-service-user
allow jcr:read,rep:write on /conf/demo/token
Tip: In order to save this OSGi configuration as repository configuration, we had to use ASCII encoded value “
” for new-line character.
During deployment, we see log statements like below in error.log file where we see each and every command is executed line-by-line.
Post deployment, you can also see the Repo Init configurations in OSGi console:
There are other ways as well for initializing a repository but Repo Init consolidates them all in one tool making the configurations easy to implement and manage. Also, Apache Sling Repo Init is compatible with AEM as a Cloud Service.