But, I don't know Groovy. Even more, I don't know Java. And I think it would be useful to document my own process. That said, let's start in the beginning…
Disclaimer Almost all the documentation I found for beginners assumes the student knows Java; most tutorials start with the differences between Groovy and Java. As I don't know Java, this posts are going to be slightly different. Maybe I'll make some comparisons with Python, but I'll try to keep those comparisons as anecdotal notes.
You have a Linux operating system. You know the command line. You know git and github. You know something about programming (variables, methods, class…). You can check the specific syntax on your own.
What is Groovy
From the oficial documentation:
To manage and install Groovy components, it's recommended to use GVM tool.
$ curl -s get.gvmtool.net | bash
Once you have installed gvm, you need to add the following to your .bashrc file:
#THIS MUST BE AT THE END OF THE FILE FOR FVM TO WORK!! [[ -s "/path/to/.gvm/bin/gvm-init.sh" ]] && source "/path/to/.gvm/bin/gvm-init.sh"
Now we can install Groovy:
$ gvm install groovy
Good! Next step.
Now you have installed Groovy, you have some tools like goovyConsole, or groovysh. Go to a terminal and type:
A groovy interpreter is open, and we're going to make our Hello, world with something like this:
aran :: ~/formacion/groovy101 » groovysh Groovy Shell (2.3.9, JVM: 1.8.0_25) Type ':help' or ':h' for help. ----------------------------------------------------------------- <strong>groovy:000> println "Hello, world!" Hello, world!</strong> ===> null groovy:000>
WIN! Our first Groovy command ;-) Keep going!
Note: for specific issues concerning groovysh, check this page.
Now, create a file called hello.groovy and write the following:
println "Hello, world!"
And in the terminal, type:
$ groovy hello.groovy
You should have something like this:
aran :: ~/formacion/groovy101 » groovy hello.groovy Hello, world! aran :: ~/formacion/groovy101 »
Ou yeah! This is the beginning; next steps: the infinite, and beyond.
It's the moment to build a simple calculator. Let's create a directory with these two files:
aran :: ~/formacion/groovy101 » ls groovyCalc Calc.groovy CalcTest.groovy
In the Calc.groovy file, you have to write the actual code, while in the CalcTest.groovy file (using jUnit), you write the tests. In this repository you can find an example with code and tests. You can clone it, or write one by yourself (recommended).
To run these tests:
$ groovy CalcTest.groovy
It only displays errors; so “no news, good news” ;-) Now you can extend the calculator, or you can create a new simple project to learn the Groovy syntax. And don't miss the tests, they are also very important!
Even if those tests are very very simple, I think it's useful to integrate them in a continous integration tool. I have tried with Travis CI, but you could use any other you feel confortable with.
If you have never used Travis, you can read this post Travis 101 (spanish) to get used. The .travis.yml file for this project would be:
language: groovy groovy: - 2.3.9 jdk: - oraclejdk8 script: - groovy CalcTest.groovy
Note: the jdk line is to force to use java8, instead of java7 (default). Currently, it's not supported openjdk8, only oraclejdk8.
You can check the status of this repository.
And that's all for now. From this moment, you can make some katas and exercises to learn the Groovy syntax. Next steps, we will learn something about gradle and spock. But now it is your turn to improve.
I would love to read your comments and feedback on this kind of posts :)