How Do You Troubleshoot “groovy.lang.MissingPropertyException: No such property” when running a Groovy Program?

Problem scenario
You are running a Groovy program.  

Here is your code:
def input = System.console().readLine ‘Please provide some input: ‘
def x = input
def boardArray = new String[2]
boardArray[0] = “”
boardArray[1] = “”
boardArray[1] = $x
println boardArray[1]

When you run it (i.e., with groovy foobar.groovy), you get this error:

“Caught: groovy.lang.MissingPropertyException: No such property: $x for class:
groovy.lang.MissingPropertyException: No such property”

What should you do?

 » Read more..

How Do You Iterate through Data Members (the Individual Components) of a Groovy Object?

Problem scenario
You have an object that you want to sequentially pass through.  That is, you want to print either the value or the key/name and value pair of every member data item of the object.  How do you print every individual data member of a given object in Groovy?

Solution
Invoke the “.properties.each” keyword.  This built-in feature will allow you to do just this. 

 » Read more..

In a Groovy Program How Do You Assign an Element in an Index to Be the Value of a Given Variable?

Problem scenario
You are trying to write a simple Groovy program.  You have an array of individual strings.  How do you assign one of them to be the value that a variable is?

Solution
To assign an element of an array, should have quotes around the variable and the $ symbol and the syntax ” as String” should follow the closing quote mark.

def input = System.console().readLine ‘Please provide some input: ‘
def x = input
def boardArray = new String[2]
boardArray[0] = “”
boardArray[1] = “”
boardArray[1] = “$x” as String  

 » Read more..

How Do You Create a Class for Objects with Many Data Members and Access One Specific Data Member in Groovy?

Problem scenario
You want to use Groovy to create a class and an object that is composed of many individual member data types.  How do you do this and access an individual data type of the object?

Solution
This Groovy program creates two objects from the same class using different syntax.  One method uses syntax “[]” and another method uses the “new” keyword.

@groovy.transform.Canonical
class Program {
   

 » Read more..

How Do You Use a Jenkins Pipeline with a Scripted Syntax?

Updated and re-tested on 9/24/19
Problem scenario
You read about what Jenkinsfiles are.  Coveros.com defines them as ‘Jenkinsfiles, using a domain specific language based on the Groovy programming language, are persistent files that model delivery pipelines “as code”, containing the complete set of encoded steps (steps, nodes, and stages) necessary to define the entire application life-cycle.’

You want to use a Scripted Pipeline in Jenkins (via a Jenkinsfile). 

 » Read more..

How Do You Write a Groovy Program to Read in User Input from the Keyboard and Print It Out?

Problem scenario
You want to test out Groovy as a scripting language.  You want a Groovy program to read in user input and print it out to the screen.  How do you do this?

Prerequisite
This assumes that you have already installed Groovy on a Linux server.  If you are running a Red Hat derivative (e.g., CentOS, RHEL, or Fedora), click on this link

 » Read more..

How Do You Run a Groovy Program on a Red Hat Derivative Linux Server?

Problem scenario
You want to use a Java Virtual Machine language on a CentOS/RHEL/Fedora server.  You want to test out the language too.  What should you do?

Prerequisites
Install Groovy; this command should work (assuming your server is configured to work with a yum repository with groovy):  sudo yum -y install groovy
# If your yum repositories do not have Groovy, see this posting.

 » Read more..

How Do You Run a Groovy Program on a Debian or Ubuntu Server?

Problem scenario
You want to use a Java Virtual Machine language on an Ubuntu Linux server.  You want to test out the language too.  What should you do?

Prerequisites
Install Groovy version 1; this command should work:  sudo apt-get -y update; sudo apt-get -y install groovy

Procedures
1.  Create a file called hello.groovy with the following five lines:

class Example {
  

 » Read more..

How Do You Install Groovy Version 2 on Debian or Ubuntu Linux?

Problem scenario
You want to install Groovy version 2 on Debian or Ubuntu Linux.  What do you do?

Solution
Run these two commands:

sudo apt-get -y update
sudo apt-get -y install groovy2

 » Read more..

How Do You Install Groovy on Centos 7.X?

Problem scenario
You want to install Groovy on CentOS 7.x but no yum repositories are configured for directly installing it.  What should you do?

Solution
Run these commands:

sudo yum -y install ant ant-junit antlr-tool apache-commons-cli apache-commons-logging apache-ivy bsf jansi jline junit objectweb-asm tomcat-jsp-2.2-api tomcat-servlet-3.0-api xstream

curl http://rpmfind.net/linux/centos/7.4.1708/os/x86_64/Packages/groovy-1.8.9-7.el7.noarch.rpm > /tmp/groovy-1.8.9-7.el7.noarch.rpm

sudo rpm -i /tmp/groovy-1.8.9-7.el7.noarch.rpm

 » Read more..