Skip to main content

Version: 25.04

Code Library Overview

What are Code Libraries?

Code Libraries are reusable Groovy classes that provide shared functionality across multiple services within Journey Manager (JM). Unlike traditional Groovy services that include files using fileIncludes, Code Libraries are first-class service definitions that can be instantiated, passed by reference between services, and support full object-oriented programming features including inheritance, interfaces, and polymorphism.

Think of Code Libraries as building blocks for your JM solutions that you build to encapsulate common logic, data models, and utilities that can be shared across forms, services, and workflows without code duplication.

Key Benefits

  • Reusability - Write once, use across multiple services and solutions
  • Maintainability - Update shared logic in one place, propagate changes everywhere
  • Type Safety - Full Groovy type checking support to catch errors at compile time
  • Version Control - Isolate different versions of libraries for different solutions
  • Pass by Reference - Objects maintain state across service invocations without serialization
  • Object-Oriented - Full support for inheritance, interfaces, composition, and polymorphism

Why use Code Libraries?

The problem they solve

Before Code Libraries, sharing classes between services required:

  • Using fileIncludes to duplicate class definitions across services.
  • Manual serialization/deserialization when passing objects between services.
  • Updating multiple service definitions when a shared class changed.
  • No true object-oriented programming with inheritance and interfaces.

The solution

Code Libraries eliminate these pain points by:

  1. Eliminating Duplication: Define a class once as a Code Library, use it everywhere
  2. Passing Objects by Reference: Share live objects between services without serialization—changes to an object in one service are immediately visible to others
  3. Enabling True OOP: Create inheritance hierarchies, implement interfaces, and compose complex objects
  4. Centralized Updates: Modify a Code Library once, all dependent services automatically use the updated version
  5. Version Isolation: Run multiple versions of the same library simultaneously for different solutions

Common use cases

  • Data Models - Define Customer, Application, Address classes shared across services
  • Business Logic - Encapsulate validation rules, calculations, or workflows
  • Utility Functions - Common operations like date formatting, string manipulation, API helpers
  • Integration Adapters - Reusable classes for connecting to external systems
  • Framework Components - Build internal frameworks for consistent solution development

Supported constructs

Currently, Code Libraries support the following reference types in Java:

  1. Class typesclass
  2. Interface typesinterface
  3. Enumeration typesenum

How Code Libraries work

Object-oriented programming support

Code Libraries support full object-oriented programming in Groovy. The services shown in the diagram illustrate how inheritance works:

Code Library Inheritance Example

The IVehicle interface serves as a blueprint that defines what every vehicle should include, such as the numWheels property and a getType() method, without providing any actual implementation. The Car class implements this interface, giving a concrete definition by returning 'Car' in the getType() method. The Hybrid class then extends Car, inheriting its behavior while overriding getType() to return 'Hybrid', showing how inheritance and polymorphism allow classes to build upon and customize existing functionality.

With Code Libraries, you can use:

  • Interfaces to define contracts
  • Inheritance to extend base classes
  • Composition to build complex objects from simpler ones
  • Polymorphism to write flexible, extensible code

Passing by reference

Code Libraries provides a straightforward approach to passing objects by reference between services. The logic flow doesn't need to go through a central invoker. Instead, a Code Library instance can be passed directly from one service to another.

Code Library Usage Flow

This diagram shows how Code Libraries contain shared logic that multiple services can call without duplicating code. Each Groovy service can directly access one or more libraries to perform specific tasks, while libraries themselves can also call other libraries. As a result, functionality can flow between services and libraries without needing to pass everything through a central invoker.

Code Library types

There are two primary types of Code Libraries, each suited for different scenarios:

TypeCentralized Code LibrariesSolution-Specific Code Libraries
PurposeShared across multiple solutions and organizationsPackaged within a specific solution
DevelopmentDeveloped in a dedicated SDK projectDeveloped within the Solution SDK project
DeploymentGenerates .zip archive for deployment to JMDeployed automatically with the solution
Maven ArtifactOptionally generates .jar artifact for use as Maven dependencyNo separate Maven artifact needed
Hierarchy LevelTypically assigned to Global or Org levelTypically assigned to Org or Org/Tag level for isolation
VersioningVersioned independently of solutionsVersioned with the solution
Best For• Common utilities (date helpers, validation rules)
• Enterprise-wide data models
• Shared integration adapters
• Internal framework components
• Solution-specific business logic
• Data models unique to one application
• Classes that won't be reused by other solutions
Learn MoreCentralized Code LibrariesSolution Code Libraries

Both approaches also work together. Use a Centralized Code Libraries for common functionality along with Solution-Specific libraries for application-unique logic. Additionally, solution projects can depend on Centralized libraries via Maven dependencies.