Skip to Content
Technical Articles
Author's profile photo Sumit Kr Haldar

Customize OOTB files using buildcallbacks in CCV2

In this blog you will learn to modify the buildcallbacks to customize (add/modify) any OOTB file, either completely or part of it.

 

Traditional Approach to Overwrite OOTB Files using ant customize

There are times, when it is not possible to override OOTB class or spring bean in the custom extension, and the only way forward is to customize the file, so that the modified file can replace the OOTB one, and additional file can also be copied to the OOTB extension path, if required. In CCV1, the necessary modified file can be placed in config/customize by following the proper directory structure and then apply the changes using ant customize. This approach can then be adopted in the build steps as well. However, this is not possible in CCV2, since users do not have control to modify the build steps.

Currently the CCV2 build process triggers the ant customize target, so similar approach to the one implemented in CCV1 can be followed here. You need to create core-customize/_CUSTOMIZE_ and place the files following the same directory structure which were supposed to be placed under config/customize in CCV1.  However, this has the following two limitations:

  1. Since CCV2 build process is not in the control of the user and if ant customize target is deprecated, the customization will cease to work.
  2. With this approach the entire file will be replaced, so any modification in the OOTB file in the future release, even though that content is not something you need to customize, will also get removed.

Based on these two problems, we can make use of the buildcallbacks to modify either part of the file content or the full file, as explained in the steps below.

 

Using buildcallbacks to write ant commands to copy or replace files

Approach 1: Replace a file completely using buildcallbacks

Step 1: Identify the buildcallbacks.xml file where you need to write ant command to copy/move file from source to target. Let’s assume it to be customstorefront/buildcallbacks.xml

Step 2: Identify the macrodef where the changes need to be done. If it is a java file, it is better to do it in before_build macrodef.

Step 3: Write the copy/move command inside the macrodef, like below:

<macrodef name="customstorefront_before_build">
   <sequential>
      <move file="${ext.customstorefront.path}/copyfiles/test.txt" tofile="${HYBRIS_BIN_DIR}/modules/base-accelerator/yacceleratorstorefront/resources/yacceleratorstorefront/test2.txt"/>
   </sequential>
</marcodef>
		

Here, I have considered the destination directory to be yacceleratorstorefront/resources/yacceleratorstorefront. Also, note that the source file name and the destination file names are different. You can either keep it same or different, depending on your choice and/or requirement. In case no change in file name is expected, following code snippet can also be used:

<macrodef name="customstorefront_before_build">
   <sequential>
      <move file="${ext.customstorefront.path}/copyfiles/test.txt" todir="${HYBRIS_BIN_DIR}/modules/base-accelerator/yacceleratorstorefront/resources/yacceleratorstorefront"/>
   </sequential>
</marcodef>
		

Sample code above is to move the file from source to target. In case you have to copy the file as well as keep the same file in the source, you can use ‘copy’ instead of ‘move’ in the ant command.

Step 4: Place the file according to your source directory mentioned above in step 3.

Step 5: Perform ant clean all and you will find the file copied/moved to the destination.

 

Approach 2: Replace multiple files in the same directory using buildcallbacks

Step 1: As mentioned in the previous approach, identify the buildcallbacks.xml file and the corresponding macrodef which can be modified for this.

Step 2: Write the copy/move command inside the macrodef, like below:

<macrodef name="customstorefront_before_build">
   <sequential>
      <copy todir="${HYBRIS_BIN_DIR}/modules/base-accelerator/yacceleratorstorefront/resources/yacceleratorstorefront" overwrite="true">
         <fileset dir="${ext.customstorefront.path}/copyfiles"/>
      </copy>
   </sequential>
</marcodef>
		

Similar to previous approach, both copy/move commands can be used.

Note the use of overwrite property. If it is marked as true, then the files in the destination will be overwritten. In case it is false, then whether the destination file will be overwritten or not will depend on the modified timestamp of the files.

 

Step 3: Place the files according to your source directory mentioned above in step 2.

Step 4: Perform ant clean all and you will find all the files copied/moved to the destination.

 

Approach 3: Replace content of a file using buildcallbacks

In case there is a scenario that the class of a spring bean needs to be modified, it can be done so without replacing the entire file. In this way, you can avoid modifying remaining content of the file, thus any new modification in later release will not be wiped out.

Step 1: As mentioned in the previous approach, identify the buildcallbacks.xml file and the corresponding macrodef which can be modified for this.

Step 2: Write the replace command inside the macrodef, like below:

<macrodef name="customstorefront_before_build">
   <sequential>
      <replaceregexp file="${HYBRIS_BIN_DIR}/modules/smartedit/cmssmarteditwebservices/web/webroot/WEB-INF/cmssmarteditwebservices-web-spring.xml" match="de.hybris.smartedit.webservicescommons.jaxb.MoxyJaxbContextFactoryImpl" replace="com.custom.jaxb.CustomMoxyJaxbContextFactoryImpl" />
   </sequential>
</marcodef>

This will look for the regex match in the file mentioned, and replace the matched content with the value provided. Do remember to copy the corresponding file, if required, by following the previous approach.

Step 3: Perform ant clean all. After that open the file mentioned above and look for the changes specified by ‘replace’.

 

References: Below two SAP Knowledge Base Articles mention the CCV2 customize process and suggests to use the buildcallbacks approach.

 

I hope I have been able to explain and throw some light on the buildcallbacks process and how it can effectively handle customization.

NOTE: It is normally not recommended to replace OOTB files, but in some cases it may be necessary. Please review carefully if any alternative is available and the requirement can be handled without replacing OOTB files.

Please share your thoughts and feedback in the comments section below. Do look out for more new blog posts.

Assigned Tags

      1 Comment
      You must be Logged on to comment or reply to a post.
      Author's profile photo David Boyd
      David Boyd

      Thanks for creating this and providing examples.  This provided most useful as we were exploring what the options are if we need to override OTB files.