domingo, 27 de outubro de 2013

Basics of Gradle based on User Guide

Gradle - http://www.gradle.org/docs/current/userguide/userguide_single.html#overview
Introduction
- Build Automate, test, publishing, deployiment and so on.
- Combines the flexibility of ANT with dependency management and conventions of Maven.
- Uses Groovy DSL and uses a declarative way to describe the builds.
Features
Declarative builds and build-by-convention
Gradle uses DSL(Domain Specific Language) based on Groovy.
Language for dependency based programming
The tasks are based in a hierarchy, favoring the builds
Structure your build
Easy to compose buid from reusable pieces
Deep API
Allow to monitor and customize the configuration and behavior execution
Scalability
Reusability allows one project uses parts of other projects increasing the produtivity
Multi-project builds
You can rebuild a project or its sub-project, that depends on another sub-project.
Different ways to manage dependencies
Maven has just one way. Can integrate with maven and ivy, or just use jars or directories.
Groovy
Insteady of XML.

Simple examples
- Project
Each project has many tasks
- To create a simple project, create a file build.gradle
Ex:

1:  task hello {  
2:  doLast {  
3:  println 'Hello world!'  
4:  }  
5:  }  
6:  OR  
7:  task hello << {  
8:                      println 'Hello world!'  
9:                  }  
RUN: gradle -q hello
It is possible to add behavior to an existing task

1:          task hello << {  
2:              println 'Hello Earth'  
3:          }  
4:          hello.doFirst {  
5:              println 'Hello Venus'  
6:          }  

Task Dependencies
One task can depend on another using:


1:  task taskX(dependsOn: 'taskY') << {  

Dynamic Tasks
Tasks can be dynamic. In this case, 4 tasks are created (task0 to task3). There is a dependency.

1:          4.times { counter ->  
2:  task "task$counter" << {  
3:                  println "I'm task number $counter"  
4:              }  
5:          }  
6:          task0.dependsOn task2, task3  
Default Tasks

1:  defaultTasks 'task1', 'task2'  
- Plugins
Plugin is an extension to Grails which configures a project in some way, typically adding pre-configured tasks.
Gradle with Java plugin
apply plugin: 'java'
To create a project, it is good to have the same structure that MAVEN project
PROJECT
- src
main
- java
- test
- java
- resources
- resources
buid
libs
Main Commands
gradle -->
build -> compile, test, build jar
clean -> delete the build directory
assemble -> Compile and build the jar. Does not run the tests
check -> compile and test
External dependencies
Repositories

1:  repositories {  
2:    mavenCentral()  
3:  }  
Dependencies

1:  dependencies {  
2:  compile -> dependency in compile-time  
3:  testCompile -> dependency in compile-time for test  
4:  runtime -> dependency in runtime  
5:  }  


Multi-project Java build create a file called: settings.gradle in the same directory of build.gradle.
Ex:

1:       include "shared", "api", "services:webservice", "services:shared"  
2:       rootProject.children.each { prj -> //para cada filho  
3:            prj.projectDir = new File("$rootDir/subprojects/$prj.name")  
4:       }  
5:       rootProject.name = 'groovy' // set the name of rootProject.  

Nenhum comentário:

Postar um comentário