first steps

HI,

for the newbies to groovy and grails
There are many things for the experts, butfor the newbies, sometimes it´d a hard time, when justa little brick is missing.

I try to show may way to groovy and grails

Sunday 27 May 2012

Groovy and creating of PDF (iText)

For some next test, I want to create a PDF
(later with data of the database)

It´s the first time, I combine a Java-Lib with groovy.
It seems, that iText is state of the the art for producing PDFs.

http://itextpdf.com/

I found only few combination at google: Groovy and iText.
This link lead to empty pages (but the contents looked good)
http://swik.net/technology/dzone.com:+tech+links/Generate+a+PDF+book+with+groovy+and+iText/by7jg

This one was a great help.
http://milkedeek.wordpress.com/2012/01/03/itext-on-the-jvm/

Before the information is lost, I copy it to here.

It was very easy, strait forward. Copy th download of  iText to groovy/lib
and the imports will the solved. Some magic with Java for the newbee,
but it realy works.

goovy pdf.groovy  and the PDF-File is created


Groovy

I followed some Groovy talks during Devoxx 2011 and I was impressed by the language, but I haven’t been able to test it out, up until now. As with Jython, it was pretty easy to install and to import iText into a project. It’s actually the same process as you would import a jar into a Java project. Assuming that you’re able to do that without instructions, so here’s the Hello World code for Groovy:

01/*
02 * Created on 28-dec.-2011
03 * Creating a simple Hello World pdf in Groovy using iText 5.1.3
04 * @author: iText
05 */
06import com.itextpdf.text.Document
07import com.itextpdf.text.Paragraph
08import com.itextpdf.text.pdf.PdfWriter
09 
10// step 1
11def document = new Document()
12println("Document Created")
13 
14// step 2
15PdfWriter.getInstance(document, new FileOutputStream("document.pdf"))
16println("PdfWriter Created")
17 
18// step 3
19document.open()
20println("Document Opened")
21 
22// step 4
23document.add(new Paragraph("Hello Groovy!"))
24println("Content Added")
25 
26// step 5
27document.close()
28println("Document Closed")

As you can see, the code is straightforward and simple and it looks pretty much like Jython. I prefer Groovy over Jython because it’s much faster and actually feels like a JVM language and not like a port of another language. Still a lot of love for Python though.


Sunday 22 April 2012

comparison groovy and other languages

A very nice comparison, Groovy and other languages

http://pleac.sourceforge.net/

groovy:  http://pleac.sourceforge.net/pleac_groovy/index.html
PHP:  http://pleac.sourceforge.net/pleac_php/index.html


Sunday 15 April 2012

groovy: how to compile and run a groovy-pogramm stand alone

 Hi,

as groovy is interpreted (better: compiled every time) as it runs,
I found a nice idea, to compile the programm and let it run.

for the complete solution, look at  :
http://docs.codehaus.org/display/GROOVY/WrappingGroovyScript

very easy going

 fest@maxdata:~/intranet/htdocs/gutacht/versand> groovy wrapper.groovy -c -m pdf
     [echo] Compiling pdf.groovy
      [jar] Building jar: /home/fest/intranet/htdocs/gutacht/versand/pdf.jar
     [echo] Run script using: 'java -jar pdf.jar ...'
fest@maxdata:~/intranet/htdocs/gutacht/versand>
fest@maxdata:~/intranet/htdocs/gutacht/versand>
fest@maxdata:~/intranet/htdocs/gutacht/versand> java -jar pdf.jar 1 2 3
Anzahl Argumente 3
Usage:  pdf  Gutachten-nr  Datei  Zeile1  Zeile
fest@maxdata:~/intranet/htdocs/gutacht/versand>
This is the wrapper.groovy
/*
 * Copyright 2002-2007 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * Wrap a script and groovy jars to an executable jar
 */
