JavaOne 2016
It is time to move your Java EE builds over to Gradle! Gradle continues to gain momentum across the industry. In fact, Google is now pushing Gradle for Android development. Gradle draws on lessons learned from both Ant and Maven and is the next evolutionary step in Java build tools. This session covers the basics of switching existing Java EE projects (that use Maven) over to Gradle and the benefits you will reap, such as incremental compiling, custom distributions, and task parallelization. You’ll see demos of all the goodies you’ve come to expect, such as integration testing and leveraging of Docker. Switching is easier than you think, and no refactoring is required.
3. Introducing Gradle
• Open source build automation system
• Apache 2.0 License
• Builds upon Apache Ant and Maven
• First released in 2007
• Uses a Groovy-based DSL (not XML)
• Uses directed acyclic graph to determine build order
• Supports multiple languages: Java, C++, C, etc.
• Rich plug-in architecture
• Convention over configuration but easily
customized/adapted
4. Introducing Gradle
• Build file can be versioned like dependencies.
Ever run Ant 1.9 file with Ant 1.6?
• Background daemon reduces build-time
• Supports incremental builds
• Built-in profiling support
• Build projects in parallel and some tasks*
• Built-in Ant/Maven integration
• Supported central repositories:
• Maven Central
• Jcenter
• Ivy
8. Questions
1. Do you need to learn Groovy?
No (Good idea)
2. Do you need to completely refactor your code base?
No
3. Do you need additional IDE plugins?
Maybe
4. Do you need to change your build process?
Depends
5. Do you need to port your entire build system over?
No – can port over individual modules
6. Can you embed custom Ant logic?
Yes
9. Questions…
7. Must all dependencies originate from a repository?
No
8. Can artifacts be pushed to a repository?
Yes
9. Can Jenkins initiate Gradle builds?
Yes
10. Why Gradle for Java EE?
• Java EE projects are:
• Large
• Complex
• Contain many dependencies
• Ant lacks dependency management
• Large Ant files are a nightmare to debug
• Maven isn’t flexible
• Custom plugins aren’t the solution
• Evolving slowly
11. Installation
• Installation similar to Ant/Maven
• Download and install from gradle.org
• Set environment variables:
• GRADLE_HOME
• PATH (to GRADLE_HOME/bin)
• gradle = ant = mvn
17. Command line – listing tasks
gradle –q tasks
------------------------------------------------------------
All tasks runnable from root project
------------------------------------------------------------
Build tasks
-----------
assemble - Assembles the outputs of this project.
build - Assembles and tests this project.
buildDependents - Assembles and tests this project and all
projects that depend on it.
buildNeeded - Assembles and tests this project and all projects
it depends on.
classes - Assembles classes 'main'.
clean - Deletes the build directory.
jar - Assembles a jar archive containing the main classes.
testClasses - Assembles classes 'test'.
18. Tasks Continued…
Build Setup tasks
-----------------
init - Initializes a new Gradle build. [incubating]
wrapper - Generates Gradle wrapper files. [incubating]
Documentation tasks
-------------------
javadoc - Generates Javadoc API documentation for the
main source code.
19. Tasks Continue…
Help tasks
----------
components - Displays the components produced by root
project 'scratch'. [incubating]
dependencies - Displays all dependencies declared in root
project 'scratch'.
dependencyInsight - Displays the insight into a specific
dependency in root project 'scratch'.
help - Displays a help message.
model - Displays the configuration model of root project
'scratch'. [incubating]
projects - Displays the sub-projects of root project 'scratch'.
properties - Displays the properties of root project 'scratch'.
tasks - Displays the tasks runnable from root project 'scratch'.
Verification tasks
------------------
check - Runs all checks.
test - Runs the unit tests.
28. Plugins
Plugin ID Automatically
Applies
Works With Description
java java-base Java compilation/testing
application java,distribution
ear java Java EE Support
maven java,war Maven publishing
war java Assembles WAR files
java-library-
distribution
java, distribution Support for tar/zip
distributions for Java
library.
idea java Generates IDEA files
eclipse java,groovy,
scala
Generates Eclipse files
Standard: https://docs.gradle.org/current/userguide/standard_plugins.html
Third party: https://plugins.gradle.org
37. [WAR] NetBeans Web App - Gradle
Java Source
directories
WAR Plugin
Local JARs
38. [WAR] NetBeans Web App – Gradle…
Web resource
directory
Java EE
Dependencies
Local JAR
39. WAR Plugin Configuration
Configuration Description
from Adds a file-set to the root of the archive
webInf Adds a file-set to the WEB-INF dir.
classpath Adds a file-set to the WEB-INF/lib dir
webAppDirName The name of the web application source directory, relative
to the project directory.
webXml Copies a file to WEB-INF/web.xml
56. Simple Docker Example – Run Container
Available Tasks:
• dockerRun
• dockerStop
• dockerRunStatus
• dockerRemoveContainer
57. Docker & Testing
• Launch Postgresql Docker container before unit tests
execute
• Test cleanup:
• Leave container running if any tests fail
• Destroy container if tests succeed