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