Creating AEM Content Packages with Groovy Console | Perficient Digital

Creating AEM Content Packages with Groovy Console

For those who (like me) sometimes need to create content packages of content using a list of tens or hundreds of paths. At best, using AEM’s package manager is tedious, but at worst, it is many, many wasted minutes of clicking. Many developers would resort to creating one by hand by doing lots of copy/paste. Some may opt to write shell scripts to do the work for them. Since I am a developer, I would much rather automate all that manual clicking. Both AEM Groovy Console and ACS AEM Commons help you automate the tedium. It turns out, using both together makes this task a cinch.

Automate all the things!

ACS Commons is a remarkable suite of tools, libraries, and functionality. AEM Groovy Console gives us the flexibility to do ad hoc scripting, using the power of everything available in the AEM instance. The two go together like peas and carrots.

ACS Commons and Groovy console go together like peas and carrots.

Without further ado, the code

import com.adobe.acs.commons.packaging.PackageHelper.ConflictResolution
import java.text.SimpleDateFormat

def packageHelper = getService("com.adobe.acs.commons.packaging.PackageHelper")

if (!(data instanceof Map)) {
    throw new IllegalArgumentException("data must be a json value")
}

if (!data.name) {
    throw new IllegalArgumentException("a name must be provided")
}

packageHelper.createPackageForPaths(
    data.paths ?: [],
    session,
    data.group ?: "my_packages",
    data.name,
    data.version ?: new SimpleDateFormat("yyyy-MM-dd'T'hh_mm_ss").format(new Date()),
    ConflictResolution.valueOf(data.conflictResolution ?: "None"),
    data.definition ?: [:])

To use this, you need to provide some JSON in the “Data” section at the top of the Groovy Console:

{
    "paths": [
        "/content/dam/fake/image.png"
    ],
    "name": "package-name"
}

I could not find a simpler way to create a content package with a list of paths. For those inventive developers out there, you can change how that list of paths is built to make it work for whatever your use-case is. For example, if you could run a query on resources using ResourceResolver.findResources(...) and use the resource paths (alternatively, PackageHelper has a method for using a Collection<Resource> instead of the Collection<String>). If you haven’t already, you should look at the code for PackageHelper to see what all of the parameters do. In fact, I strongly recommend familiarizing yourself with as much of ACS Commons as you can. There really is a lot of great stuff in there.

Feel free to take this script and modify it to your heart’s content. Make it bend to your will to make your content package creation easier.

Happy packaging!

Leave a Reply