1. Imports in Groovy

Groovy, like Java, uses imports to include other classes and packages into your script. This makes it easier to access and use classes without having to reference their full package path. In Magnolia CMS, Groovy scripts are often used for scripting complex functionality, accessing and manipulating content nodes, or interacting with Magnolia's APIs.

Basic Syntax

The basic syntax for an import statement in Groovy is:

import [package].[ClassName]

You can also import all classes from a package:

import [package].*

Static Imports

Groovy also supports static imports, which allow you to access static members of a class directly without class qualification.

import static [package].[ClassName].[staticMember]


// Importing a specific class from Magnolia CMS
import info.magnolia.cms.beans.config.ServerConfiguration

// Importing all classes from a specific Magnolia package
import info.magnolia.jcr.util.*

// Static import of a method from a Magnolia utility class
import static info.magnolia.cms.util.AlertUtil.showAlert

// Importing multiple specific classes from the same Magnolia package
import info.magnolia.context.MgnlContext
import info.magnolia.context.WebContext

// Importing a class from the Java standard library
import java.util.Date

// Importing a class from a third-party library (Apache Commons Lang)
import org.apache.commons.lang3.StringUtils

// Importing an enum type from Magnolia CMS
import info.magnolia.link.LinkType

// Importing Groovy's SQL class for database interactions
import groovy.sql.Sql

// Importing Java Content Repository (JCR) class for session operations
import javax.jcr.Session

// Importing Magnolia templating functions for use in templates
import info.magnolia.templating.functions.TemplatingFunctions