Сергей Моренец: "Gradle. Write once, build everywhere"
-
Upload
provectus -
Category
Technology
-
view
167 -
download
1
Transcript of Сергей Моренец: "Gradle. Write once, build everywhere"
Gradle 2.Write once, build everywhere.
Sergey Morenets, [email protected], 6 2015
About author• Works in IT since 2000• 11 year of Java SE/EE experience• Regular speaker at Java conferences• Author of “Development of Java applications” and
“Main errors in Java programming ”books• Founder of http://it-simulator.com• 3 years of Gradle usage
Agenda
Agenda
Agenda
Apache Ant• Offers extreme flexibility• Imposes no convention or project standards• Maintenance headache• Support Ruby, Groovy, JavaScript, Judoscript,
Jython scripting
Apache Ivy• Agile dependency manager• Transitive dependencies• Ant & Maven integration• Enhanced support of repositories
Apache Maven
• Support for dependency management• Standard project layout• Archetypes• Hard customization• Declarative approach• Plugin execution framework
Build systems
Apache Maven
Ant plugin
Groovy plugin
Echo plugin
Gradle
Issue #1. XML
• Large and complex files are hard to understand• Hierarchical structure limits the expressivenessof the format• Good format for the data and complex for the
flow
Hans Dockter• Founder of Gradle and Gradleware• 13 years of experience as a software developer,
team leader, architect, trainer, and mentor• Previously worked at Jboss and founded Jboss-IDE• Holds a Diploma in Physics with a minor in
Computer Science• Admirer of domain-driven-design
Slogan• Make the impossible possible• Make the possible easy• Make the easy elegant
Solution #1. Groovy
• An agile and dynamic language for the Java Virtual Machine
• Makes modern programming features available to Java developers with almost-zero learning curve
• Provides the ability to statically type check and statically compile your code for robustness and performance
• Share base syntax, type system, packages hierarchy with Java
• Every Gradle build file is Groovy script
Gradle
Gradle• Development started in Apr 2008• Current version 2.4 released in May 2015• Default build tool for Android OS
Gradle overview
• A flexible general purpose build tool• Programming tool• Declarative builds and build-by-convention• Multi-project support• Powerful dependency management
Development
Version Release dateMaven 1.0 2004
Maven 2.0 2005
Maven 3.0 2010
Maven 3.1 2013
Maven 3.3 2015
Development
Version Release dateGradle 0.7 2009
Gradle 1.0 2012
Gradle 1.5 2013
Gradle 2.0 2014
Gradle 2.4 2015
Build structure
Hello world
$ gradle helloWorld
build.gradle
Maven pom
Sample project
Sample project
Custom task
• Writes audit information at the end of the build• Audit information includes project name and build
timestamp• Audit files are located in the separate folder
Custom task
Custom task
Cache everything
• Remote metadata and artifacts• Transitive dependency resolution• Build execution plan• Plugin inputs and outputs• Test results
Daemon
• Improves startup and execution time of Gradle• Initial Gradle command forks daemon process• Subsequent Gradle commands reuse the build
daemon• If daemon is currently busy then new daemon
process is started on-demand• Useful for small tasks execution• Expires after 3 hours of idle time
gradle.properties
• org.gradle.daemon=true• org.gradle.configureondemand=true• org.gradle.parallel=true• org.gradle.java.home=C:\\Program Files\\Java\\
jdk1.8.0_45\\• org.gradle.jvmargs=-Xmx100m
Test execution
# of threads
H2 – time(sec) MySQL – time(sec)
1 24,4 27,5
2 31,1 36,9
4 46,2 51,1
8 71,1 80,5
Plugin• Reusable pieces of build logic• Can be used in different projects/builds
Plugin• Add tasks to the project• Pre-configure added tasks with useful defaults• Add dependency configurations to the project • Add new properties and methods to existing type
via extensions
Plugin import
Plugin portal
Custom plugin
gradle helloWorld
Maven
Gradle
Integrates with everything
Gradle and Maven
Properties
Properties
Comparison
• Maven 3.3.3• Gradle 2.4• JDK 1.8.0.45• Intel Core i7, 4 cores, 16 GB• Optimized and default modes
Comparison
Operation Gradle (opt)
Gradle (daemon)
Gradle(no daemon)
Maven
Maven (parall)
Build(sec) 8,03 10,73 13,68 12,40 13,02
Inc build(sec)
1,92 2,16 4,74 4,62 4,64
Clean(sec) 1,03 1,20 2,77 1,71 1,70
Gradle
Optimized Not optimized
Maven
Optimized Not optimized
Maven converter
• maven2Gradle is obsolete now• Build init plugin converts:
o POM settingso Dependencieso Propertieso Java compiler settingso Single- and multi-project settingso Packaging of sources and tests
Repositories
Maven Scope
Maven Scope
Scopes:• compile• provided• runtime• test• system• import
Dependencies
Scope flexibility
Version management
Profiles
• build.gradle• dev-profile.gradle• test-profile.gradle
• $ gradle –Pprofile=dev build
Skip tasks
• $ gradle -PskipTests
Caching
• Gradle caches all compiles scripts by default• Compiled scripts are put into .gradle folder• Gradle uses compiled version if the script hasn’t
changed• --recompile-scripts option discards cache
Wrapper
• Preferred way of starting a Gradle build• Gradle will be automatically downloaded via
wrapper• Includes shell script• Useful for CI tasks
Practical tasks
• Multiple projects• Liquibase• Deployment
Multiple projects• Huge project• Multiple sub-projects/pom.xml files• Complicated maintenance
Liquibase• Plugins for 2 and 3 versions• Lightweight front-end for Liquibase command-line• Gradle task for each Liquibase command
Deployment
• Separate plugins for Jetty/Tomcat• General plugin for multiple containers
Cargo plugin
Cargo plugin
Pros• Native Java/Scala/Groovy support• Ant/Maven/Ivy integration• Full IDE support• Flexible DSL• Multiple third-party plugins(70+)• Declarative & imperative approaches• Rapid development• Performance
Cons• Compilation & run-time issues• Larger learning curve• Less community & industry support
Future
Practice
• https://github.com/hibernate/hibernate-orm• https://github.com/SpringSource/spring-
framework• https://github.com/gradle/gradle