Packages and Imports in Groovy

Imports in Groovy are for using other packages and libraries in our code. Specific set of functionalities or features are bundled as Packages or libraries in Java and we can use these Packages or Libraries in our code using the import statement.

In Groovy the syntax for writing an import statement is as follows:

import PackageName

Here is a small example for package import in Groovy where we are importing a package called as MarkupBuilder:

import groovy.xml.MarkupBuilder

You can also import multiple packages with the * symbol like the following:

import groovy.xml.*

Importing like this will import all the packages under the XML. Another way of importing all the XML packages is:

import groovy.xml

If you are using IntelliJ IDEA editor, it will suggest these packages as and when you type the code. If you don’t have the IntelliJ IDEA editor installed, you can install it by checking Installing IntelliJ IDEA in Ubuntu Using Ubuntu Software article.

Related Posts