Here are slides from basic training for Gradle.
This training is aimed to help Java Developers to get hands-on experience to use Gradle as a primary build tool for Java source code starting from simple compilation continuing with different kinds of tests and finishing with code quality analysis and artefacts publishing.
2. ● SA at EPAM Systems
● primary skill is Java
● hands-on-coding with Groovy, Ruby
● exploring FP with Erlang/Elixir
● passionate about agile, clean code and devops
9. #2 Test on closed to prod environment
Principles
#1 Each change auto. built and deployed
10. #2 Test on closed to prod environment
#1 Each change auto. built and deployed
#3 Integrate as frequently as possible
Principles
11. #2 Test on closed to prod environment
#1 Each change auto. built and deployed
#3 Integrate as frequently as possible
Principles
#4 The highest priority is to fix failed build
12. Benefits
● Each change guarantees working code
● Each update should guarantee working
code ;)
● There is no delay for epic merge
● Less bugs - depends on your tests
efficiency*
● Allows to have code ready to go live
13. Challenges
● Need to build infrastructure
● Need to build team culture
● Need to support/enhance infrastructure
● Overhead with writing different kinds of
tests
17. Principles
#1 Every commit should result in a release
#2 Automated tests are essential
#3 Automate everything!
18. Principles
#1 Every commit should result in a release
#2 Automated tests are essential
#3 Automate everything!
#4 Done means release/live in prod
19. Benefits
● Speed of delivery of business idea to
customer
● Easy going live deployment
● Less time spent on delivery - more profit
● More motivation to do more as you can
see what you can change/improve
20. Challenges
● Big effort to implement changes for:
○ database increment/rollback
○ infrastructure rollout/rollback
○ decrease down time …
● Need to get customers to buy in
● Security policies
25. Gradle
- General purpose build system
- Comes with a rich DSL based on Groovy
- Follows ”build-by-convention” principles
26. Gradle
- General purpose build system
- Comes with a rich DSL based on Groovy
- Follows ”build-by-convention” principles
- Built-in plug-ins for JVM languages, etc
27. Gradle
- General purpose build system
- Comes with a rich DSL based on Groovy
- Follows ”build-by-convention” principles
- Built-in plug-ins for JVM languages, etc
- Derives all the best from Ivy, Ant & Maven
34. 1. Run to see default tasks list
gradle tasks
2. Replace build.gradle file content with
apply plugin: 'java'
3. Run to see new available tasks
gradle tasks
4. Checkout step s3_apply_plugin
5. Run to build Java source code
gradle build
6. Explore directory build
#2 Create simple build
36. 1. Run task with part of name
gradle ta
2. Run task with part of name to clean and compile
gradle cle tC
3. Run task with part of name to clean and compile and
exclude processTestResources
gradle cle tC -x pTR
4. Get details for task
gradle -q help --task clean
#3 Execute tasks
37. 1. Run task
gradle tasks
2. Run task to generate wrapper
gradle wrapper
3. Run tasks using wrapper
./gradlew tasks
4. Customize task wrapper to use another Gradle version
task wrapper(type: Wrapper) {
gradleVersion = '2.2.1'
}
5. Check Gradle version
./gradlew -v
#4 Use wrapper
39. 1. Checkout step s5_prepare
2. Add directory common
3. Move src to common
4. Create common/build.gradle for Java
5. Add new module to settings.gradle
include ':common'
6. Run build
./gradlew clean build
7. Run task for module
./gradlew :com:compJ
#5 Create multi-module build
42. Gradle
- compile - to compile source
- runtime - required by classes at runtime
43. Gradle
- compile - to compile source
- runtime - required by classes at runtime
- testCompile - to compile test sources
44. Gradle
- compile - to compile source
- runtime - required by classes at runtime
- testCompile - to compile test sources
- testRuntime - required to run the tests
45. 1. Add repositories to download dependencies from to
build.gradle
allprojects { currProject ->
repositories {
mavenLocal()
mavenCentral()
jcenter()
maven {url 'http://repo.mycompany.com/’}
}
}
#6 Dependencies
46. 1. Add common dependencies for all subprojects in
build.gradle
subprojects {
apply plugin: 'java'
dependencies {
compile 'org.slf4j:slf4j-api:1.7.7'
testCompile
'org.mockito:mockito-core:1.10.19',
'junit:junit:4.12'
}
}
#6.1 Dependencies
47. 1. Add dependencies for concrete module in
common/build.gradle
dependencies {
compile
'org.projectlombok:lombok:1.14.4'
}
#6.2 Dependencies
48. 1. List project dependencies
./gradlew :common:dependencies
#6.3 Dependencies
54. ● Lifecycle
● Create a Settings instance for the build.
● Evaluate the settings.gradle script, if present, against the Settings
object to configure it.
● Use the configured Settings object to create the hierarchy of Project
instances.
● Finally, evaluate each Project by executing its build.gradle file, if
present, against the project. The project are evaluated in such order
that a project is evaluated before its child projects.
55. ● Tasks
● A project is essentially a collection of Task objects.
● Each task performs some basic piece of work.
● Dependencies
● A project generally has a number of dependencies.
● Project generally produces a number of artifacts, which other projects
can use.
56. ● Plugins
● Plugins can be used to modularise and reuse project configuration.
● Properties
● Any property or method which your script uses is delegated through to
the associated Project object.
● A project has 5 property 'scopes'.
● Dynamic Methods
● A project has 5 method 'scopes'.
61. 1. Open build.gradle to add dependency for one task
from another
printParameter.dependsOn 'help'
2. Run printParameter task
./gradlew printParameter
#8.2 Tasks Dependencies
62. 1. Open build.gradle to add ordering for one task from
another
task areTestsExist {
if ([
file("${projectDir}/src/test/java").listFiles()
].isEmpty()) {
println 'Test directory is empty'
} else {
println 'Test directory is not empty, will
execute tests'
}
}
test.mustRunAfter areTestsExist
#8.3 Tasks Ordering*
63. 2. Run test task
./gradlew test
#8.3 Tasks Ordering*
64. 1. Add rule to validate running of integration tests task
tasks.addRule('Check correctness of running tests'){ String
taskName ->
gradle.taskGraph.whenReady{
Map<String, String> args =
gradle.startParameter.systemPropertiesArgs
gradle.taskGraph.allTasks.each { Task task ->
if (task.name.contains('integTest') &&
!args.containsKey('profile')) {
throw new
org.gradle.api.tasks.StopExecutionException("Profile was not
specified to run tests (-Dprofile=ci).")
}
}
}
}
#8.4 Rules
65. 2. Run check task to have failure
./gradlew check
3. Run check task with expected parameter
./gradlew check -Dprofile=ci
#8.4 Rules
67. 1. Switch to 9th step and execute next command
./gradlew test --parallel
2. Try to modify amount of executable threads
./gradlew test --parallel --parallel-threads=3
#9 Parallel builds
74. 2. Apply configuration from gradle/coverage.gradle in
build.gradle
3. Implement proper test for proper method
4. Let’s run check task to collect coverage metrics
./gradlew check -Dprofile=ci
5. Open
common/build/reports/jacoco/test/html/index.html file
to overview coverage
#11 Test Coverage
75. 1. Add JaCoCo configuration in gradle/coverage.gradle for
integration tests
task jacocoIntegrationTestReport(type: JacocoReport) {
dependsOn integTest
sourceSets sourceSets.main
executionData integTest
reports {
xml {
enabled true
destination
"$buildDir/reports/jacoco/integTest/jacocoIntegTestReport.xml"
}
csv.enabled false
html {
destination "$buildDir/reports/jacoco/integTest/html"
}
}
}
#11.1 Integration Test Coverage
86. ● Build script
Visible for build file
● buildSrc/src/main/groovy
Visible for project
● Standalone project
Could be shared between projects using binary artefact
87. 1. Create file PluginsPrinterPlugin.groovy in
buildSrc/src/main/groovy
import org.gradle.api.Plugin
import org.gradle.api.Project
public class PluginsPrinterPlugin implements Plugin<Project> {
void apply(Project project) {
project.task('printPlugins') << {
println 'Current project has next list of plugins:'
ext.plugins = project.plugins.collect { plugin ->
plugin.class.simpleName
}
println plugins
}
}
}
#14 Plugins Printer
88. 2. Apply plugin for all projects in build.gradle file
allprojects {
apply plugin: PluginsPrinterPlugin
}
3. Let’s run printPlugins task to print plugins activated for
project
./gradlew printPlugins
#14 Plugins Printer