Gradle Introduction Notes
Here's my notes from this excellent introduction to Gradle. Most of the project I work with use maven, but I'm seeing gradle more and more. These notes help me quickly get my brain back up to speed.
- The Video
- My source code from it
Notes from Gradle a Gentle Introduction
- Enterprise apps look really different, gradle works well with that...
- ant is a imperative (step by step)
- maven is a declarative build (these lead to conventions, which you might not want, you can add new conventions)
Tasks
a task, the fundimental unit of build activity
$ touch build.gradle task helloWorld // a task is an object
$ gradle task //show tasks, hello world now there task hellowworld helloWorld { doLast { //super of hellowWorld has a method called doLast println "hello, world" } }
$gradle helloWorld hello, world //this prints output
Create Two Tasks
task hello task world world { doLast { println "world" } } hello { doLast { println "hello" } }
Now run it...
$ gradle world world $ gradle hello hello
Task Dependencies
Make world depend on hellow
task hello task world world { dependsOn << hello doLast { println "world" } } hello { doLast { println "hello" } }
Run it
$ gradle world hello, world
Another Dependency Example
task hello task world task helloWorld { dependsOn = [ world, hello ] } world { dependsOn << hello doLast { println "world" } } hello { doLast { println "hello" } }
Ways to run it
$ gradle helloWorld $ gradle hW //shorthand for methods with camel case
Documentation
Look up documentation for existing tasks. If a task is an object, lets look at the documentation for other objects.
file:///Tools/gradle/docs/dsl/index.html
---- He answers some questions here
questions: the tasks are built into a graph
Plugins
So far the examples are still pretty imperitive. Let's look at this documentation (standard_plugins.html), which lists the plugins gradle comes with.
Let's start by creating a Java program.
$ mkdir -p src/main/java/org/gradle/poetry/ vi src/main/java/org/gradle/poetry/ package org.gradle.poetry; public class Poetry { public List juliusCaesar() { List lines = new ArrayList(); lines.add("O, pardon me, thou bleeding piece of earth"); lines.add("That I am meek and gentle with these butchers!"); ... return lines; } public void emit(List lines) { for (String line : lines) { System.out.println(line); } } public static void main(STring[] args) { Poetry p = new Poetry(); p.emit(p.juliusCaesar()); } } }
Now add the Java plugin to the gradle file
$ vi build.gradle apply plugin: 'java' $ gradle tasks //notice we now have more tasks because we added the java plugin assemble build buildDepedents classes $ gradle build //notice the new build folder created with our compiled classes $ java -cp build/classes/main org.gradle.poetry.Poetry
We can even write a task to run the application.
$ vi build.gradle apply plugin: 'java' task caesar(type: JavaExec) { //create a task object which takes a type: JavaExec main = 'org.gradle.poetry.Poetry' classpath = runtimeClasspath } $ gradle caesar //output from the program... // gradle is smart enough to know when to recompile your project
Nexus and Dependencies
Let's update our project to depend on a dependency from Maven Central.
$ vi poetry.java package org.gradle.poetry; public class Poetry { public List juliusCaesar() { List lines = new ArrayList(); lines.add("O, pardon me, thou bleeding piece of earth"); lines.add("That I am meek and gentle with these butchers!"); ... return lines; } public void emit(List lines) { for (String line : lines) { System.out.println(encode(line); //use new method } } public String encode(String text) { //new method Base64 codec = new Base64(); return new String(codec.encode(text.getBytes())); } public static void main(STring[] args) { Poetry p = new Poetry(); p.emit(p.juliusCaesar()); } } }
Now let's update the build.gralde file to search the repo, as well as include it.
//lets get Base64 into our code $ vi build.gradle apply plugin: 'java' repositories { mavenCentral() } dependencies { compile 'commons-codec:commons-codec:1.6' } task caesar(type: JavaExec) { //create a task object which takes a type: JavaExec main = 'org.gradle.poetry.Poetry' classpath = runtimeClasspath } $ gradle caesar laiclaiwj elajc selicj alsdj casd falsijd claisje clkasjd clkja sldkjc a s liajs elicja lskdjc laksjd clakjs df
Misc
We could add the following anywhere to print the classpath...
println configurations.compile.asPath
Hmm, it looks like build.Gradle is its own class and you override methods in it like dependencies