"Building with Gradle" by Laurynas Tretjakovas.
A short introduction to Gradle. How to start using Gradle for Java projects and how does it compare to Maven and Ant.
The Ultimate Guide to Choosing WordPress Pros and Cons
Building with Gradle
1. Building with
Laurynas Tret jakovas
laurynas.tret jakovas@gmail.com
Kaunas Java User Group, 2014 1
2. Why Gradle?
• Ant makes you reinvent the wheel every time;
• Ant build scripts can get hard to maintain very quickly;
• Maven does not care about your project, you must make sure that your project
works well with Maven – it enforces convention;
• You must do everything “The Maven Way” or implement custom plugins;
• Gradle combines the power and flexibility of Ant with dependency management
of Ivy and conventions of Maven;
• Gradle is a programming build tool with DSL based on Groovy.
Kaunas Java User Group, 2014 2
3. Features
• Build Java, Android, Scala, Groovy, c++ and others;
• Dependency management;
• Incremental builds;
• Task exclusion;
• Supports plugins;
• Multi-project builds;
• Hook into any build step;
• Camel case invocation – helloWorld task can be called by doing gradle hW;
• Gradle wrapper;
• HTML test reporting;
• Ant integration.
Kaunas Java User Group, 2014 3
4. Hello World
• Gradle has two phases – configuration and execution;
• Run each task and see what gets printed out
• How would you do that with Maven?
Kaunas Java User Group, 2014 4
5. Tasks
• Gradle has task types for most common operations;
• You can specify inputs and outputs for a task. If those haven‘t changed since last
execution, it won‘t be executed again;
• You can create your own task types by extending DefaultTask.
Kaunas Java User Group, 2014 5
6. Task Dependencies
• shouldRunAfter is used for task ordering, not for dependencies. For example, it
can be used for running unit tests before integration tests. This may be ignored
during parallel task execution;
• mustRunAfter will never be ignored, use this for strong ordering;
Kaunas Java User Group, 2014 6
8. Java Plugin Tasks
• compileJava - compiles production Java source files using javac;
• processResources - copies production resources into the production classes
directory;
• classes - assembles the production classes directory;
• compileTestJava - compiles test Java source files using javac;
• processTestResources - Copies test resources into the test classes directory;
• testClasses - Assembles the test classes directory;
• jar - Assembles the jar file;
• javadoc - Generates API documentation for the production Java source, using
Javadoc;
• test - Runs the unit tests using JUnit or TestNG.
Kaunas Java User Group, 2014 8
9. Lifecycle Tasks
• assemble - Assembles all the archives in the project;
• check - Performs all verification tasks in the project;
• build - Performs a full build of the project (check and assemble);
• buildNeeded - Performs a full build of the project and all projects it depends on;
• buildDependents - Performs a full build of the project and all projects which
depend on it.
Kaunas Java User Group, 2014 9
12. Test Reporting
• Tests have XML and HTML reporting by default;
• If you have multiple test tasks, you will need a separate reporting task;
• HTML reporting includes execution time, stdout and stderr outputs.
Kaunas Java User Group, 2014 12
13. The Gradle Wrapper
• Gradle wrapper allows developers to use gradle, without even downloading it
manually;
• Every developer will be using the same version for building and CI server won‘t
need additional configuration;
• Run wrapper task once, it will create gradle executables which you will commit
into source control;
• You can specify the version or full url.
Kaunas Java User Group, 2014 13
14. Init Scripts
• Init scripts can be run each time before task execution;
• You can pass init scripts with -I flag or include .gradle files in
GRADLE_HOME/init.d/ directory;
• This can be useful for enterprise policies and repositories.
Kaunas Java User Group, 2014 14
15. Roadmap
• Incremental Java compilation;
• Persistable and much faster configuration;
• Improved parallel execution;
• Global cache;
• Dependency variants;
• Watcher mode;
Kaunas Java User Group, 2014 15
16. Demo
• Sources - https://github.com/n3ziniuka5/gradle-examples
Kaunas Java User Group, 2014 16
17. Thank You!
Laurynas Tret jakovas
laurynas.tret jakovas@gmail.com
Kaunas Java User Group, 2014 17