Goldilocks would use gradle

an introduction to gradle
by Matt Payne
10/15/2013
    Updated Tue Nov 5 12:13:14 CST 2013

About Me

Talk Outline

  1. 5WH: What, Why, Where, Who, When, and How
  2. Using and demoing gradle
    1. From scatch
    2. Eclipse Plugin
    3. What about maven and ant?
    4. Multiproject builds
    5. Multiple language builds
  3. Review shortcuts, tips, and tricks
  4. Maven broken?
  5. gradle-1.7/samples
  6. References

  7. Please ask questions and make suggestions!

    • "Everyone in this room is an expert!"
      • Jeff Sheets' great observation from 8/20/2013

What is gradle?

Google points to gradle's wikipedia entry:

Gradle is a project automation tool that builds upon the concepts of Apache Ant and Apache Maven and introduces a Groovy-based DSL instead of the more traditional XML form of declaring the project configuration. -- http://en.wikipedia.org/wiki/Gradle


I say:

  1. don't prototype with maven: use gradle.
  2. When you have a choice between ant, maven, and gradle: choose gradle.

There are lots of build systems

Build tools recognized by Search.Maven.org

  1. Maven
  2. Buildr
  3. Ivy
  4. Google Grape
  5. Grails (aka Gradle)
  6. Scala SBT

and there are many more still... For example WAF

  • Used by node.js and Samba.org

Why use gradle?

  1. Gradle manifesto:
  2. Make the impossible possible, make the possible easy and make the easy elegant.
    • (inspired by Moshe Feldenkrais)

philosophy and theory

  1. Technology Acceptance Model (TAM)
    • Perceived usefulness
    • Perceived ease-of-use
  2. Network effect

Where is gradle being used?

  1. Adoption curves
  2. Add the list of projects and companies that use gradle
    • Hibernate, spring, spring source, Tahles, Siemens, LinkedIn, etc

Who is who in the gradle zoo?

  1. http://www.gradleware.com/team
  2. GradleWare.com
  3. Tim Berglund -- Great books and articles

When is gradle ready to use?

Now

  1. One can have gradle doing builds while ant or maven are also doing builds
  2. Roadmap
  3. Events

How to get started using gradle? From...

  1. Command line
  2. Eclipse
  3. maven
  4. ant
  5. multi project
  6. multi language

Gradle on the command line

  1. Hello world project
  2. Turning project into an eclipse project

First gradle: the source

Hello.java

package demo;

/* ... */
public class Hello {

  public static void main(String[] args) {
    System.out.format("Hello world\n");
  }
}

First gradle: the build file

build.gradle

apply plugin: 'java'

First gradle: the session


mkdir -p src/main/java/demo
cat > src/main/java/demo/Hello.java 
cat > gradle.build
tree
gradle build
gradle build
gradle --daemon build
gradle build
java -cp build/libs/first.jar demo.Hello
  1. Let's note the timing differences
    • Tip 1: gradle's daemon makes things "faster"
  2. gradle build does several things
    • Compile
    • Build jar
    • Run tests
    • Build test report

That's a lot of files

  1. gradle clean; find * | wc -l # output 6
  2. gradle build; find * | wc -l # output 17

gradle tasks: That's a lot of tasks

Not a lot of gradle


mpayne-mbp:first mpayne$ cat build.gradle 
apply plugin: 'java'
mpayne-mbp:first mpayne$

special tasks

Build Setup tasks

  1. setupBuild - Initializes a new Gradle build. [incubating]
    • Generates directory tree and sample build file
  2. wrapper - Generates Gradle wrapper files. [incubating]
    • Generates shell/batch script that downloads gradle for you.
    • Wonderful if you continous integration (CI) server does not directly support gradle. Also if you want to share a script with someone who may not have gradle.
  3. And, there's a gui: gradle --gui

Dependency management: the source

MainDemoBeanUtils.java

package demo;
import org.apache.commons.beanutils.BeanUtils;

/* ... */
public class MainBeanUtilsDemo {
  private String jdbcDriverClass, dbUrl, dbUser, dbPassword;
  
  public MainBeanUtilsDemo(String[] args) throws FileNotFoundException,
    IOException, IllegalAccessException, InvocationTargetException {
    
    this.strPropertyFileName = args.length > 0 ? args[0] : "jdbc.properties";
    Properties properties = new Properties();
    properties.load(new FileReader(strPropertyFileName));
    BeanUtils.populate(this, properties); // remember the accessors
    System.out.format("user=%s password=%s\n", dbUser, dbPassword);
  }
}

