groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Richard Heintze <sieg_hein...@yahoo.com>
Subject Re: Wanted: Help converting groovy script to "groovy -e"
Date Fri, 04 Sep 2015 22:23:26 GMT
I don't think it is a *nix problem. I tried it on an Unbuntu docker instance and I get the
exact same error.
Please see http://docs.groovy-lang.org/latest/html/documentation/grape.html .
I believe the problem is with annotations: they don't work with 'groovy -e' for some reason
and you need to use the functional version instead.Unfortunately, I'm having trouble figuring
out the functional version of @GrabConfig(systemClassLoader=true).
ThanksSiegfried  


     On Friday, September 4, 2015 1:19 PM, Owen Rubel <orubel@gmail.com> wrote:
   

 Do you have a VM you can run Linux in? Windows in the only non-NIX based OS out there now
and considering your code will most likely run on a NIX server, it might be a good idea to
run in the environment it is built for.
Owen Rubel
415-971-0976
orubel@gmail.com
On Fri, Sep 4, 2015 at 9:13 AM, Richard Heintze <sieg_heintze@yahoo.com> wrote:

  The script below works with groovy 2.3.0-beta-2 with the groovy program. I want to move
the SQL code to separate text files and execute it from (1) "groovy -e" (2) groovy-console
and (3) groovysh.
After looking at http://www.techper.net/2010/04/19/groovy-grape-adding-dependencies-to-root-classloader/ here
is my first attempt using Cygwin on windows 8 and groovy that is not working:

groovy -e "import groovy.sql.Sql
import groovy.xml.MarkupBuilder;
import groovy.sql.Sql
 
def classLoader = this.getClass().getClassLoader();
while (!classLoader.getClass().getName().equals('org.codehaus.groovy.tools.RootLoader')) {
  classLoader = classLoader.getParent()
}
groovy.grape.Grape.grab(group:'com.h2database', module:'h2', version:'1.4.188')
def sql = Sql.newInstance('jdbc:h2:mem:test_mem', 'sa', '', 'org.h2.Driver');"
Caught: java.lang.ClassNotFoundException: org.h2.Driver
java.lang.ClassNotFoundException: org.h2.Driver
 at script_from_command_line.run(script_from_command_line:10)


As you can see, I'm trying to convert the @GrabConfig which does not seem to work with "groovy
-e".

ThanksSiegfried



This works:
import groovy.sql.Sql
import groovy.xml.MarkupBuilder
@GrabConfig(systemClassLoader=true)
@Grab(group='com.h2database', module='h2', version='1.4.188')sqltext = [""" 
create table gov_unit (
 id  integer NOT NULL,
 parent_id integer DEFAULT 3,
 name  varchar(10),
 type  varchar(8),
 constraint gov_unit_pk 
  primary key (id),
 constraint gov_unit_type_chk
  check (type in ('County','Township','City','State')),
        constraint gov_unit_loop
                foreign key (parent_id)
                references gov_unit
 );
insert into gov_unit values (3,null,'Michigan','State');
insert into gov_unit values (2,3,'Alger','County');
insert into gov_unit values (1,2,'Munising','City');
insert into gov_unit values (4,2,'Munising','Township');
insert into gov_unit values (5,2,'Au Train','Township');
insert into gov_unit values (6,3,'Baraga','County');
insert into gov_unit values (7,3,'Ontonagon','County');
insert into gov_unit values (8,7,'Interior','Township');
insert into gov_unit values (9,3,'Dickinson','County');
insert into gov_unit values (10,3,'Gogebic','County');
insert into gov_unit values (11,3,'Delta','County');
insert into gov_unit values (12,11,'Masonville','Township');
""","""
WITH RECURSIVE gov (depth, id, parent_id, name, type) AS( 
    SELECT 1 AS depth, parent.* FROM gov_unit parent WHERE parent.parent_id IS NULL
  UNION ALL
    SELECT parent.depth+1 AS depth, child.* FROM  gov parent, gov_unit child WHERE child.parent_id
= parent.id
)
SELECT * FROM gov ;
"""]
 def sql = Sql.newInstance("jdbc:h2:mem:test_mem", "sa", "", "org.h2.Driver")
sql.execute sqltext[0]
 
def xml = new groovy.xml.MarkupBuilder(new FileWriter(java.io.FileDescriptor.out))
 
xml.table (name:"gov_unit") {
  sql.rows(sqltext[1]).each{ row->
   xml.gov_unit (
    row
   ) }
}




  
Mime
View raw message