Chapter 11. Building polyglot projects
This chapter covers
- Managing JavaScript with Gradle
- Building a Gradle project with Java, Groovy, and Scala
- Exploring plugin support for other languages
In recent years, the interest in polyglot programming and its application in real-world, mission-critical software stacks has skyrocketed. The term polyglot programming was coined to describe projects that incorporate more than one language. That’s particularly true for the Java world. For web developers, the world can no longer do without a potpourri of JavaScript, corresponding frameworks, libraries, and CSS. Backend code or desktop applications are no longer built with Java alone. Other JVM-based languages like Groovy or Scala are added to the mix. It’s all about picking the right tool for the job. In this chapter, we’ll discuss how Gradle faces the challenge of organizing and building polyglot projects by using your To Do application as an example.
A rich user experience has become the key to success for any application. This holds true for desktop, mobile, and web applications. Today, users demand the same look and feel for web applications as they do for native applications. Long gone are the days when customers were happy with static HTML pages and synchronous server roundtrips.
JavaScript has become the most popular programming language for the web that can meet this demand. Developers no longer have to depend on plain JavaScript to implement their features. Many application development frameworks and libraries (like jQuery, Prototype, and Backbone, to name just a few) are available to simplify the use of JavaScript development and to smooth the rough edges this language introduces.
The days when Java was the one and only language used for writing applications are over. Today, when we think about Java, we also refer to it as a mature development platform: the Java Virtual Machine (JVM). There’s a wide range of languages running on the JVM that are suitable for enterprise or server-side development. Among them are popular languages likes Groovy, Scala, Clojure, and JRuby. Why would you even want to use a different language than Java or mix them within a single project? Again, it’s all about the right tool for the job. You may prefer Java for its statically typed nature and library support to implement your business logic. However, Java’s syntax isn’t a natural fit for producing DSLs. This is where other, more suitable languages like Groovy come into play.
So far, we’ve discussed how to build Java applications, but there’s more to Gradle. It has first-class support for other JVM languages. In this section, we’ll look at how to organize and build Groovy and Scala projects and how to bring them all under one umbrella within a single project. Before we dive into the details, let’s take a step back and review some of the inner workings of the Java plugin, which to an extent builds the foundation for other language plugins.
There are far more languages out there than we could possibly discuss in this book. Some of these languages are directly supported by a core plugin; others can be integrated with your build through a plugin developed by the Gradle community. Table 11.1 lists some of the more popular language plugins.
Even if you don’t find an appropriate plugin for the language you’re trying to incorporate into your build, you’re not out of luck. You learned that Ant tasks and Java APIs can be wrapped with a Gradle task. The same technique is used by the Groovy plugin, for example, which internally invokes the groovyc Ant task. Alternatively, you can always execute a compiler from the command line by creating an enhanced task of type Exec.
JavaScript has been the dominant language for creating dynamic web experiences for over a decade. Obviously, page-loading times influenced by the size of the JavaScript files play a significant role for end users. JavaScript files can be merged and minified to improve the page rendering performance. You learned that Gradle can help automate otherwise manual steps to perform these actions and integrate them with the development lifecycle of your To Do application. We didn’t stop there. We also explored how to simplify the required configuration of your JavaScript build code by using the community JavaScript plugin. Later, you wrapped existing Grunt build code with Gradle to provide a single, unified control unit for automating all components of your application.
The JVM-based languages Groovy and Scala are directly supported within a Gradle build. Gradle ships with first-class plugin support for both languages. Each language plugin builds on top of the Java base plugin to enforce a separation of capabilities from conventions. You transformed some of the existing To Do application Java classes into Groovy and Scala equivalents to demonstrate the use of these plugins. Source set conventions aren’t set in stone. You learned how to reconfigure them in case your project needs to adapt to a legacy project layout. Groovy and Scala source code can coexist with Java source code in a single project. Bidirectional dependencies between Java and Groovy or Scala require the use of joint compilation. We discussed how to prepare the compiler for handling such a scenario. In the last part of this chapter, we touched on other programming languages supported by Gradle.