Adobe

Creating AEM Content Packages with Groovy Console

Webp.net Resizeimage (50)

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!

Thoughts on “Creating AEM Content Packages with Groovy Console”

  1. Thanks for this promising post. I’m very late to comment but was hoping you could elaborate on two things.

    First, can you please share an example of how to add a filter mode (e.g., mode=”merge”) and an exclude (e.g., pattern=”.*/rep:policy”)? For example:

    Second, do you have any tips for figuring out how to derive the various attributes and sample values that might be used for creating packages? For example, how do you determine if these attributes go in the data section of the groovy console or in the groovy script code?

    Thanks so much for sharing your knowledge.

  2. Paul Bjorkstrand Post author

    You could set the import mode by creating a package using createPackageFromPathFilterSets [1]. The class PathFilterSet has a method called setImportMode (inherited from PathFilter [2]), which allows you to change that mode.

    Excluding rep:policy is a package level property IIRC. I do not know the exact packageDefinitionProperties should be passed to the “create package” methods to set the package to include/exclude the ACLs/ACEs, though.

    [1]: https://github.com/Adobe-Consulting-Services/acs-aem-commons/blob/7a946166da2baf9962500b9b1e6f087b0664a68e/bundle/src/main/java/com/adobe/acs/commons/packaging/impl/PackageHelperImpl.java#L270
    [2]: https://jackrabbit.apache.org/filevault/apidocs/org/apache/jackrabbit/vault/fs/api/FilterSet.html#setImportMode(org.apache.jackrabbit.vault.fs.api.ImportMode)

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Paul Bjorkstrand

More from this Author

Subscribe to the Weekly Blog Digest:

Sign Up
Categories
Follow Us
TwitterLinkedinFacebookYoutubeInstagram