May 26, 2020
Estimated Post Reading Time ~

On-Deploy Scripts – an ACS AEM Commons Utility

Developers need to perform certain tasks during deployment to the AEM server. Component reauthoring is one of the major tasks for a developer to make 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 an 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

Using Servlet
Using Ad-hoc scripts
Issues with Servlet or Ad-hoc Scripts

Security Risk
Password Expose
Anyone can execute
The server remains in the 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:
Install v3.15.0 or higher version ACS-AEM Commons
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"/>
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()
                );}}
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”);}}}
Make sure acs-commons-on-deploy-scripts-service user has all necessary permissions
Just install the package
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


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.