Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Kenneth A. Kousen - Making Java Groovy - 2014.pdf
Скачиваний:
50
Добавлен:
19.03.2016
Размер:
15.36 Mб
Скачать

292

APPENDIX A Installing Groovy

Figure A.1 The Groovy console, which comes with the Groovy distribution. Remember to go under the View menu and select Auto Clear Output On Run to make the tool far more practical.

The Groovy shell is essentially the REPL3 for Groovy (or even Java). Note that the response here is null because the println command has a void return type.

The Groovy console is a bit more useful. Start it with this command:

$ groovyConsole

On Windows, that spawns a separate process. On Macs and Unix flavors, the groovyConsole command locks up that particular shell, so you might want to run it in the background by appending an ampersand (&). The result looks like figure A.1.

The Groovy console appends its results to the output window, which can be problematic. Worse, if you type a line that throws an exception,4 the result window stops scrolling, even if you later fix the error. My recommendation, therefore, is to select the last entry under the View menu, entitled Auto Clear Output On Run. That will make the console clear the output every time you execute a script. The Groovy console includes an Abstract Syntax Tree browser, among other things. It’s useful even if you normally work with an IDE.

Speaking of IDEs, the next section documents their current level of support.

A.4 IDE support

If you’re an Eclipse user, the Groovy Eclipse plugin is state-of-the-art. To add it to an existing Eclipse distribution, use the update string found on the page at http://groovy

.codehaus.org/Eclipse+Plugin. The plugin can also be found at the Eclipse Marketplace. Eclipse has an annoying bug that requires the installation directory to be writable by the user. Groovy Eclipse cannot be installed into a so-called “shared” install, which

3Read-Eval-Print-Loop, discussed further in appendix B.

4I know you would never do that, but you know what your coworkers are like. They’re capable of anything.

www.it-ebooks.info

Installing other projects in the Groovy ecosystem

293

often includes the c:\Program Files directory on Windows. Simply move your Eclipse installation somewhere else, and you’ll be fine.

If you only want to use Groovy, the Groovy Eclipse plugin is sufficient. If you want to use Grails as well, then you can install the Groovy and Grails Tool Suite, GGTS. GGTS is a set of plugins based on Eclipse and is managed by Pivotal (formerly SpringSource). You can download GGTS from www.springsource.org/downloads/sts-ggts. Be careful: the site lists the STS downloads first and the GGTS downloads below that.

Both STS and GGTS come from the same code base. The difference is the initial set of plugins. GGTS comes with both the Groovy Eclipse plugin and Grails support that provides an entire Grails perspective, various wizards, keyboard shortcuts, and more.

The major IDE alternative is IntelliJ IDEA. The page at www.jetbrains.com/idea/ features/groovy_grails.html discusses its Groovy and Grails features. It even has Griffon support, which is pretty unusual at this point. IntelliJ IDEA is the tool of choice of most of the core Groovy, Grails, and Griffon team members, but it’s a commercial product and therefore requires a license.5 If you participate in an open source project or make presentations at your local Java/Groovy/Grails user group, you can get a free license, which is one more reason to participate in the open source world.

The web page athtp://groovy.codehaus.org/IDE+Support lists plugins and support for other IDEs, ranging from Emacs to TextMate to UltraEdit. If you can’t find the one you’re interested in, be sure to ask on the mailing lists. Somebody will know and tell you where to find what you need.

A.5 Installing other projects in the Groovy ecosystem

The GVM tool currently will install and manage Groovy, Grails, Griffon, and Gradle distributions, among other projects.6 That’s the easiest way to proceed if you’re on a Mac or Unix distribution. Again, on Macs both HomeBrew and MacPorts have options for the same set of projects. On Windows, Groovy has the installer mentioned earlier in this chapter.

Grails is always a ZIP file that you download and unzip. Then you set an environment variable (GRAILS_HOME in this case) and add the bin subdirectory to your path. Griffon and Gradle work much the same way.

Note that all of these projects have their own source code repositories on GitHub. You can always clone the distribution and build it yourself, though that tends to get involved. See the respective project pages for details. One of the best things about GitHub is that you can browse the source code without downloading anything. It’s a good idea to get familiar with the test cases contained in the various projects, because they’re the executable documentation for each. Web pages can go out of date, but

5There’s a community edition that didn’t used to offer Grails support, but that may be changing. Be sure to check the website for current capabilities.

6The current list of candidates is Groovy, Grails, Griffon, Gradle, Lazybones, Vertx, and Groovyserv.

www.it-ebooks.info

294

APPENDIX A Installing Groovy

continuous integration servers execute test cases all the time. When they break everybody knows about it, and they get fixed right away.

The only other project discussed extensively in this book is Spock. Spock is a library rather than a framework and is usually installed as part of a Gradle (or Maven) build. Its source code is on GitHub, too.

www.it-ebooks.info

appendix B Groovy by feature

Some people learn by example. Some people learn by feature. In this book I’m trying to satisfy both. If you’re a Java developer with only passing familiarity with Groovy, hopefully either this appendix or chapter 2, “Groovy by example,” will bring you up to speed on the Groovy language.

This appendix walks through most of the major features of Groovy and provides short snippets of code illustrating them. While this chapter does not claim to be an exhaustive reference like Groovy in Action (Manning, 2007; called GinA in the rest of this appendix), it has a couple of features that favor it over the more comprehensive treatment: (1) it’s considerably shorter, and (2) it has the words “Don’t Panic!” written in nice, friendly letters in the appendix (in this sentence, actually).1 More seriously, in this appendix I review the major features of the Groovy programming language that are used throughout the book.

Because this isn’t going to be a comprehensive treatment, I’ve chosen aspects of Groovy to review based on two criteria: (1) how often they’re used in practice and (2) how much they offer an advantage over corresponding features in Java (assuming the corresponding feature in Java even exists). After getting the basics of Groovy out of the way (like how to run Groovy programs and basic data types like numbers and strings), I’ll move on to issues like collections, I/O, XML, and more. Some topics, like SQL, are covered in other chapters, but you’ll find the essentials here.

1For those born too late, that was a Hitchhiker’s Guide to the Galaxy reference. I could go on to say that this chapter “contains much that is apocryphal, or at least wildly inaccurate,” but that probably wouldn’t be good for sales.

295

www.it-ebooks.info

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]