Adding Third-Party dependencies in SAP Commerce Cloud using Maven

Nuray Fahri
3 min readNov 5, 2023

When we need to work with external tools or libraries that are not included in the SAP Commerce Cloud(Hybris) package, Maven can be used to manage them. Each platform extension is equipped with the capability to utilize Maven to manage dependencies.

Steps to declare third-party dependencies(libraries)

To make our extension use Maven, we adjust our extensioninfo.xml file and add usemaven=”true”:

 <extension abstractclassprefix="Generated" classprefix="Example" managername="Example" managersuperclass="de.hybris.platform.jalo.extension.Extension" name="example" 
usemaven="true">
....
</extension>

2. We define third-party dependencies of SAP Commerce extensions in regular maven pom.xml files called external-dependencies.xml. The location of external-dependencies.xml depends on which component the extension contains:

a) coremodule — The extension’s root directory

Since all extensions share a common classpath(all extension/lib folders). It is important to ensure that an incompatible version of a library is not accidentally introduced.

b) webmodule — The extension’s customextension/web/webroot/WEB-INF directory

The customextension/web/webroot/WEB-INF/lib folder is not shared, and we can make changes to it as needed.

Here is an example:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.hybris.platform</groupId>
<artifactId>test</artifactId>
<version>6.3.0.0-SNAPSHOT</version>

<packaging>jar</packaging>

<dependencies>

<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>msal4j</artifactId>
<version>1.14.0</version>
</dependency>

</dependencies>
</project>

And result after system build(ant clean all):

As we can see marker file .lastupdate is created automatically. This file is used to avoid duplicate downloads.

What is unmanaged-dependencies.txt?

When we change usemaven to true in our extensioninfo.xml this means that build process will remove all libs in the lib folder and will add the ones declared in external-dependencies.xml because of the ant task updateMavenDependencies . If for some reason we already have libraries in lib folder and do not want them to be removed we can list these libraries in unmanaged-dependencies.txt files.

Transitive Dependencies

It’s important to know that the build process in SAP Commerce Cloud(Hybris) doesn’t download transitive dependencies.

What is transitive dependencies in Maven?

There are two types of dependencies in Maven: direct and transitive.

a ) Direct dependencies are the ones that we explicitly include.

b) Transitive dependencies are the ones required by our direct dependencies.

Usually, Maven automatically includes required transitive dependencies in our projects, but this is not valid for SAP Commerce (Hybris). Downloading transitive dependencies is disabled. SAP recommends all required dependencies to be explicitly(manually) defined.

Workaround for transitive dependencies

However, this can be a challenging task, particularly for libraries with numerous dependencies. There is also an alternative workaround, although it is not recommended.

We can override maven.download.options to enable transitive dependencies in our project.properties for the extension by:

maven.download.options=-DoverWriteReleases=true -DoverWriteSnapshots=true -DoverWriteIfNewer=true

Enabling transitive dependencies results in all required libraries for the declared one being downloaded automatically. Here is the result for our previous example (msal4j-1.14.0.jar) with enabled transitive dependencies:

I want to mention that there is also option we manually place needed *.jar files under lib folders without using Maven at all.

For more information, you can refer to the following links:

--

--