Sling RepoInit: a Handy Tool to Manage Content and Users in AEM
Managing the initial repository state can be hard because it involves setting up code, configuration, content, and more. This is a lot to cope with when we need to complete several tasks in advance, such as creating service users, creating groups, setting up ACL permissions, and pre-creating/updating content. Repoinit (Apache Sling Repository Initialization) is a tool to help configure all of this in one place.
The advantages of Repoinit include:
- Keeping the environments in sync in terms of initial and basic configuration
- Speeding up local environment setup for developers
- Creating resources at startup so they’re built into the logic from the beginning
Enable Repoinit in AEM
To enable Repoinit, OSGi configuration for factory PID org.apache.sling.jcr.repoinit.RepositoryInitializer in one of the project’s configuration folders. Since it’s an OSGi configuration, it is easy to have a set of scripts for author and publish run modes, along with different environments such as Stage and Production. For example, config.author, config.publish, config.author.prod, and config.publish.stage, etc. use a descriptive name for the configuration like org.apache.sling.jcr.repoinit.RepositoryInitializer~init.
These configurations have two optional fields:
- A multi-value references field with each value providing the URL (as a String) of raw Repoinit statements
- A multi-value scripts field with each value providing Repoinit statements as plain text in a String
Note 1: this service presents a challenge when you’re resolving the references. Each reference is expected to be in the form of a URL, and OSGi supports exposing bundles as URLs. However, when Apache Felix resolves a bundle’s URL in the URLHandlersBundleStreamHandler, it expects the URL host to be the UUID of the bundle, not a stable ID such as the Bundle’s Symbolic Name. You can find out how to resolve this issue here.
Note 2: When configuring script properties, it is easier to define them in the .config file than it is to define in the JSON-based .cfg.json format, since scripts are typically multiline.
Configuration file example: /apps/my-app/config.author/org.apache.sling.jcr.repoinit.RepositoryInitializer-init.config
Repoinit vs. Ensure Authorizable
Ensure Service User config:
/ui.apps/src/main/content/jcr_root/apps/site-com/config/com.adobe.acs.commons.users.impl.EnsureServiceUser-user-1.xml (this is the config for user-1 only; for user-2 we will need to create a separate xml config)
Equivalent Repoinit script:
create service user user-1, user-2
Content Management: Repoinit vs. OnDeploy Scripts
Imagine that we need to create a “/path/A” resource and update the existing one under “/path/B”.
The OnDeplyScript might look something like this:
And this is a script which does the same thing:
These are just a few examples of how we used Repoinit for our project; find more ideas about what you can do using Repoinit here.
Was this article useful for you?
Get in the know with our publications, including the latest expert blogs
End-to-End Digital Transformation
Reach out to our experts to discuss how we can elevate your business