Blogs

a perpendicular cylinder breaking up into puzzle pieces
On-Deploy Scripts – an ACS AEM Commons Utility

 

Developers need to perform certain tasks during deployment to AEM server. Component reauthoring is one of the major tasks for a developer making changes to existing components. In this article, we’ll talk about using on-deploy scripts to simplify reauthoring tasks in AEM.

Problem Statement:

If the component is to be configured on one or two pages, then reauthoring is simple. However, if the component needs to be configured on many pages, then it’s tedious for an author to reauthor the same component on every page.

This is one of the scenarios in which we can use on-deploy script. Here is a list of problems we will try to solve.

What Problems are We Trying to Solve?

  • Running ad-hoc script manually
  • Update the content without reauthoring
  • Admin update that needs to do on all server
  • Mass content updates
  • One-time content creation
  • Removing obsolete content
  • Deleting all nodes of a particular resource type

Available Methodology to Solve the Above Problems

  1. Using Servlet
  2. Using Ad-hoc scripts

Issues with Servlet or Ad-hoc Scripts

  • Security Risk
  • Password Expose
  • Anyone can execute
  • Server remains in broken state till we run Ad-hoc script or if it fails
  • Making sure you run scripts on deployment else existing functionality breaks
  • Keeping track of scripts that have been run becomes difficult

ACS AEM Commons provides a useful utility called “On-Deploy Scripts,” which help developers create one-time scripts that execute upon deployment to an AEM server. This feature is available for v3.15.0 and higher versions of ACS-AEM Commons.

So, to overcome this problem we can use On-Deploy Scripts.

Advantages of On-Deploy Scripts:

On-Deploy Scripts allows developers to create one-time scripts that execute upon deployment to an AEM server. Some advantages of this one-time script creation feature are given below.

  • Fully automated
  • Continuous integration gives you automatic testing on Pre-prod
  • Scripts execute along with deployment – more broken state
  • Automation removes chances for error
  • Script status is preserved to be validated later
  • Available with version v3.15.0

How To use:

  1. Install v3.15.0 or higher version ACS-AEM Commons
  2. Enable on-deploy script feature
    • Create OSGi configuration

“com.adobe.acscommons.ondeploye.impl.OnDeployExecutorImpl.xml”

<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0"
xmlns:jcr="http://www.jcp.org/jcr/1.0"
                      jcr:primaryType="sling:OsgiConfig"/>
  1. Implement Script Provider Service
    • Override the getScripts () method.
@Component(immediate = true)
@Service
@Properties({
                @Property(name = "service.description", value = "Developer 
service that identifies code scripts to execute upon deployment")
})
public class OnDeployScriptProviderImpl implements OnDeployScriptProvider 
{
      @Override
      public List<OnDeployScript> getScripts() {
               return Arrays.asList(
                               new ScriptCreateNode(),
                               new ScriptSearchUpdateResourceType()
                );}}
  1. Create Script and Add Script Provider
    • Override execute () method
public class ScriptUpdateProperty extends OnDeployScriptBase  implements
OnDeployScript {
         @Override
         protected void execute() throws Exception {

Node = nodeupdate = getOrCreateNode(“content/we-
retail/us/en/onDeployePage/jcr:content”,”cq:Page”,”cq:PageContent”);

Nodeupdate.setProperty(“sling:resourceType”,”weretail/components/structure/page”);}}}
  1. Make sure acs-commons-on-deploy-scripts-service user has all necessary permissions
  2. Just install the package
  3. Check status at /var/acs-commons/on-deploy-script-status

Demo:

A working demo present on bitbucket https://bitbucket.org/argildx/aemdev-meetup-s1/overview

 

Read our article about another ACS AEM Commons Utility, Ensure Service User

Share it:

Ankit Kumar Vishwakarma

August 5, 2019

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Comment

Related Posts