In many situations little additional scripts make our lives as developers easier: For example the Groovy System Script to customise Jenkins when building a NATURAL code base mentioned above, or a Groovy Script Transformation to perform repeated code changes in a large Java project using WalkMod, or a custom build during a Maven build, using any language supported by the Apache Bean Scripting Framework, e.g. Groovy, Ruby, Python or others. Now if the scripts are getting complicated IDE support would be nice.
Getting support for the language in the IDE
Depending on the language, most IDEs need installation of a plugin or extension. I will briefly describe the steps to set up Groovy support into Eclipse because it is more elaborate and the organisation I initially wrote this documentation used Eclipse. Getting plugins for IntelliJ IDEA is straight forward.
- First obtain the version of Eclipse you are using. Customised distributions like the Spring Tool Suite, IBM/Rational products or NaturalONE follow different version schemes than Eclipse. Navigate to the Help menu, item About, button Installation Details, tab Features, line Eclipse Platform to see the real version. For example, NaturalONE 8.3.2 is based on Eclipse 4.4.
- The Groovy plugin for Eclipse is GrEclipse. For each major version of Eclipse there is a matching version of GrEclipse. The section Releases lists the Update Site for each release. Find the suitable version. (If you use a newer Eclipse, maybe you have to use a snapshot version instead of a release version.) Copy the URL of the Update Site.
- The section How to Install explains in detail how to continue and install GrEclipse.
Enabling support for the language in the project
Next we configure the project for the additional language. In Eclipse this is often possible through the (right click) context menu of the project, menu item Configure. I usually do not bother and edit Eclipse's
.project
file directly to add the needed natures and build commands. Adding Groovy to a non-Java project needs adding to .project
,<projectDescription> <!-- existing configuration --> <buildSpec> <!-- existing configuration --> <buildCommand> <name>org.eclipse.jdt.core.javabuilder</name> <arguments> </arguments> </buildCommand> </buildSpec> <natures> <!-- existing configuration --> <nature>org.eclipse.jdt.groovy.core.groovyNature</nature> <nature>org.eclipse.jdt.core.javanature</nature> </natures> </projectDescription>and
.classpath
<classpath> <!-- existing configuration --> <classpathentry exported="true" kind="con" path="GROOVY_SUPPORT"/> <classpathentry exported="true" kind="con" path="GROOVY_DSL_SUPPORT"/> </classpath>Similar changes are needed for Ruby (using
org.eclipse.dltk.core.scriptbuilder
and org.eclipse.dltk.ruby.core.nature
) and Python (org.python.pydev.PyDevBuilder
and org.python.pydev.pythonNature
). If I am not sure, I create a new project for the target language and merge the .project
s manually. Sometimes other files like .classpath
, .buildpath
or .pydevproject
have to be copied, too.Enabling support for libraries
Adding a nature to
.project
gives general support like syntax highlighting and refactoring for the respective language but there is no completion of library classes. Especially when using new libraries, like Jenkins Core, using code completion helps exploring the new API because you get a list of all possible methods to call. The trick is to add the needed dependency to the project in a way that it is not accessible from or packaged together with the production code.An Example: Adding Jenkins System Script support to a NaturalONE project
To add dependencies to the NaturalONE project earlier, I converted the Java/Groovy project to Maven (by setting another nature and builder) and added the needed classes as fake dependencies to the
pom.xml
.<dependencies> <!-- other dependencies --> <dependency> <groupId>org.jenkins-ci.main</groupId> <artifactId>jenkins-core</artifactId> <version>2.58</version> <scope>test</scope> <!-- (1) --> </dependency> <dependency> <groupId>org.jenkins-ci.plugins</groupId> <artifactId>subversion</artifactId> <version>2.7.2</version> <scope>test</scope> </dependency> </dependencies>Groovy has optional typing and after adding the types to the definitions and arguments, Eclipse will pick up the type and we get code completion on available fields and methods:
import hudson.model.Build def Build build = Thread.currentThread()?.executable build._ // <-- code completion, yeah!This made developing and understanding the script to find modified files much easier. The Jenkins dependencies in the
pom.xml
are never used, as the System Script runs inside Jenkins where the classes are provided. The dependencies are declared to "trick" Eclipse into analysing them and providing type information. The scope of the dependencies is set to test
(see line (1) in code snippet above) so the dependencies are not packaged and cannot be called from the production code. This zipped repository contains the NATURAL project together with all the Eclipse configuration files.Another Example: Adding WalkMod Transformation support to a Maven project
Another situation where I wish for script support in Eclipse is when writing WalkMod Script Transformations. WalkMod is an open source Java tool that can be used to apply code conventions automatically. Read this tutorial by Raquel Pau to see how WalkMod works. WalkMod allows for Groovy scripts to define code transformations which manipulate the AST of Java classes. Navigating the AST is difficult in the beginning.
After adding the Groovy nature as shown in the previous example, the relevant dependencies to get code completion for the AST are
<dependencies> <dependency> <!-- API --> <groupId>org.walkmod</groupId> <artifactId>walkmod-core</artifactId> <version>3.0.4</version> <scope>test</scope> </dependency> <dependency> <!-- AST --> <groupId>org.walkmod</groupId> <artifactId>javalang</artifactId> <version>4.8.8</version> <scope>test</scope> </dependency> <dependency> <groupId>com.squareup</groupId> <artifactId>javapoet</artifactId> <version>1.10.0</version> <scope>test</scope> </dependency> </dependencies>In case of Walkmod transformations, the interesting class is (
org.walkmod.javalang.ast.CompilationUnit
) and it is already in scope as variable node
. To add type information we need to alias it:import org.walkmod.javalang.ast.CompilationUnit def CompilationUnit cu = node cu._ // <-- code completion on available fields and methodsConclusion
Supporting scripts in Groovy, Ruby or Python make our lives easier. With some additional extensions or plugins and some tweaks in the project configuration our IDEs will support the scripting language. Why should we accept code editing like in the early 1990s?
No comments:
Post a Comment