def cli = new CliBuilder()
cli.h( longOpt: 'help', required: false, 'show usage information' )
cli.d( longOpt: 'destfile', argName: 'destfile', required: false, args: 1, 'jar destintation filename, defaults to {mainclass}.jar' )
cli.m( longOpt: 'mainclass', argName: 'mainclass', required: true, args: 1, 'fully qualified main class, eg. HelloWorld' )
cli.c( longOpt: 'groovyc', required: false, 'Run groovyc' )

//--------------------------------------------------------------------------
def opt = cli.parse(args)
if (!opt) { return }
if (opt.h) {
  cli.usage();
  return
}

def mainClass = opt.m
def scriptBase = mainClass.replace( '.', '/' )
def scriptFile = new File( scriptBase + '.groovy' )
if (!scriptFile.canRead()) {
   println "Cannot read script file: '${scriptFile}'"
   return
}
def destFile = scriptBase + '.jar'
if (opt.d) {
  destFile = opt.d
}

//--------------------------------------------------------------------------
def ant = new AntBuilder()

if (opt.c) {
  ant.echo( "Compiling ${scriptFile}" )
  org.codehaus.groovy.tools.FileSystemCompiler.main( [ scriptFile ] as String[] )
}

def GROOVY_HOME = new File( System.getenv('GROOVY_HOME') )
if (!GROOVY_HOME.canRead()) {
  ant.echo( "Missing environment variable GROOVY_HOME: '${GROOVY_HOME}'" )
  return
}

ant.jar( destfile: destFile, compress: true, index: true ) {
  fileset( dir: '.', includes: scriptBase + '*.class' )

  zipgroupfileset( dir: GROOVY_HOME, includes: 'embeddable/groovy-all-*.jar' )
  zipgroupfileset( dir: GROOVY_HOME, includes: 'lib/commons*.jar' )
  // add more jars here

  manifest {
    attribute( name: 'Main-Class', value: mainClass )
  }
}

ant.echo( "Run script using: \'java -jar ${destFile} ...\'" )

groovy: command line parameter / argument handling

Hi,

used from C, but there is not argc, argv.

it´s

args (Arry og arguments) and
args.size()  number of arguments

Like this

if ( args.size() != 4  )
  { println "Anzahl Argumente " + args.size()
    println "Usage:  pdf  Gutachten-nr  Datei  Zeile1  Zeile"
    return
  }

def nr = args[0]
def datei = args[1]
def zeile1 = args[2]
def zeile2 = args[3]

Wednesday 11 April 2012

groovy first example: Hello World

the must in every language, teh hello world

println "hello World"

nothing more than this.

make it more complicate
def hello="hello"
def world="world"
println hello+" " +world
println "$hello $world"
Put this in a file  hello.groovy

and command
groovy hello.groovy 
So nice feature,
no ;
no main
no $  (only in the strings)
no ()   after the function println, as long, as all is cllear enough 

And the "def" gives a little more clarity,
other than in PHP,where every varibale, you write $abc or $abd
are varibales implizit declared.





Installing groovy

Look  at the introduction at
http://groovy.codehaus.org/Installing+Groovy

Short abstract
These instructions describe how to install a binary distribution of Groovy.
  • first, Download a binary distribution of Groovy and unpack it into some file on your local file system
  • set your GROOVY_HOME environment variable to the directory you unpacked the distribution
  • add GROOVY_HOME/bin to your PATH environment variable
  • set your JAVA_HOME environment variable to point to your JDK.

It did it on windows,

first test is a DOS command-windows and there print  "groovy -version" and you see,
wether all is fine and installed


Grails: first steps

What are the first steps to grails heaven ?

http://groovy.codehaus.org/ (groovy)
http://grails.org/ (Grails, comes with groovy included)

a nice IDE (STS) comes comes
http://www.grails.org/STS+Integration

How to install grails, an excellent leed in grails
http://www.grailsexample.net/  (still unter construction)

But in the next step, we start with groovy.

some more reference to the newbie 
http://groovy.codehaus.org/For+those+new+to+both+Java+and+Groovy