faster java ee builds with gradle [con4921]

Post on 21-Jan-2017

219 Views

Category:

Software

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

FASTER JAVA EE BUILDS WITH GRADLERyan Cuprak

AboutRyan 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

Ant

Maven

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

Build Comparison ✅ ❌

Custom Dependency Scopes ✅ ❌

ReplaceByRules ✅ ❌

Why Gradle?

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

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 management• Large Ant files are a nightmare to debug

• Maven 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 Files

Build file build.gradle

Configuration settings settings.gradle

Local settings ~/.gradle/gradle.properties

Local repository (project)/.gradle

build.gradle = pom.xml = build.xml

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

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

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 Gradlegradle build

Output

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

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’

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

Group Tasks

Custom Group

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

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 SupportIDE Separate Plugin Java EE SupportIDEA (free) No Yes

IDEA (paid) No Yes

NetBeans Yes Depends

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

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

[WAR] NetBeans Web App - Gradle

Java Source directories

WAR Plugin

Local JARs

[WAR] NetBeans Web App – Gradle…

Web resource directory

Java EE Dependencies

Local JAR

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

JavaScript Minification

Minification Output

Google Minifier

JavaScript Minification…Extend JavaExec Task to invoke Minifier

JavaScript Minification…

gradle -PjsOptimize=true build

Generating JPA Meta-Model

Create custom plugin to run Java Annotation Processor:

implementation-class=JavaAptPlugin

Custom Annotation Processor

Custom Annotation Processor

Custom Annotation Processor:Build Plugin

Exclude everything but JPA entities

EAR ProjectsProject Output

EAR Project

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

• Dry-rungradle –m build

Q&A

top related