Faster Java EE Builds with Gradle

Preview:

Citation preview

Faster Java EE Builds with GradleRYAN CUPRAK

About Ryan Cuprak

@ctjava rcuprak@gmail.com http://www.cuprak.info https://www.linkedin.com/in/rcuprak

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

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

Build System Evolution

AntMaven

Gradle2004

2009

2000

Gradle versus MavenFeature Gradle Maven

Fully configurable DAG ✅ ❌

Task Exclusion ✅ ❌

Dry Run ✅ ❌

Advanced Task Ordering ✅ ❌

Custom Distributions ✅ ❌

Repository Aware Cache ✅ ❌

Version Conflict Resolution ✅ ❌

File Based Dependencies ✅ ❌

Finalizers ✅ ❌

Custom Dependency Scopes ✅ ❌

ReplaceByRules ✅ ❌

https://gradle.org/maven-vs-gradle

Why Gradle?

Questions1. Do you need to learn Groovy?

No (Good idea)2. Do you need to completely refactor your code base?

No3. Do you need additional IDE plugins?

Maybe4. Do you need to change your build process?

Depends5. Do you need to port your entire build system over?

No – can port over individual modules6. Can you embed custom Ant logic?

Yes

Questions…7. Must all dependencies originate from a

repository?No

8. Can artifacts be pushed to a repository? Yes9. Can Jenkins initiate Gradle builds? Yes

Why Gradle for Java EE?Java EE projects are: Large Complex Contain many dependencies Ant lacks dependency managementLarge Ant files are a nightmare to debugMaven isn’t flexible Custom plugins aren’t the solution Evolving slowly

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

Key Gradle FilesBuild file build.gradleConfiguration settings settings.gradleLocal settings ~/.gradle/gradle.propertiesLocal repository (project)/.gradle

build.gradle = pom.xml = build.xml

Gradle DaemonGradle daemon is enabled by default

(Disable for continuous build environments!)Displaying status

gradle –statusStopping daemon:

gradle –stopDisabling daemon:

Add org.gradle.daemon=false to ~/.gradle

Project CreationTo start a new project: gradle init – creates a new project

Uses pom.xml if present. Imports multi-model projects

Optionally specify –type <type> java-library scala-library groovy-library basic (default) – no src directories created.

Central repository defaults to jcenter()https://bintray.com/bintray/jcenter

Project Creation… gradle init --type java-library

Default Project Layout

Initial Gradle File

Command line – listing tasksgradle –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'.

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.

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.

Sample Outputgradle build

Output

Projects versus TasksProject 1 Project 2

Project 3

Task 1

Task 3

Task 2Depends

on

Task 1

Task 2

Depends on

Task 1

Task 3

Task 2Depends

onTask 3

Depends on

Understanding build.gradleorg.gradle.api.Project

apply(options: Map<String,?>)buildscript(config: Closure)dependencies(config: Closure)configurations(config: Closure)getDependencies()getConfigurations()getAnt()getName()getDescription()getGroup()getPath()getVersion()getLogger()setDescription(description: String)setVersion(version: Object)file(path: Object)task(args: Map<String,?>,name: String)

Project is an implicit object.

Project.apply plugin: ‘java

Understanding Gradle Tasksorg.gradle.api.Task

dependsOn(tasks: Object…)doFirst(action: Closure)doLast(action: Closure)getActions()getInputs()getOutputs()getAnt()getDescription()getEnabled()getGroup()setDescription(description: String)setEnabled(enabled: boolean)setGroup(group: String)

Tasks are built on the Task object.

Defining Tasks

Task Dependencies

Grouping Tasks

Grouping Tasks

Custom Group

PluginsPlugin ID Automatically

AppliesWorks With

Description

java java-base Java compilation/testing

application java,distribution

ear java Java EE Supportmaven java,war Maven publishingwar java Assembles WAR filesjava-library-distribution

java, distribution

Support for tar/zip distributions for Java library.

idea java Generates IDEA fileseclipse java,groovy,

scalaGenerates Eclipse files

Standard: https://docs.gradle.org/current/userguide/standard_plugins.htmlThird party: https://plugins.gradle.org

Multi-Module Projects