Dependency management: the build file

build.gradle

apply plugin: 'java'
apply plugin: 'eclipse'
repositories { mavenCentral() }

dependencies { 
    compile 'commons-beanutils:commons-beanutils:1.8.3'
}

LMGTFY.com

What is that jar?

google the sha1sum or md5 of an unknown jar to find out what it's maven coordinates are

Dependency management: the session


mkdir -p src/main/java/demo # redundant
cat > src/main/java/demo/MainBeanUtilsDemo.java 
cat > gradle.build
echo 'org.gradle.daemon=true' > gradle.properties
gradle eclipse # So we can import this into eclipse and fix it up a bit.
gradle build # Don't need the --daemon now
  1. Tip 2: Use gradle.properties to always use the daemon
    • gradle's daemon makes things "faster"
    • ps -ef | egrep -i 'gradle'
  2. User Guide Chapter 20: The Build Environment

Using gradle's eclipse plugin

  1. add "apply plugin: 'eclipse'" to your build.gradle
  2. gradle eclipse
  3. In eclipse, import existing eclipse project
  4. joy

Next, Using eclipse's gradle plugin

Using eclipse's gradle plugin

  1. Gradle tooling
  2. Gradle Integration for Eclipse
    • I did not install the Spring Dashboard
    • github.com/spring-projects/eclipse-integration-gradle/

Dependency management: Running

  • Let's use gradle to run our code
  • Folks manage the classpath like this with ant and maven too...

defaultTasks 'BeanDemo'
apply plugin: 'java'
apply plugin: 'eclipse'
repositories { mavenCentral() }

dependencies { 
    compile 'commons-beanutils:commons-beanutils:1.8.3'
}

task BeanDemo(type: JavaExec) {
  main='demo.MainBeanUtilsDemo'
  classpath=runtimeClasspath
}
  1. gradle BeanDemo
  2. gradle BD
    • Tip 3: The CamelCaseShortCut (CCSC)

maven

  1. Gradle uses maven repos
  2. Gradle can publish to maven repos
    • Use gradle's maven plugin
    • apply plugin: 'maven'
    • gradle install # publishes to local maven repo
      • This includes utilties to build a pom.xml for your artifact

maven: Converting a maven project

Let's convert a simple mvn project to gradle. For example
kohsuke/github-api
  1. git clone https://github.com/kohsuke/github-api.git
  2. cd github-api
  3. gradle setupBuild
  4. vim pom.xml # Remove parent & move groupID to top
  5. less build.gradle

Reusing ant: ant.importBuild 'build.xml'


task zip << {
  ant.zip(destfile: 'archive.zip') {
      fileset(dir: 'src') {
          include(name: '**.xml')
          exclude(name: '**.java')
      }
  }
}
Many ant examples in the user guideExample from StackOverflow

Good things to know

  1. Build life cycle
  2. Task api
  3. The DSL is on top of groovy. So, all of groovy is there
  4. Groovy can be a lot like java

Build Life Cycle

  1. Initialization
  2. Configuration
  3. Execution
Chapter 55: The Build Lifecycle

Task API

  1. build.gradle is a program
  2. tasks are objects

Gradle DSL

  1. Gradle DSL Reference
  2. a simple groovy example
  3. simple example java like

Review shortcuts, tips, and tricks

  • Tip 1: daemon
    • gradle --daemon
  • Tip 2: gradle.properties
    • org.gradle.daemon=-true
  • Tip 3: The CamelCaseShortCut (CCSC)
  • Tip 4: default task
    • defaultTasks 'BeanDemo' # can be a comma seperated list
  • apply from http
    • common settings for projects on your whole team
  • github.com/spring-projects/eclipse-integration-gradle/
  • --parallel option runs unit tests in parallel

Maven broken?

  1. Some people think so
  2. Maven broken by design
    • 9/26/2013
  3. This is why Goldilocks would use gradle
  4. http://www.gradleware.com/news/blog/answering-more-gradle-v-maven-fud
  5. Is gradle perfect? Of course not!
  6. Discussion? Over beer?

gradle-1.7/samples

  1. webApplication
  2. ear
  3. groovy
  4. java/multiproject
  5. java/quickstart
  6. java/customizedLayout
  7. java/withIntegrationTests
    • gradle integrationTest

Free References

  1. http://www.gradle.org/learn
  2. http://mrhaki.blogspot.com
  3. gradle.org/book