64
FASTER JAVA EE BUILDS WITH GRADLE Ryan Cuprak

Faster java ee builds with gradle [con4921]

Embed Size (px)

Citation preview

Page 1: Faster java ee builds with gradle [con4921]

FASTER JAVA EE BUILDS WITH GRADLERyan Cuprak

Page 2: Faster java ee builds with gradle [con4921]

AboutRyan Cuprak

• @ctjava• [email protected]• http://www.cuprak.info• https://www.linkedin.com/in/rcuprak

Page 3: Faster java ee builds with gradle [con4921]

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

Page 4: Faster java ee builds with gradle [con4921]

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

Page 5: Faster java ee builds with gradle [con4921]

Build System Evolution

Ant

Maven

Gradle2004

2009

2000

Page 6: Faster java ee builds with gradle [con4921]

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 ✅ ❌

Build Comparison ✅ ❌

Custom Dependency Scopes ✅ ❌

ReplaceByRules ✅ ❌

Page 7: Faster java ee builds with gradle [con4921]

Why Gradle?

Page 8: Faster java ee builds with gradle [con4921]

Questions1. 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

Page 9: Faster java ee builds with gradle [con4921]

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

Page 10: Faster java ee builds with gradle [con4921]

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

Page 11: Faster java ee builds with gradle [con4921]

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

Page 12: Faster java ee builds with gradle [con4921]

Key Gradle Files

Build file build.gradle

Configuration settings settings.gradle

Local settings ~/.gradle/gradle.properties

Local repository (project)/.gradle

build.gradle = pom.xml = build.xml

Page 13: Faster java ee builds with gradle [con4921]

Gradle Daemon• Gradle daemon is enabled by default

• Disable for continuous build environments!• Displaying status

gradle –status• Stopping daemon:

gradle –stop• Disabling daemon:

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

Page 14: Faster java ee builds with gradle [con4921]

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

• Uses pom.xml if present.• Import 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

Page 15: Faster java ee builds with gradle [con4921]

Project Creation…gradle init --type java-library

Default Project Layout

Page 16: Faster java ee builds with gradle [con4921]

Initial Gradle File

Page 17: Faster java ee builds with gradle [con4921]

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'.

Page 18: Faster java ee builds with gradle [con4921]

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.

Page 19: Faster java ee builds with gradle [con4921]

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.

Page 20: Faster java ee builds with gradle [con4921]

Sample Gradlegradle build

Output

Page 21: Faster java ee builds with gradle [con4921]

Projects versus TasksProject 1 Project 2

Project 3

Task 1

Task 3

Task 2

Depends on

Task 1

Task 2

Depends on

Task 1

Task 3

Task 2

Depends on

Task 3

Depends on

Page 22: Faster java ee builds with gradle [con4921]

Understanding Gradle Build Fileorg.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)

Within a Gradle file, project implicit. Above could be written as:project.apply plugin: ‘java’

Page 23: Faster java ee builds with gradle [con4921]

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.

Page 24: Faster java ee builds with gradle [con4921]

Defining Tasks

Page 25: Faster java ee builds with gradle [con4921]

Task Dependencies

Page 26: Faster java ee builds with gradle [con4921]

Grouping Tasks

Page 27: Faster java ee builds with gradle [con4921]

Group Tasks

Custom Group

Page 28: Faster java ee builds with gradle [con4921]

PluginsPlugin ID Automatically

AppliesWorks 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.htmlThird party: https://plugins.gradle.org

Page 29: Faster java ee builds with gradle [con4921]

Multi-Module ProjectsHierarchical Layout Flat Layout

Page 30: Faster java ee builds with gradle [con4921]

Hierarchical Layout: Example Project

ctjavabuild.gradlesettings.gradle

ctcorebuild.gradlesettings.gradle

ctwebbuild.gradlesettings.gradle

migratebuild.gradlesettings.gradle

Page 31: Faster java ee builds with gradle [con4921]

Hierarchical Layout: Top Level

build.gradle gradle.settings

ctjava