Multi-Module ProjectsHierarchical Layout Flat Layout

Hierarchical Layout: Example Project

ctjavabuild.gradlesettings.gradle

ctcorebuild.gradlesettings.gradle

ctwebbuild.gradlesettings.gradle

migratebuild.gradlesettings.gradle

Hierarchical Layout: Top Level

build.gradle gradle.settings

ctjava

Hierarchical Layout: Second Level

gradle.settings for ctcore/migrate/ctweb: rootProject.name = 'ctjava’ migrate/ctweb dependencies on ctcore compile project (':ctcore’)

IDE Support

IDE SupportIDE Separate Plugin Java EE SupportIDEA (free) No YesIDEA (paid) No YesNetBeans Yes DependsEclipse Yes Yes

Eclipse Gradle Support http://download.eclipse.org/buildship/

updates/e46/releases/1.0

NetBeans Gradle Support

Multi-project Java EE projects not recognized.

IntelliJ Support

Legacy Project

NetBeans EE Project to GradleFile System Project Representation

NetBeans EE Project to Gradle

Java Source directories

WAR Plugin

Local JARs

NetBeans EE Project to Gradle

Web resource directory

Java EE Dependencies

Local JAR

Continued…

WAR Plugin ConfigurationConfiguration Descriptionfrom Adds a file-set to the root of the archivewebInf Adds a file-set to the WEB-INF dir.classpath Adds a file-set to the WEB-INF/lib dirwebAppDirName The name of the web application source directory,

relative to the project directory.webXml Copies a file to WEB-INF/web.xml

Recipes

JavaScript Minification

Minification Output

Google Minifier

JavaScript Minification… Extend JavaExec Task to invoke Minifier

JavaScript Minification…

gradle -PjsOptimize=true build

Node/Webpack Integration

Node Gradle Plugin

https://github.com/srs/gradle-node-plugin

Supports: NodeJS, Yarn, Grunt, Gulp

Generating JPA Meta-ModelCreate custom plugin to run Java Annotation Processor:

implementation-class=JavaAptPlugin

Custom Annotation ProcessorCustom annotation processor

Custom Annotation ProcessorCustom annotation processor

Custom Annotation Processor:

Build Plugin

Exclude everything but JPA entities

Custom annotation processor

EAR ProjectsProject Output

Contents of EAR

EAR Project

EAR plugin

Provided Scope – Non-WAR Projects

• providedCompile is a configuration on WAR plugin.• Non-WAR projects must add a custom scope.

jaxb Code Generation

POJOxsd

Generating JAX-WS Client Generate JAX-WS client for WSDL using wsimport Plugin:

https://plugins.gradle.org/plugin/me.seeber.wsimport Generated source code:

build/generated-src/wsimport

Generating JAX-WS Client

https://plugins.gradle.org/plugin/me.seeber.wsimport

Generating JAX-WS Client Generated Source Code

Docker Build Docker images from project output:

Transmode/gradle-docker - http://tinyurl.com/k7o7nab Build/publish docker files from build script – not Dockerfile

bmuschko/gradle-docker-plugin - http://tinyurl.com/hg4q6jr docker-remote-api – interacts with Docker via remote API docker-java-application – creates/pushes docker images for

java applications Run Docker containers during build

palantir/gradle-docker - http://tinyurl.com/hpw853h docker – building and pushing docker images docker-compose - populating placeholders in a docker-

compose template docker-run – starting/stopping/status on named images

Building Docker Images

Simple Docker Example – Run Container

Available Tasks:• dockerRun• dockerStop• dockerRunStatus• dockerRemoveContainer

Docker & Testing Launch PostgreSQL Docker container before unit

tests execute Test cleanup:

Leave container running if any tests fail Destroy container if tests succeed

Docker & Testing

Docker Database Testing

Parameter Substitution: persistence.xml

Parameter Substitution: build.gradle

Testing with Arquillian/Selenium

Misc View Dependencies:

gradle -q ctweb:dependencies

Build GUI:gradle –gui

Profiling:gradle –profile

Dryrungradle –m build

Reasons to Convert Incremental compilation Better dependency management/control Customizable without needing plugins Supports multiple languages/platforms Build system can be versioned

Q&A