Anton Moiseev On Software Development

Eclipse Integration with Hybris Groovy Console

This post explains how to set up Eclipse IDE in order to remotely execute Groovy scripts on the running Hybris instance. The post is based on and develops further the idea of the article eCommerce with Hybris: Debugging Hybris with Beanshell from Eclipse IDE. Make sure you are familiar with the topics explained there before continuing reading this post.


If you follow instructions from the previous post, you already know how to get the job done. But what are the areas we can improve there? Here is what I think can be done better:

  1. BeanShell. Although it's fairly simple language, a lot of people are already familiar with Groovy and often it's their language of choice when it comes to the scripting on the JVM. It is also my personal preference.

  2. Eclipse integration. If you draw attention to the existing approach, we actually write Java code in the source file within restricted area bounded with specially-treated comments. When you run the program your script is fetched from this area and converted to the BeanShell on the fly, then it is sent to the Hybris BeanShell endpoint. While it's definitely valid and working approach, it seems a bit complex and fragile to me.

Let's work towards improving the points we highlighted.

WARNING: The approach discussed below, right now works only on unix systems, because of the unix-spceific bash script used to post Groovy scripts to Hybris.

Adding Groovy Support

We don't want to pollute our project with debugging code, so we will create a separate project for our Groovy scripts. And since we are going to write our scripts in Groovy we need to add Groovy support to Eclipse. To teach Eclipse understand Groovy install this plugin. This post doesn't cover installation instructions, the process is well explained on the plugin's web page, you need to install it on your own.

After successfully installing Groovy-Eclipse plugin, create new Groovy project and add it to your workspace. The workspace should look similar to this:

Workspace after adding Groovy project

Since your scripts will be executed within Hybris process that knows nothing about your Groovy project, all the scripts should have default package, i.e. source files should be located right in the src directory:

Groovy scripts in the `src` directory

Despite the fact Groovy is dynamic language you'd rather use typed variables, to leverage auto-completion support. To make Hybris platform's classes visible in our scripts, add appropriate project to the Groovy project dependencies:

Adding Hybris dependencies to Groovy project

Posting Groovy Script To Hybris

Here is the interesting part. In order to post your scripts to Hybris Groovy endpoint we will use this bash script:

 1 #!/bin/bash
 3 # This script posts BeanShell/Groovy script to the
 4 # remote Hybris endpoint and parses the response.
 6 _usage_ ()
 7 {
 8   echo "Usage: ./hybris_script <path_to_script> [<hybris_url>]"
 9 }
11 case $# in
12   0) _usage_; exit 1 ;;
13   1) SCRIPT=$1; HYBRIS_URL="http://localhost:9001" ;;
14   2) SCRIPT=$1; HYBRIS_URL=$2 ;;
15   *) echo "Too many arguments."; _usage_; exit 1 ;;
16 esac
18 FILE_NAME=$(basename "$SCRIPT")
21 # Extract extension and obtain hybris url for script processing.
23 echo "Script extension: $SCRIPT_EXTENSION"
24 if [ "$SCRIPT_EXTENSION" == "bsh" ]
25 then
26   echo "BeanShell script obtained."
27   HYBRIS_SCRIPT_URL="/console/beanshell/execute"
28 elif [ "$SCRIPT_EXTENSION" == "groovy" ]
29 then
30   echo "Groovy script obtained."
31   HYBRIS_SCRIPT_URL="/console/groovy/execute"
32 else
33   echo "Not supported script type passed."
34   echo "Only .bsh and .groovy allowed"
35   exit 1
36 fi
38 # Post script to the remote endpoint and parse response.
41 echo "Posting $SCRIPT to the $FULL_URL..."
43 OUTPUT_TEXT=$(curl "$FULL_URL"                           \
44   -k -iL -b cookies.txt -c cookies.txt -s -u admin:nimda \
45   -H "Origin: $HYBRIS_URL"                               \
46   -H "Content-Type: application/x-www-form-urlencoded"   \
47   -H "Accept: application/json"                          \
48   -H "Referer: $FULL_URL"                                \
49   -H "X-Requested-With: XMLHttpRequest"                  \
50   -H "Connection: keep-alive"                            \
51   --data-urlencode "script@$SCRIPT"                      \
52   --data "commit=false")
54 echo "Obtained response:"
55 echo -e $OUTPUT_TEXT
  1. Line 13: Update URL to your project-specific value.
  2. Line 44: -u admin:nimda - change admin user's credentials if your settings differ from default ones.

IMPORTANT: Because we explicitly provide admin user's credentials along with the request, we don't need to modify Spring Security configuration to allow access to the Hybris Groovy console endpoint.

In order to integrate this script with Eclipse IDE, we need to configure it as "External Tool". Here is how to do this:

  1. Open Eclipse IDE
  2. In the main menu find Run -> External Tools -> External Tools Configuration... command and click on it:

    External Tools in the main menu

  3. External Tools Configuration dialog window should appear. Inside the window do following steps:

    1. Click Program list item on the left panel.
    2. Click New launch configuration toolbar button. In the appeared panel on the right, choose Main tab.
    3. In the Name text field type descriptive name for your custom configuration, e.g. Hybris Groovy Console.
    4. In the Location field specify full path to the <> shown above.
    5. In the Arguments text area specify single argument ${resource_loc} which always refers to full path of the file currently active in the Eclipse.
    6. Click Apply button to save your settings. Here is how the External Tools Configuration window should look like after completing these steps:

      External tools dialog window, Main tab

    7. Optionally you may want to disable building the entire workspace every time you launch a Groovy script. Navigate to Build tab and switch radio button to the The project containing the selected resource option:

      External tools dialog window, Build tab

NOTE: You can create several External Tools Configurations for different environments, e.g. test, staging, etc. Just provide URL for a specific environment as the second argument on the step 5.

Now we are all set and ready to execute Groovy scripts on a live Hybris instance. Let's try a simple example. Type following code in your GroovyMain.groovy file:

import de.hybris.platform.catalog.CatalogService
import de.hybris.platform.core.Registry

CatalogService catalogService = Registry
    .getBean("catalogService") as CatalogService

println "\nHybris catalogs:"
catalogService.allCatalogs.each {
    println "$ - $it.version"

As you can see full auto-completion support is available:

Auto-completion support

Execute Run -> External Tools -> Hybris Groovy Console command:

Hybris Groovy Console command

After running this command you should be able to see results of the script execution in the Eclipse console:

Result of Groovy script remote execution

You can get in touch with me at or follow me on Twitter at @antonmoiseev. I'm currently working on the book "Angular 2 Development with TypeScript" you can find it here.

comments powered by Disqus