ec-groovy
is a small wrapper program that can be used to run groovy scripts according to Groovy API overview.
Problem
Your Groovy script may require an extra library in some cases. For example, the following script:
@Grapes([ @Grab('org.slf4j:slf4j-simple:1.5.11'), @Grab('mysql:mysql-connector-java:5.1.47'), ]) import org.slf4j.* import groovy.sql.* import java.sql.DriverManager DriverManager.registerDriver(new com.mysql.jdbc.Driver()) def logger = LoggerFactory.getLogger('sql') logger.info 'Initialize SQL' def sql = Sql.newInstance('jdbc:mysql://localhost:3306/ecloud', 'root', 'ecloud', 'com.mysql.jdbc.Driver') logger.info "Got myself a SQL connection: $sql"
results in the following failure in my test environment:
~/ecgroovy$ ec-groovy connectDB.groovy [main] INFO sql - Initialize SQL Caught: java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/ecloud java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/ecloud at connectDB.run(connectDB.groovy:13) ~/ecgroovy$
The cause of the failure is there is no suitable driver to connect to the MySQL database.
Solution
In order to fix the issue, the first thing is to download the library ("mysql-connector-java-8.0.21.jar" in this case) to be used as the driver to connect to the MySQL database.
Then there are two ways to let ec-groovy
to use the library.
1. use an option to tell ec-groovy
to load the library
Put "mysql-connector-java-8.0.21.jar" to the current directory and run ec-groovy
with the cp
option:
~/ecgroovy$ ls mysql-connector-java-8.0.21.jar mysql-connector-java-8.0.21.jar ~/ecgroovy$ ec-groovy -cp mysql-connector-java-8.0.21.jar connectDB.groovy Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary. [main] INFO sql - Initialize SQL [main] INFO sql - Got myself a SQL connection: groovy.sql.Sql@9a20cbd
Note you can put "mysql-connector-java-8.0.21.jar" wherever you like as long as you point to it correctly with the cp
option.
2. put the library to the folder from where ec-groovy
automatically loads the libraries
ec-groovy
automatically loads the libraries in <INSTALL_DIR>/utils/langs
when it runs. So you can put "mysql-connector-java-8.0.21.jar" there and there is no need to use the cp
option:
~/ecgroovy$ ls /opt/electriccloud/electriccommander/utils/langs/mysql-connector-java-8.0.21.jar
/opt/electriccloud/electriccommander/utils/langs/mysql-connector-java-8.0.21.jar
~/ecgroovy$ ec-groovy connectDB.groovy
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
[main] INFO sql - Initialize SQL
[main] INFO sql - Got myself a SQL connection: groovy.sql.Sql@9a20cbd
~/ecgroovy$
Which way is better? It depends on your use case.
Although this example is given in the command line, it also applies to a commander step which uses ec-groovy
as the shell.