Building with Gradle

17
Building with Laurynas Tretjakovas [email protected] 1 Kaunas Java User Group, 2014

description

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

Transcript of Building with Gradle

Page 1: Building with Gradle

1

Building withLaurynas [email protected]

Kaunas Java User Group, 2014

Page 2: Building with Gradle

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.

2Kaunas Java User Group, 2014

Page 3: Building with Gradle

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.

3Kaunas Java User Group, 2014

Page 4: Building with Gradle

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?

4Kaunas Java User Group, 2014

Page 5: Building with Gradle

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.

5Kaunas Java User Group, 2014

Page 6: Building with Gradle

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;

6Kaunas Java User Group, 2014

Page 7: Building with Gradle

Building Java Projects• Simply include the plugin

7Kaunas Java User Group, 2014

Page 8: Building with Gradle

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.

8Kaunas Java User Group, 2014

Page 9: Building with Gradle

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.

9Kaunas Java User Group, 2014

Page 10: Building with Gradle

Dependency Management

10Kaunas Java User Group, 2014

Page 11: Building with Gradle

Source Sets

11Kaunas Java User Group, 2014

Page 12: Building with Gradle

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.

12Kaunas Java User Group, 2014

Page 13: Building with Gradle

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.

13Kaunas Java User Group, 2014

Page 14: Building with Gradle

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.

14Kaunas Java User Group, 2014

Page 15: Building with Gradle

Roadmap• Incremental Java compilation;• Persistable and much faster configuration;• Improved parallel execution;• Global cache;• Dependency variants;• Watcher mode;

15Kaunas Java User Group, 2014

Page 16: Building with Gradle

Demo• Sources - https://github.com/n3ziniuka5/gradle-examples

16Kaunas Java User Group, 2014

Page 17: Building with Gradle

Kaunas Java User Group, 2014 17

Thank You!Laurynas [email protected]