Mostrando postagens com marcador continuous integration. Mostrar todas as postagens
Mostrando postagens com marcador continuous integration. Mostrar todas as postagens

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.  

Basics of Maven

- Maven
                Features and goals:
                               - Simplify the build process, sharing JARs, publishing informations in a easy way.
                               - Deploy a system
                               - Manage Documentation, dependencies, releases, distribution, report
                               - Maven works on top of ANT
                                               Ant needs you to write the logic and provide de data
                                               Maven needs you to provide the data and know how to do most things.
                               - Maven has a local repository and well named jars, docs, etc.
                                               Provide a single location for downloads which versions are created automatically
                - POM (Project Object Model)
                              
                - Create a Project
                               Simple way:
                                                - mvn archetype:generate
                                                - There are over 850 archetypes.  Use the 314(
                                                -archetype-quickstart), sugested but maven
                                                - Version of maven-archetype-quickstart: Use the sugested(1.1), number 6.
                                                - groupId: The groupId that will be inside the pom.xml. I use: br.com.romalopes.meven2Test
                                                - artifactId: mavenToGradle.  The project will be created inside this directory.
                                                - Version: Accept the sugested.
                                                - Value for property papckage: The package that will be create inside the structure:br.com.romalopes.meven2Test
                                                - Confirm
                              
                               - A more sofisticated way
                                               - mvn archetype:generate -DgroupId=br.com.romalopes.maven2Test -DartifactId=maven-test -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
                It creates a structure like that:
                - my-app
                               |-- pom.xml
                               -- src
                                               |-- main
                                               |   `-- java.
                                               |       `-- com.
                                               |           `-- mycompany-
                                               |               `-- app
                                               |                   `-- App.java
                                               |-- resources
                                                              -- META-INF
                                                              -- application.properties   -->  InputStream is = getClass().getResourceAsStream( "/test.properties" );
                                               -- test
                                                               -- java
                                                                               -- com
                                                                                              -- mycompany
                                                                                                              -- app
                                                                                                                              -- AppTest.java
                - Phases to buil a project
                               Validate
                               generate-sources
                               process-sources
                               generate-resources
                               process-resources
                               compile
                               mvn package
                               integration-test
                               verify
                               install - install into the local repository
                               deploy - copies the final package to the remote repository
                               - others
                                               clean
                                               test
                                               dependency:copy-dependencies
                                               mvn site - generate a site for project documentation
                                               can call
                                                               mvn clean dependency:copy-dependencies package
                              
                - maven to Eclipse
                               Simple java
                                               mvn eclipse:eclipse
                               Dynamic Web Project
                                               mvn eclipse:eclipse -Dwtpversion=1.5
                                               In Eclipse:
                                                               Properties->Modify Project->Change Facet
                - plugins
                               To customize the build for a Maven Project
                                               
- Example

1:          <project xmlns="http://maven.apache.org/POM/4.0.0"  
2:           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
3:           xsi:schemaLocation="http://maven.apache.org/POM/4.0.0  
4:                               http://maven.apache.org/xsd/maven-4.0.0.xsd">  
5:           <modelVersion>4.0.0</modelVersion>  
6:           <groupId>com.mycompany.app</groupId>  
7:           <artifactId>my-app</artifactId>  
8:           <version>1.0-SNAPSHOT</version>  
9:           <packaging>jar</packaging>  
10:           <name>Maven Quick Start Archetype</name>  
11:           <url>http://maven.apache.org</url>  
12:           <dependencies>  
13:              <dependency>  
14:               <groupId>junit</groupId>  
15:               <artifactId>junit</artifactId>  
16:               <version>3.8.1</version>  
17:               <scope>test</scope>  
18:              </dependency>  
19:           </dependencies>  
20:           <build>  
21:              <resources>  
22:               <resource>  
23:                  <directory>src/main/resources</directory>  
24:                  <filtering>true</filtering>  
25:               </resource>  
26:              </resources>  
27:           </build>  
28:           <properties>  
29:              <my.filter.value>hello</my.filter.value>  
30:           </properties>  
31:          </project>