Page 32: Faster java ee builds with gradle [con4921]

Hierarchical Layout: Second Level• gradle.settings for ctcore/migrate/ctweb: rootProject.name = 'ctjava’• migrate/ctweb dependencies on ctcore:

compile project (':ctcore’)

Page 33: Faster java ee builds with gradle [con4921]

IDE SupportIDE Separate Plugin Java EE SupportIDEA (free) No Yes

IDEA (paid) No Yes

NetBeans Yes Depends

Eclipse Yes Yes

Page 34: Faster java ee builds with gradle [con4921]

Eclipse Gradle Support http://download.eclipse.org/buildship/updates/e46/releases/1.0

Page 35: Faster java ee builds with gradle [con4921]

NetBeans Gradle Support

Multi-project Java EE projects not recognized.

Page 36: Faster java ee builds with gradle [con4921]

[WAR] NetBean Web App – Ant BuildFile System File System

Page 37: Faster java ee builds with gradle [con4921]

[WAR] NetBeans Web App - Gradle

Java Source directories

WAR Plugin

Local JARs

Page 38: Faster java ee builds with gradle [con4921]

[WAR] NetBeans Web App – Gradle…

Web resource directory

Java EE Dependencies

Local JAR

Page 39: Faster java ee builds with gradle [con4921]

WAR Plugin ConfigurationConfiguration Descriptionfrom 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

Page 40: Faster java ee builds with gradle [con4921]

JavaScript Minification

Minification Output

Google Minifier

Page 41: Faster java ee builds with gradle [con4921]

JavaScript Minification…Extend JavaExec Task to invoke Minifier

Page 42: Faster java ee builds with gradle [con4921]

JavaScript Minification…

gradle -PjsOptimize=true build

Page 43: Faster java ee builds with gradle [con4921]

Generating JPA Meta-Model

Create custom plugin to run Java Annotation Processor:

implementation-class=JavaAptPlugin

Page 44: Faster java ee builds with gradle [con4921]

Custom Annotation Processor

Page 45: Faster java ee builds with gradle [con4921]

Custom Annotation Processor

Page 46: Faster java ee builds with gradle [con4921]

Custom Annotation Processor:Build Plugin

Exclude everything but JPA entities

Page 47: Faster java ee builds with gradle [con4921]

EAR ProjectsProject Output

Page 48: Faster java ee builds with gradle [con4921]

EAR Project

Page 49: Faster java ee builds with gradle [con4921]

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

Page 50: Faster java ee builds with gradle [con4921]

jaxb Code Generation

POJOxsd

Page 51: Faster java ee builds with gradle [con4921]

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

Page 52: Faster java ee builds with gradle [con4921]

Generating JAX-WS Client

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

Page 53: Faster java ee builds with gradle [con4921]

Generating JAX-WS Client Generated Source Code

Page 54: Faster java ee builds with gradle [con4921]

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

Page 55: Faster java ee builds with gradle [con4921]

Building Docker Images

Page 56: Faster java ee builds with gradle [con4921]

Simple Docker Example – Run Container

Available Tasks:• dockerRun• dockerStop• dockerRunStatus• dockerRemoveContainer

Page 57: Faster java ee builds with gradle [con4921]

Docker & Testing• Launch Postgresql Docker container before unit tests

execute• Test cleanup:

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

Page 58: Faster java ee builds with gradle [con4921]

Docker & Testing

Page 59: Faster java ee builds with gradle [con4921]

Docker Database Testing

Page 60: Faster java ee builds with gradle [con4921]

Parameter Substitution: persistence.xml

Page 61: Faster java ee builds with gradle [con4921]

Parameter Substitution: build.gradle

Page 62: Faster java ee builds with gradle [con4921]

Testing with Arquillian/Selenium

Page 63: Faster java ee builds with gradle [con4921]

Misc• View Dependencies:

gradle -q ctweb:dependencies

• Build GUI:gradle –gui

• Profiling:gradle –profile

• Dry-rungradle –m build

Page 64: Faster java ee builds with gradle [con4921]

Q&A