ManyDesigns logo

How to use Eclipse to write and debug Groovy scripts

Posted March 27, 2013 by Giampiero Granatella

In this post we are going to see how to develop applications using Eclipse and Portofino 4. The traditional development of a web application in Eclipse requires the creation of a web project and its deployment on an application server such as Tomcat. This is certainly possible even with Portofino, but it has two drawbacks: first redeployment is slow and second this life cycle loses the capability of Groovy scripts to be edited on live systems.

In this post we propose the idea to create a project that points to the web application running under Tomcat. Therefore we can harness the power of the IDE and to maintain the capability to make changes on the live system without redeploy.

Why use an IDE? Portofino can be managed only through a browser and, optionally, with a text editor. But the use of an IDE such as Eclipse, facilitates to create and edit xml pages, jsp and groovy scripts. With Eclipse we can debug Groovy scripts, browse and search classes, have syntax highlighting, auto-completion, import and automatic functions refactoring.

Note. During realization of this post I used the latest version of Eclipse Juno and the bundle "Eclipse IDE for Java EE Developers".

Project setup

Within Eclipse, create a new Dynamic Web Project
 
 
STEP1. Set the project name and use the war expanded under Tomcat (ROOT direcetory) as "Location". We can ignore the other parameters and click on "Next".
 
 
 
STEP 2. We can remove the "src" directory that Eclipse created, and add the directory "apps / default / groovy" where shared groovy scripts are located.
 
 
 
STEP 3. We set the Context root to "/", or the name of the context of your webapp,  and the Content directory to "/ apps / default",
 
 
Click on "Finish" and wait that  the process ends.
 
To avoid dependency problems, we create a "User Library" with all the jars in WEB-INF/lib and the lib directory of Tomcat.
To do this right click on the "Build Path> Configure build path ..." and create this library.
 
 
Now our project is ready to be managed by Eclipse.
If you have never installed the appropriate Groovy plug-in, you still have problems editing scripts.

Groovy

To install the Groovy plug-in Groovy refer to the website http://groovy.codehaus.org/Install+Groovy-Eclipse+Plugin
Click on "Help> Install New Software ..." , add the following URL and selecte packages as shown (url changes depending on the version of Eclipse).
 
 
Restart Eclipse and now our Groovy files have the syntax highlighting, and we can use the automatic import and autocompletion.
 
 

Remote Debug

Next step is to use Eclipse to debug our live application. To do this we need:

  1. configure Tomcat to start in debug mode
  2. configure Eclipse to connect to the remote server
For these steps you can refer to http://wiki.apache.org/tomcat/FAQ/Developing.
 
To activate debug mode,  set two environment variables and starting Tomcat as shown below (the following code is for Linux or Mac)
export JPDA_ADDRESS=5005 export JPDA_TRANSPORT=dt_socket bin/catalina.sh jpda start
 
In Eclipse click on "Run->Debug Configurations...".  
 
 
Select a new "Remote Java Applications" and set the host and port, in our case is 5005,  the value of JPDA_ADDRESS variable.
 
 
And that's it. Put a break point in security.groovy, log in and you can use the Eclipse debug.
 

Conclusions

In this post we configured Eclipse to develop live web applications in Portofino. Developing with an IDE has many advantages, first of all the possibility to debug your live application.

You can use other Java IDEs such as NetBeans or IntelliJ,  the configuration is similar to what done in this post.

 

 

comments powered by Disqus